3 namespace Friendica\Core\Cache;
5 use Friendica\Core\Cache;
11 * Memcache Cache Driver
13 * @author Hypolite Petovan <mrpetovan@gmail.com>
15 class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
18 use TraitCompareDelete;
25 public function __construct($memcache_host, $memcache_port)
27 if (!class_exists('Memcache', false)) {
28 throw new Exception('Memcache class isn\'t available');
31 $this->memcache = new Memcache();
33 if (!$this->memcache->connect($memcache_host, $memcache_port)) {
34 throw new Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
41 public function get($key)
44 $cachekey = $this->getCacheKey($key);
46 // We fetch with the hostname as key to avoid problems with other applications
47 $cached = $this->memcache->get($cachekey);
49 // @see http://php.net/manual/en/memcache.get.php#84275
50 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
54 $value = @unserialize($cached);
56 // Only return a value if the serialized value is valid.
57 // We also check if the db entry is a serialized
58 // boolean 'false' value (which we want to return).
59 if ($cached === serialize(false) || $value !== false) {
69 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
71 $cachekey = $this->getCacheKey($key);
73 // We store with the hostname as key to avoid problems with other applications
75 return $this->memcache->set(
82 return $this->memcache->set(
93 public function delete($key)
95 $cachekey = $this->getCacheKey($key);
96 return $this->memcache->delete($cachekey);
102 public function clear($outdated = true)
107 return $this->memcache->flush();
114 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
116 $cachekey = $this->getCacheKey($key);
117 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);