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 get($key)
62 $cachekey = $this->getCacheKey($key);
64 // We fetch with the hostname as key to avoid problems with other applications
65 $value = $this->memcached->get($cachekey);
67 if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
77 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
79 $cachekey = $this->getCacheKey($key);
81 // We store with the hostname as key to avoid problems with other applications
83 return $this->memcached->set(
89 return $this->memcached->set(
100 public function delete($key)
102 $cachekey = $this->getCacheKey($key);
103 return $this->memcached->delete($cachekey);
109 public function clear($outdated = true)
114 return $this->memcached->flush();
121 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
123 $cachekey = $this->getCacheKey($key);
124 return $this->memcached->add($cachekey, $value, $ttl);