/** * */ public function onAfterWrite() { $serializer = CacheHelper::get_serializer(); // update the cache CacheHelper::get_cache()->save($serializer->serialize($this->owner), $this->key()); parent::onAfterWrite(); }
/** * FixMe: Currently only db and has_one fields are cached. * @param string|int $identifier an identifier which will be used for key generation * @param callable $callback the function which can be called to fetch the data * @return DataObject */ protected function cache($identifier, $callback) { $serializer = CacheHelper::get_serializer(); // check for cacheable extension of data object class $className = $this->dataClass; if (Object::has_extension($className, 'CacheableExtension')) { // search in cache $cache = CacheHelper::get_cache(); $key = CacheHelper::to_key("{$className}.{$identifier}"); if ($data = $cache->load($key)) { return $serializer->deserialize($data); } else { // if not found in cache, perform callback $data = $callback(); if (!$data) { // if result is empty, return null return null; } $cachedFunctions = array_keys($data->hasOne()); foreach ($cachedFunctions as $fn) { $data->{$fn}(); } $cache->save($serializer->serialize($data), $key); // return result return $data; } } else { return $callback(); } }
public function testCacheDataObject() { $key = 'Member_1'; $data = $this->cacheDataObject(); $cache = CacheHelper::get_cache()->load($key); $this->assertEquals($data, CacheHelper::get_serializer()->deserialize($cache)); }
public function testGetSerializer() { $serializer = CacheHelper::get_serializer(); $this->assertTrue(ClassInfo::classImplements($serializer->class, 'ICacheSerializer')); }