3 namespace Friendica\Core\Cache;
5 use Friendica\Core\Cache;
11 * Memcached Cache Driver
13 * @author Hypolite Petovan <hypolite@mrpetovan.com>
15 class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
18 use TraitCompareDelete;
26 * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
28 * 0 => "hostname, port(, weight)",
32 * @param array $memcached_hosts
35 public function __construct(array $memcached_hosts)
37 if (!class_exists('Memcached', false)) {
38 throw new Exception('Memcached class isn\'t available');
41 $this->memcached = new Memcached();
43 array_walk($memcached_hosts, function (&$value) {
44 if (is_string($value)) {
45 $value = array_map('trim', explode(',', $value));
49 $this->memcached->addServers($memcached_hosts);
51 if (count($this->memcached->getServerList()) == 0) {
52 throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
59 public function getAllKeys()
61 return $this->memcached->getAllKeys();
67 public function get($key)
70 $cachekey = $this->getCacheKey($key);
72 // We fetch with the hostname as key to avoid problems with other applications
73 $value = $this->memcached->get($cachekey);
75 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
85 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
87 $cachekey = $this->getCacheKey($key);
89 // We store with the hostname as key to avoid problems with other applications
91 return $this->memcached->set(
97 return $this->memcached->set(
108 public function delete($key)
110 $cachekey = $this->getCacheKey($key);
111 return $this->memcached->delete($cachekey);
117 public function clear($outdated = true)
122 return $this->memcached->flush();
129 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
131 $cachekey = $this->getCacheKey($key);
132 return $this->memcached->add($cachekey, $value, $ttl);