3 namespace Friendica\Core\Cache;
5 use Friendica\Core\Cache;
8 * Memcache Cache Driver
10 * @author Hypolite Petovan <mrpetovan@gmail.com>
12 class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
15 use TraitCompareDelete;
22 public function __construct($memcache_host, $memcache_port)
24 if (!class_exists('Memcache', false)) {
25 throw new \Exception('Memcache class isn\'t available');
28 $this->memcache = new \Memcache();
30 if (!$this->memcache->connect($memcache_host, $memcache_port)) {
31 throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
38 public function get($key)
41 $cachekey = $this->getCacheKey($key);
43 // We fetch with the hostname as key to avoid problems with other applications
44 $cached = $this->memcache->get($cachekey);
46 // @see http://php.net/manual/en/memcache.get.php#84275
47 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
51 $value = @unserialize($cached);
53 // Only return a value if the serialized value is valid.
54 // We also check if the db entry is a serialized
55 // boolean 'false' value (which we want to return).
56 if ($cached === serialize(false) || $value !== false) {
66 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
68 $cachekey = $this->getCacheKey($key);
70 // We store with the hostname as key to avoid problems with other applications
72 return $this->memcache->set(
79 return $this->memcache->set(
90 public function delete($key)
92 $cachekey = $this->getCacheKey($key);
93 return $this->memcache->delete($cachekey);
99 public function clear($outdated = true)
104 return $this->memcache->flush();
111 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
113 $cachekey = $this->getCacheKey($key);
114 return $this->memcache->add($cachekey, $value, $ttl);