3 namespace Friendica\Core\Cache;
5 use Friendica\BaseObject;
6 use Friendica\Core\Cache;
9 * Memcache Cache Driver
11 * @author Hypolite Petovan <mrpetovan@gmail.com>
13 class MemcacheCacheDriver extends BaseObject implements ICacheDriver
20 public function __construct($memcache_host, $memcache_port)
22 if (!class_exists('Memcache', false)) {
23 throw new \Exception('Memcache class isn\'t available');
26 $this->memcache = new \Memcache();
28 if (!$this->memcache->connect($memcache_host, $memcache_port)) {
29 throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
33 public function get($key)
37 // We fetch with the hostname as key to avoid problems with other applications
38 $cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key);
40 // @see http://php.net/manual/en/memcache.get.php#84275
41 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
45 $value = @unserialize($cached);
47 // Only return a value if the serialized value is valid.
48 // We also check if the db entry is a serialized
49 // boolean 'false' value (which we want to return).
50 if ($cached === serialize(false) || $value !== false) {
57 public function set($key, $value, $duration = Cache::MONTH)
59 // We store with the hostname as key to avoid problems with other applications
60 return $this->memcache->set(
61 self::getApp()->get_hostname() . ":" . $key,
68 public function delete($key)
70 return $this->memcache->delete($key);
73 public function clear()