10 defined(
'JPATH_PLATFORM') or die;
28 protected static $_db = null;
36 protected $_persistent =
false;
44 protected $_compress = 0;
53 public function __construct($options = array())
55 parent::__construct($options);
56 if (self::$_db === null)
58 $this->getConnection();
70 protected function getConnection()
72 if ((extension_loaded(
'memcached') && class_exists(
'Memcached')) !=
true)
78 $this->_persistent = $config->get(
'memcache_persist',
true);
79 $this->_compress = $config->get(
'memcache_compress',
false) ==
false ? 0 : Memcached::OPT_COMPRESSION;
87 $server[
'host'] = $config->get(
'memcache_server_host',
'localhost');
88 $server[
'port'] = $config->get(
'memcache_server_port', 11211);
91 if ($this->_persistent)
94 self::$_db =
new Memcached($session->getId());
98 self::$_db =
new Memcached;
100 $memcachedtest = self::$_db->addServer($server[
'host'], $server[
'port']);
102 if ($memcachedtest ==
false)
104 throw new RuntimeException(
'Could not connect to memcached server', 404);
107 self::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress);
110 if (self::$_db->
get($this->_hash .
'-index') ===
false)
113 self::$_db->set($this->_hash .
'-index', $empty, 0);
130 public function get($id, $group, $checkTime =
true)
132 $cache_id = $this->_getCacheId($id, $group);
133 $back = self::$_db->get($cache_id);
144 public function getAll()
148 $keys = self::$_db->get($this->_hash .
'-index');
149 $secret = $this->_hash;
153 if (!empty($keys) && is_array($keys))
155 foreach ($keys as $key)
161 $namearr = explode(
'-', $key->name);
163 if ($namearr !==
false && $namearr[0] == $secret && $namearr[1] ==
'cache')
166 $group = $namearr[2];
168 if (!isset($data[$group]))
174 $item = $data[$group];
177 $item->updateSize($key->size / 1024);
179 $data[$group] = $item;
198 public function store($id, $group, $data)
200 $cache_id = $this->_getCacheId($id, $group);
202 if (!$this->lockindex())
207 $index = self::$_db->get($this->_hash .
'-index');
208 if ($index ===
false)
213 $tmparr =
new stdClass;
214 $tmparr->name = $cache_id;
215 $tmparr->size = strlen($data);
217 self::$_db->replace($this->_hash .
'-index', $index, 0);
218 $this->unlockindex();
221 if (!self::$_db->replace($cache_id, $data, $this->_lifetime))
223 self::$_db->set($cache_id, $data, $this->_lifetime);
239 public function remove($id, $group)
241 $cache_id = $this->_getCacheId($id, $group);
243 if (!$this->lockindex())
248 $index = self::$_db->get($this->_hash .
'-index');
249 if ($index ===
false)
254 foreach ($index as $key => $value)
256 if ($value->name == $cache_id)
262 self::$_db->replace($this->_hash .
'-index', $index, 0);
263 $this->unlockindex();
265 return self::$_db->delete($cache_id);
280 public function clean($group, $mode = null)
282 if (!$this->lockindex())
287 $index = self::$_db->get($this->_hash .
'-index');
288 if ($index ===
false)
293 $secret = $this->_hash;
294 foreach ($index as $key => $value)
297 if (strpos($value->name, $secret .
'-cache-' . $group .
'-') === 0 xor $mode !=
'group')
299 self::$_db->delete($value->name, 0);
303 self::$_db->replace($this->_hash .
'-index', $index, 0);
304 $this->unlockindex();
315 public static function isSupported()
317 if ((extension_loaded(
'memcached') && class_exists(
'Memcached')) !=
true)
323 $host = $config->get(
'memcache_server_host',
'localhost');
324 $port = $config->get(
'memcache_server_port', 11211);
326 $memcached =
new Memcached;
327 $memcachedtest = @$memcached->addServer($host, $port);
350 public function lock($id, $group, $locktime)
352 $returning =
new stdClass;
353 $returning->locklooped =
false;
355 $looptime = $locktime * 10;
357 $cache_id = $this->_getCacheId($id, $group);
359 if (!$this->lockindex())
364 $index = self::$_db->get($this->_hash .
'-index');
365 if ($index ===
false)
370 $tmparr =
new stdClass;
371 $tmparr->name = $cache_id;
375 self::$_db->replace($this->_hash .
'-index', $index, 0);
377 $this->unlockindex();
379 $data_lock = self::$_db->add($cache_id .
'_lock', 1, $locktime);
381 if ($data_lock ===
false)
388 while ($data_lock ===
false)
391 if ($lock_counter > $looptime)
393 $returning->locked =
false;
394 $returning->locklooped =
true;
399 $data_lock = self::$_db->add($cache_id .
'_lock', 1, $locktime);
404 $returning->locked = $data_lock;
419 public function unlock($id, $group = null)
421 $cache_id = $this->_getCacheId($id, $group) .
'_lock';
423 if (!$this->lockindex())
428 $index = self::$_db->get($this->_hash .
'-index');
429 if ($index ===
false)
434 foreach ($index as $key => $value)
436 if ($value->name == $cache_id)
443 self::$_db->replace($this->_hash .
'-index', $index, 0);
444 $this->unlockindex();
446 return self::$_db->delete($cache_id);
456 protected function lockindex()
459 $data_lock = self::$_db->add($this->_hash .
'-index_lock', 1, 30);
461 if ($data_lock ===
false)
467 while ($data_lock ===
false)
469 if ($lock_counter > $looptime)
476 $data_lock = self::$_db->add($this->_hash .
'-index_lock', 1, 30);
491 protected function unlockindex()
493 return self::$_db->delete($this->_hash .
'-index_lock');