3 namespace Friendica\Core\Cache;
6 use Friendica\Core\BaseCache;
7 use Friendica\Core\Config\IConfiguration;
13 * @author Hypolite Petovan <hypolite@mrpetovan.com>
15 class MemcacheCache extends BaseCache implements IMemoryCache
18 use TraitCompareDelete;
19 use TraitMemcacheCommand;
29 public function __construct(string $hostname, IConfiguration $config)
31 if (!class_exists('Memcache', false)) {
32 throw new Exception('Memcache class isn\'t available');
35 parent::__construct($hostname);
37 $this->memcache = new Memcache();
39 $this->server = $config->get('system', 'memcache_host');;
40 $this->port = $config->get('system', 'memcache_port');
42 if (!@$this->memcache->connect($this->server, $this->port)) {
43 throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
50 public function getAllKeys($prefix = null)
52 $keys = $this->getOriginalKeys($this->getMemcacheKeys());
54 return $this->filterArrayKeysByPrefix($keys, $prefix);
60 public function get($key)
63 $cachekey = $this->getCacheKey($key);
65 // We fetch with the hostname as key to avoid problems with other applications
66 $cached = $this->memcache->get($cachekey);
68 // @see http://php.net/manual/en/memcache.get.php#84275
69 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
73 $value = @unserialize($cached);
75 // Only return a value if the serialized value is valid.
76 // We also check if the db entry is a serialized
77 // boolean 'false' value (which we want to return).
78 if ($cached === serialize(false) || $value !== false) {
88 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
90 $cachekey = $this->getCacheKey($key);
92 // We store with the hostname as key to avoid problems with other applications
94 return $this->memcache->set(
101 return $this->memcache->set(
112 public function delete($key)
114 $cachekey = $this->getCacheKey($key);
115 return $this->memcache->delete($cachekey);
121 public function clear($outdated = true)
126 return $this->memcache->flush();
133 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
135 $cachekey = $this->getCacheKey($key);
136 return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
142 public function getName()
144 return Type::MEMCACHE;