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(
'memcache') && class_exists(
'Memcache')) !=
true)
78 $this->_persistent = $config->get(
'memcache_persist',
true);
79 $this->_compress = $config->get(
'memcache_compress',
false) ==
false ? 0 : MEMCACHE_COMPRESSED;
87 $server[
'host'] = $config->get(
'memcache_server_host',
'localhost');
88 $server[
'port'] = $config->get(
'memcache_server_port', 11211);
91 self::$_db =
new Memcache;
92 self::$_db->addServer($server[
'host'], $server[
'port'], $this->_persistent);
94 $memcachetest = @self::$_db->connect($server[
'host'], $server[
'port']);
95 if ($memcachetest ==
false)
97 throw new RuntimeException(
'Could not connect to memcache server', 404);
101 if (self::$_db->
get($this->_hash .
'-index') ===
false)
104 self::$_db->set($this->_hash .
'-index', $empty, $this->_compress, 0);
121 public function get($id, $group, $checkTime =
true)
123 $cache_id = $this->_getCacheId($id, $group);
124 $back = self::$_db->get($cache_id);
135 public function getAll()
139 $keys = self::$_db->get($this->_hash .
'-index');
140 $secret = $this->_hash;
146 foreach ($keys as $key)
152 $namearr = explode(
'-', $key->name);
154 if ($namearr !==
false && $namearr[0] == $secret && $namearr[1] ==
'cache')
157 $group = $namearr[2];
159 if (!isset($data[$group]))
165 $item = $data[$group];
168 $item->updateSize($key->size / 1024);
170 $data[$group] = $item;
189 public function store($id, $group, $data)
191 $cache_id = $this->_getCacheId($id, $group);
193 if (!$this->lockindex())
198 $index = self::$_db->get($this->_hash .
'-index');
199 if ($index ===
false)
204 $tmparr =
new stdClass;
205 $tmparr->name = $cache_id;
206 $tmparr->size = strlen($data);
209 $lifetime = (int) $config->get(
'cachetime', 15);
210 if ($this->_lifetime == $lifetime)
212 $this->_lifetime = $lifetime * 60;
216 self::$_db->replace($this->_hash .
'-index', $index, 0, 0);
217 $this->unlockindex();
220 if (!self::$_db->replace($cache_id, $data, $this->_compress, $this->_lifetime))
222 self::$_db->set($cache_id, $data, $this->_compress, $this->_lifetime);
238 public function remove($id, $group)
240 $cache_id = $this->_getCacheId($id, $group);
242 if (!$this->lockindex())
247 $index = self::$_db->get($this->_hash .
'-index');
248 if ($index ===
false)
253 foreach ($index as $key => $value)
255 if ($value->name == $cache_id)
261 self::$_db->replace($this->_hash .
'-index', $index, 0, 0);
262 $this->unlockindex();
264 return self::$_db->delete($cache_id);
279 public function clean($group, $mode = null)
281 if (!$this->lockindex())
286 $index = self::$_db->get($this->_hash .
'-index');
287 if ($index ===
false)
292 $secret = $this->_hash;
293 foreach ($index as $key => $value)
296 if (strpos($value->name, $secret .
'-cache-' . $group .
'-') === 0 xor $mode !=
'group')
298 self::$_db->delete($value->name, 0);
302 self::$_db->replace($this->_hash .
'-index', $index, 0, 0);
303 $this->unlockindex();
314 public static function isSupported()
316 if ((extension_loaded(
'memcache') && class_exists(
'Memcache')) !=
true)
322 $host = $config->get(
'memcache_server_host',
'localhost');
323 $port = $config->get(
'memcache_server_port', 11211);
325 $memcache =
new Memcache;
326 $memcachetest = @$memcache->connect($host, $port);
349 public function lock($id, $group, $locktime)
351 $returning =
new stdClass;
352 $returning->locklooped =
false;
354 $looptime = $locktime * 10;
356 $cache_id = $this->_getCacheId($id, $group);
358 if (!$this->lockindex())
363 $index = self::$_db->get($this->_hash .
'-index');
364 if ($index ===
false)
369 $tmparr =
new stdClass;
370 $tmparr->name = $cache_id;
373 self::$_db->replace($this->_hash .
'-index', $index, 0, 0);
374 $this->unlockindex();
376 $data_lock = self::$_db->add($cache_id .
'_lock', 1,
false, $locktime);
378 if ($data_lock ===
false)
385 while ($data_lock ===
false)
388 if ($lock_counter > $looptime)
390 $returning->locked =
false;
391 $returning->locklooped =
true;
396 $data_lock = self::$_db->add($cache_id .
'_lock', 1,
false, $locktime);
401 $returning->locked = $data_lock;
416 public function unlock($id, $group = null)
418 $cache_id = $this->_getCacheId($id, $group) .
'_lock';
420 if (!$this->lockindex())
425 $index = self::$_db->get($this->_hash .
'-index');
426 if ($index ===
false)
431 foreach ($index as $key => $value)
433 if ($value->name == $cache_id)
439 self::$_db->replace($this->_hash .
'-index', $index, 0, 0);
440 $this->unlockindex();
442 return self::$_db->delete($cache_id);
452 protected function lockindex()
455 $data_lock = self::$_db->add($this->_hash .
'-index_lock', 1,
false, 30);
457 if ($data_lock ===
false)
463 while ($data_lock ===
false)
465 if ($lock_counter > $looptime)
472 $data_lock = self::$_db->add($this->_hash .
'-index_lock', 1,
false, 30);
487 protected function unlockindex()
489 return self::$_db->delete($this->_hash .
'-index_lock');