19 define(
'SQ_DEJA_VU_CONTROL', SQ_DATA_PATH.
'/private/conf/.dejavu');
20 define(
'SQ_DEJA_VU_MEMCACHE_PREFIX',
'sq_deja_vu|');
23 define(
'SQ_DEJA_VU_ASSET',
'asset');
24 define(
'SQ_DEJA_VU_LOCALE',
'locale_manager');
69 if (is_file(SQ_DEJA_VU_CONTROL)) {
70 $this->_enabled = (int) file_get_contents(SQ_DEJA_VU_CONTROL);
71 if ($this->_enabled !== 1) $this->_enabled = 0;
75 assert_true(extension_loaded(
'memcache'),
'Cannot use Deja Vu; it requires the memcache PECL extension installed within , which is not installed');
76 assert_true(file_exists(SQ_DATA_PATH.
'/private/conf/memcache.inc'),
'Cannot use Deja Vu; the Memcache configuration file is not set');
78 $memcache_conf = require(SQ_DATA_PATH.
'/private/conf/memcache.inc');
79 $hosts =& $memcache_conf[
'hosts'];
80 $services =& $memcache_conf[
'services'];
82 assert_true(count($hosts) > 0,
'Cannot use Deja Vu; no hosts are defined in the Memcache configuration file');
83 assert_true(array_key_exists(
'deja_vu', $services) === TRUE,
'Cannot use Deja Vu; no Memcache hosts are assigned');
84 assert_true(count($services[
'deja_vu']) > 0,
'Cannot use Deja Vu; no Memcache hosts are assigned');
87 $this->_memcache =
new Memcache;
89 foreach ($services[
'deja_vu'] as $host_key => $weight) {
90 assert_true(array_key_exists($host_key, $hosts) === TRUE,
'Cannot use Deja Vu; host key "'.$host_key.
'" assigned for use for Deja Vu but not defined as a host');
91 $host = $hosts[$host_key];
92 $this->_memcache->addServer($host[
'host'], $host[
'port'], $host[
'persistent'], $weight, $host[
'timeout'], $host[
'retry_interval'], $host[
'status'], Array($this,
'failureCallback'));
95 $this->_memcache->setCompressThreshold($memcache_conf[
'compression_threshold'], $memcache_conf[
'compression_min_saving']);
98 $em = $GLOBALS[
'SQ_SYSTEM']->getEventManager();
99 $em->addEventListener($this, Array(
'AssetStatusUpdate',
122 return $this->_enabled;
136 public function recall($type, $assetid = NULL, $contextid = NULL)
139 if (strpos($assetid,
':') !== FALSE)
return NULL;
142 if (defined(
'SQ_ROLLBACK_VIEW') && SQ_ROLLBACK_VIEW)
return null;
143 $cache_key = $this->_getCacheKey($type, $assetid, $contextid);
146 $obj = $this->_memcache->get($cache_key);
154 if ($obj instanceof __PHP_Incomplete_Class) {
155 $obj_def = (array) $obj;
157 $type_code = strtolower($obj_def[
'__PHP_Incomplete_Class_Name']);
159 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
160 if ($am->installed($type_code)) {
161 $am->includeAsset($type_code);
164 $obj = $this->_memcache->get($cache_key);
167 log_error(
'Deja Vu could not load asset type "'.$type_code.
'" for #'.$assetid.
'. Assuming the asset type has been uninstalled, so forgetting this asset.');
168 $this->
forget($assetid, $type);
177 $data = $this->_memcache->get($cache_key);
201 public function remember($type, $assetid, $object, $contextid = NULL)
204 if (strpos($assetid,
':') !== FALSE)
return NULL;
207 if (defined(
'SQ_ROLLBACK_VIEW') && SQ_ROLLBACK_VIEW)
return null;
208 return $this->_memcache->set($this->_getCacheKey($type, $assetid, $contextid), $object);
220 public function forget($type, $assetid = NULL, $contextid = NULL)
222 return $this->_memcache->delete($this->_getCacheKey($type, $assetid, $contextid));
234 return $this->_memcache->flush();
249 private function _getCacheKey($type, $assetid = NULL, $contextid = NULL)
252 case SQ_DEJA_VU_LOCALE:
253 return SQ_DEJA_VU_MEMCACHE_PREFIX.
'|'.$type;
256 $contextid = is_null($contextid) ? $GLOBALS[
'SQ_SYSTEM']->getContextId() : $contextid;
257 return SQ_DEJA_VU_MEMCACHE_PREFIX.$contextid.
'\\'.$assetid.
'|'.$type;
278 $contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
279 foreach ($contexts as $contextid => $context) {
280 $this->
forget(SQ_DEJA_VU_ASSET, $broadcaster->id, $contextid);
295 $contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
296 foreach ($contexts as $contextid => $context) {
297 $this->
forget(SQ_DEJA_VU_ASSET, $broadcaster->id, $contextid);
313 $this->
forget(SQ_DEJA_VU_ASSET, $broadcaster->id);
328 $this->
forget(SQ_DEJA_VU_ASSET, $broadcaster->id);
343 $this->
forget(SQ_DEJA_VU_ASSET, $broadcaster->id);
358 $contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
359 foreach ($contexts as $contextid => $context) {
360 $this->
forget(SQ_DEJA_VU_ASSET, $broadcaster->id, $contextid);
378 $rv = (bool) file_put_contents(SQ_DEJA_VU_CONTROL,
'1');
398 $rv = (bool) file_put_contents(SQ_DEJA_VU_CONTROL,
'0');
419 log_error(get_class().
" failure communicating with $hostname:$port", E_USER_WARNING);