3 namespace Friendica\Core\Cache;
5 use Friendica\Core\Cache;
8 * Memcached Cache Driver
10 * @author Hypolite Petovan <mrpetovan@gmail.com>
12 class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
15 use TraitCompareDelete;
22 public function __construct(array $memcached_hosts)
24 if (!class_exists('Memcached', false)) {
25 throw new \Exception('Memcached class isn\'t available');
28 $this->memcached = new \Memcached();
30 $this->memcached->addServers($memcached_hosts);
32 if (count($this->memcached->getServerList()) == 0) {
33 throw new \Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
37 public function get($key)
40 $cachekey = $this->getCacheKey($key);
42 // We fetch with the hostname as key to avoid problems with other applications
43 $value = $this->memcached->get($cachekey);
45 if ($this->memcached->getResultCode() === \Memcached::RES_SUCCESS) {
52 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
54 $cachekey = $this->getCacheKey($key);
56 // We store with the hostname as key to avoid problems with other applications
58 return $this->memcached->set(
64 return $this->memcached->set(
72 public function delete($key)
74 $cachekey = $this->getCacheKey($key);
75 return $this->memcached->delete($cachekey);
78 public function clear($outdated = true)
83 return $this->memcached->flush();
88 * @brief Sets a value if it's not already stored
90 * @param string $key The cache key
91 * @param mixed $value The old value we know from the cache
92 * @param int $ttl The cache lifespan, must be one of the Cache constants
95 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
97 $cachekey = $this->getCacheKey($key);
98 return $this->memcached->add($cachekey, $value, $ttl);