3 namespace Friendica\Core\Cache;
5 use Friendica\Core\Cache;
11 * Memcached Cache Driver
13 * @author Hypolite Petovan <mrpetovan@gmail.com>
15 class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
18 use TraitCompareDelete;
25 public function __construct(array $memcached_hosts)
27 if (!class_exists('Memcached', false)) {
28 throw new Exception('Memcached class isn\'t available');
31 $this->memcached = new Memcached();
33 $this->memcached->addServers($memcached_hosts);
35 if (count($this->memcached->getServerList()) == 0) {
36 throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
40 public function get($key)
43 $cachekey = $this->getCacheKey($key);
45 // We fetch with the hostname as key to avoid problems with other applications
46 $value = $this->memcached->get($cachekey);
48 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
55 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
57 $cachekey = $this->getCacheKey($key);
59 // We store with the hostname as key to avoid problems with other applications
61 return $this->memcached->set(
67 return $this->memcached->set(
75 public function delete($key)
77 $cachekey = $this->getCacheKey($key);
78 return $this->memcached->delete($cachekey);
81 public function clear($outdated = true)
86 return $this->memcached->flush();
91 * @brief Sets a value if it's not already stored
93 * @param string $key The cache key
94 * @param mixed $value The old value we know from the cache
95 * @param int $ttl The cache lifespan, must be one of the Cache constants
98 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
100 $cachekey = $this->getCacheKey($key);
101 return $this->memcached->add($cachekey, $value, $ttl);