3 namespace Friendica\Core\Cache;
5 use Friendica\BaseObject;
6 use Friendica\Core\Cache;
9 * Memcached Cache Driver
11 * @author Hypolite Petovan <mrpetovan@gmail.com>
13 class MemcachedCacheDriver extends BaseObject implements ICacheDriver
20 public function __construct(array $memcached_hosts)
22 if (!class_exists('Memcached', false)) {
23 throw new \Exception('Memcached class isn\'t available');
26 $this->memcached = new \Memcached();
28 $this->memcached->addServers($memcached_hosts);
30 if (count($this->memcached->getServerList()) == 0) {
31 throw new \Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
35 public function get($key)
39 // We fetch with the hostname as key to avoid problems with other applications
40 $value = $this->memcached->get(self::getApp()->get_hostname() . ':' . $key);
42 if ($this->memcached->getResultCode() === \Memcached::RES_SUCCESS) {
49 public function set($key, $value, $duration = Cache::MONTH)
51 // We store with the hostname as key to avoid problems with other applications
52 return $this->memcached->set(
53 self::getApp()->get_hostname() . ":" . $key,
59 public function delete($key)
61 return $this->memcached->delete($key);
64 public function clear()