/** * Writes the cache object to the cache * @param zibo\library\cache\CacheObject $object * @return null */ public function writeToCache(CacheObject $object) { $type = $object->getType(); $id = $object->getId(); $this->readType($type); if (!isset($this->cache[$type])) { $this->cache[$type] = array(); } $this->cache[$type][$id] = $object; }
/** * Writes the cache object to the cache * @param zibo\library\cache\CacheObject $object * @return null */ public function writeToCache(CacheObject $object) { $type = $object->getType(); $id = $object->getId(); $this->readType($type); if (!array_key_exists($type, $this->cache)) { $this->cache[$type] = array(); } $this->cache[$type][$id] = $object; }
public function testAccess() { $time = time(); $cache = new CacheObject('type', 'id', ''); sleep(1); $cache->access(); $accessTime = $time + 1; $this->assertEquals(1, $cache->getTimesAccessed()); $this->assertEquals($time, $cache->getCreationDate()); $this->assertEquals($accessTime, $cache->getLastAccessDate()); }
/** * Writes a cache object to file * @param zibo\library\cache\CacheObject $object Cache object to write to the data source * @return null */ public function writeToCache(CacheObject $object) { $cacheFile = $this->getCacheFile($object->getType(), $object->getId()); $cacheDirectory = $cacheFile->getParent(); $cacheDirectory->create(); $serializedValue = serialize($object); if ($this->useLock) { $cacheFile->lock(); $cacheFile->write($serializedValue); $cacheFile->unlock(); } else { $cacheFile->write($serializedValue); } }
public function testGetCleansUpWhenCleanUpAgeReached() { $type = 'type'; $id = 'id'; $value = 'value'; $object = new CacheObject($type, $id, $value); sleep(3); $object->access(); $IOMock = $this->getMock('zibo\\library\\cache\\io\\CacheIO', array('readFromCache', 'clearCache', 'writeToCache')); $IOMock->expects($this->once())->method('readFromCache')->with($this->equalTo($type), $this->equalTo($id))->will($this->returnValue($object)); $IOMock->expects($this->once())->method('clearCache')->with($this->equalTo($type), $this->equalTo($id)); $cache = new ExtendedCache($IOMock); $cache->setCleanUpAge(2); $cache->get($type, $id); }
/** * Processes the read of a cache object * @param CacheObject $cacheObject The cache object to process * @return mixed The value of the cache object */ protected function processObject(CacheObject $cacheObject = null) { if (!$cacheObject) { return null; } $cacheObject->access(); if ($this->needCleanUp($cacheObject)) { $this->io->clearCache($cacheObject->getType(), $cacheObject->getId()); } else { $this->io->writeToCache($cacheObject); } return $cacheObject->getData(); }