3 namespace Friendica\Core\Cache;
6 use Friendica\Core\Config\IConfiguration;
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
14 class MemcacheCache extends Cache implements IMemoryCache
17 use TraitCompareDelete;
18 use TraitMemcacheCommand;
28 public function __construct(string $hostname, IConfiguration $config)
30 if (!class_exists('Memcache', false)) {
31 throw new Exception('Memcache class isn\'t available');
34 parent::__construct($hostname);
36 $this->memcache = new Memcache();
38 $this->server = $config->get('system', 'memcache_host');;
39 $this->port = $config->get('system', 'memcache_port');
41 if (!@$this->memcache->connect($this->server, $this->port)) {
42 throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
49 public function getAllKeys($prefix = null)
51 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
53 return $this->filterArrayKeysByPrefix($keys, $prefix);
59 public function get($key)
62 $cachekey = $this->getCacheKey($key);
64 // We fetch with the hostname as key to avoid problems with other applications
65 $cached = $this->memcache->get($cachekey);
67 // @see http://php.net/manual/en/memcache.get.php#84275
68 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
72 $value = @unserialize($cached);
74 // Only return a value if the serialized value is valid.
75 // We also check if the db entry is a serialized
76 // boolean 'false' value (which we want to return).
77 if ($cached === serialize(false) || $value !== false) {
87 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
89 $cachekey = $this->getCacheKey($key);
91 // We store with the hostname as key to avoid problems with other applications
93 return $this->memcache->set(
100 return $this->memcache->set(
111 public function delete($key)
113 $cachekey = $this->getCacheKey($key);
114 return $this->memcache->delete($cachekey);
120 public function clear($outdated = true)
125 return $this->memcache->flush();
132 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
134 $cachekey = $this->getCacheKey($key);
135 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
141 public function getName()
143 return self::TYPE_MEMCACHE;