3 namespace Friendica\Core\Cache;
5 use Friendica\Core\Cache;
11 * Memcache Cache Driver
13 * @author Hypolite Petovan <hypolite@mrpetovan.com>
15 class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
18 use TraitCompareDelete;
26 * @param string $memcache_host
27 * @param int $memcache_port
30 public function __construct($memcache_host, $memcache_port)
32 if (!class_exists('Memcache', false)) {
33 throw new Exception('Memcache class isn\'t available');
36 $this->memcache = new Memcache();
38 if (!$this->memcache->connect($memcache_host, $memcache_port)) {
39 throw new Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
46 public function get($key)
49 $cachekey = $this->getCacheKey($key);
51 // We fetch with the hostname as key to avoid problems with other applications
52 $cached = $this->memcache->get($cachekey);
54 // @see http://php.net/manual/en/memcache.get.php#84275
55 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
59 $value = @unserialize($cached);
61 // Only return a value if the serialized value is valid.
62 // We also check if the db entry is a serialized
63 // boolean 'false' value (which we want to return).
64 if ($cached === serialize(false) || $value !== false) {
74 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
76 $cachekey = $this->getCacheKey($key);
78 // We store with the hostname as key to avoid problems with other applications
80 return $this->memcache->set(
87 return $this->memcache->set(
98 public function delete($key)
100 $cachekey = $this->getCacheKey($key);
101 return $this->memcache->delete($cachekey);
107 public function clear($outdated = true)
112 return $this->memcache->flush();
119 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
121 $cachekey = $this->getCacheKey($key);
122 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);