<?php if (!defined('VB_ENTRY')) die('Access denied.'); class vB_Cache_Memory extends vB_Cache { protected $events = array(); protected $cache = array(); protected static $instance; public function __construct($cachetype) { parent::__construct($cachetype); $this->cachetype = $cachetype; } public static function instance($type = self::CACHE_STD) { if (!isset(self::$instance)) { $class = __CLASS__; self::$instance = new $class($type); } return self::$instance; } protected function writeCache($cache) { $data = $cache['data']; $cacheKey = $cache['key']; $events = $cache['events']; if (empty($events)) { $events = array(); } else if (!is_array($events)) { $events = array($events); } if (!empty($this->values_read[$cacheKey]['events'])) { $oldevents = $this->values_read[$cacheKey]['events']; if(!is_array($oldevents)) { $oldevents = array($oldevents); } foreach ($oldevents AS $eventKey) { unset($this->events[$eventKey][$cacheKey]); if (empty($this->events[$eventKey])) { unset($this->events[$eventKey]); } } } if (!empty($events)) { $this->addEvents($cacheKey, $events); } } public function addEvents($key, $events) { if (!is_array($events)) { $events = array($events); } foreach ($events AS $event) { if (empty($this->events[$event])) { $this->events[$event] = array(); } $this->events[$event][$key] = $key; } } public function removeEvents($event) { if (isset($this->events[$event])) { unset($this->events[$event]); } } protected function readCache($key) { if (!isset($this->values_read[$key])) { return false; } $cache = $this->values_read[$key]; if (($cache === false) OR ( ($cache['expires'] > 0) AND ($this->timeNow > $cache['expires']))) { return 0; } return $cache['data']; } public function event($events) { if (!is_array($events)) { $events = array($events); } foreach ($events AS $event) { if (empty($event) OR !is_string($event)) { throw new vB_Exception_Cache('invalid_data'); } if (array_key_exists($event, $this->events)) { $cacheKeys = $this->events[$event]; foreach($cacheKeys AS $cacheKey) { $this->purgeCache($cacheKey); } unset($this->events[$event]); } } } protected function expireCache($key){} public function lock($key) { return true; } protected function getLoadedValue($key) { return $this->readCache($key); } protected function purgeCache($cacheKey) { if (empty($cacheKey) OR !is_string($cacheKey)) { return false; } if (isset($this->values_read[$cacheKey])) { unset($this->values_read[$cacheKey]); return true; } return false; } public function clean($only_expired = true) { if ($only_expired) { foreach($this->values_read as $cacheKey => $cacheRecord) { if (!empty($cacheRecord['expires']) AND ($cacheRecord['expires'] > $this->timeNow)) { $this->purgeCache($cacheKey); } } if (self::$cacheLogging) { $this->logCacheAction(0, self::CACHE_LOG_CLEAR, $this->cachetype); } } else { $this->values_read = array(); $this->events = array(); } } } 