25 require_once SQ_CORE_PACKAGE_PATH.
'/system/cache_storage/cache_storage/cache_storage.inc';
27 define(
'SQ_CACHE_MEMCACHE_PREFIX',
'sq_cache|');
28 define(
'SQ_CACHE_MEMCACHE_ASSET_INDEX_PREFIX',
'sq_cache_asset_index|');
29 define(
'SQ_CACHE_MEMCACHE_URL_INDEX_PREFIX',
'sq_cache_url_index|');
63 parent::__construct($assetid);
68 public function initMemcache()
70 assert_true(extension_loaded(
'memcache'),
'Cannot use Memcache Cache Storage; it requires the memcache PECL extension installed within PHP, which is not installed');
71 assert_true(is_file(SQ_DATA_PATH.
'/private/conf/memcache.inc'),
'Cannot use Memcache Cache Storage; the Memcache configuration file is not set');
73 $memcache_conf = require(SQ_DATA_PATH.
'/private/conf/memcache.inc');
74 $hosts =& $memcache_conf[
'hosts'];
75 $services =& $memcache_conf[
'services'];
77 assert_true(count($hosts) > 0,
'Cannot use Memcache Cache Storage; no hosts are defined in the Memcache configuration file');
78 assert_true(array_key_exists(
'cache_manager', $services) === TRUE,
'Cannot use Memcache Cache Storage; no Memcache hosts are assigned to cache manager');
79 assert_true(count($services[
'cache_manager']) > 0,
'Cannot use Memcache Cache Storage; no Memcache hosts are assigned to cache manager');
82 $this->_memcache =
new Memcache;
84 foreach ($services[
'cache_manager'] as $host_key => $weight) {
85 assert_true(array_key_exists($host_key, $hosts) === TRUE,
'Cannot use Memcache Cache Storage; host key "'.$host_key.
'" assigned for use for cache manager but not defined as a host');
86 $host = $hosts[$host_key];
87 $this->_memcache->addServer($host[
'host'], $host[
'port'], $host[
'persistent'], $weight, $host[
'timeout'], $host[
'retry_interval'], $host[
'status'], Array($this,
'failureCallback'));
90 $this->_memcache->setCompressThreshold($memcache_conf[
'compression_threshold'], $memcache_conf[
'compression_min_saving']);
104 if (extension_loaded(
'memcache') === TRUE) {
105 if (is_file(SQ_DATA_PATH.
'/private/conf/memcache.inc')) {
149 function store($cache_key, $perm_key, $url, $assetid, $data, $expiry)
151 if (!$this->_memcache) $this->initMemcache();
153 if ($this->_memcache) {
156 $expiry_length = $expiry - time();
157 if ($expiry_length === 0)
return FALSE;
160 $index_url = md5(trim(preg_replace(
'/\?.*/',
'', $url),
'/'));
163 $mcache_key = SQ_CACHE_MEMCACHE_PREFIX.
"$cache_key|$perm_key|$url|$assetid";
166 $indexes = $this->_memcache->get(Array(SQ_CACHE_MEMCACHE_ASSET_INDEX_PREFIX.$assetid, SQ_CACHE_MEMCACHE_URL_INDEX_PREFIX.$index_url));
169 $assetid_hits = isset($indexes[SQ_CACHE_MEMCACHE_ASSET_INDEX_PREFIX.$assetid]) ? $indexes[SQ_CACHE_MEMCACHE_ASSET_INDEX_PREFIX.$assetid] : Array();
170 if (!in_array($mcache_key, $assetid_hits)) {
171 array_push($assetid_hits, $mcache_key);
172 $this->_memcache->set(SQ_CACHE_MEMCACHE_ASSET_INDEX_PREFIX.$assetid, $assetid_hits);
176 $url_hits = isset($indexes[SQ_CACHE_MEMCACHE_URL_INDEX_PREFIX.$index_url]) ? $indexes[SQ_CACHE_MEMCACHE_URL_INDEX_PREFIX.$index_url] : Array();
177 if (!in_array($mcache_key, $url_hits)) {
178 array_push($url_hits, $mcache_key);
179 $this->_memcache->set(SQ_CACHE_MEMCACHE_URL_INDEX_PREFIX.$index_url, $url_hits);
183 $ok = $this->_memcache->set($mcache_key, $data, FALSE, $expiry_length);
212 function read($cache_key, $perm_key, $url, $assetid)
214 if (!$this->_memcache) $this->initMemcache();
216 if ($this->_memcache) {
218 return $this->_memcache->get(SQ_CACHE_MEMCACHE_PREFIX.
"$cache_key|$perm_key|$url|$assetid", FALSE);
234 if (!$this->_memcache) $this->initMemcache();
236 if ($this->_memcache) {
237 return $this->_memcache->flush();
283 if (!$this->_memcache) $this->initMemcache();
286 foreach ($assetids as $assetid) {
287 $assetid_hits = $this->_memcache->get(SQ_CACHE_MEMCACHE_ASSET_INDEX_PREFIX.$assetid, FALSE);
289 foreach ($assetid_hits as $key) {
290 $this->_memcache->delete($key);
292 $this->_memcache->delete(SQ_CACHE_MEMCACHE_ASSET_INDEX_PREFIX.$assetid);
312 if (!$this->_memcache) $this->initMemcache();
315 $index_key = SQ_CACHE_MEMCACHE_URL_INDEX_PREFIX.md5($url);
316 $url_hits = $this->_memcache->get($index_key);
319 foreach ($url_hits as $key) {
320 $this->_memcache->delete($key);
322 $this->_memcache->delete($index_key);
375 log_error(get_class().
" failure communicating with $hostname:$port", E_USER_WARNING);