3 namespace Friendica\Core\Cache;
5 use Friendica\BaseObject;
6 use Friendica\Core\Cache;
9 * Memcache Cache Driver
11 * @author Hypolite Petovan <mrpetovan@gmail.com>
13 class MemcacheCacheDriver extends BaseObject implements IMemoryCacheDriver
16 use TraitCompareDelete;
23 public function __construct($memcache_host, $memcache_port)
25 if (!class_exists('Memcache', false)) {
26 throw new \Exception('Memcache class isn\'t available');
29 $this->memcache = new \Memcache();
31 if (!$this->memcache->connect($memcache_host, $memcache_port)) {
32 throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
39 public function get($key)
43 // We fetch with the hostname as key to avoid problems with other applications
44 $cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key);
46 // @see http://php.net/manual/en/memcache.get.php#84275
47 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
51 $value = @unserialize($cached);
53 // Only return a value if the serialized value is valid.
54 // We also check if the db entry is a serialized
55 // boolean 'false' value (which we want to return).
56 if ($cached === serialize(false) || $value !== false) {
66 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
68 // We store with the hostname as key to avoid problems with other applications
70 return $this->memcache->set(
71 self::getApp()->get_hostname() . ":" . $key,
77 return $this->memcache->set(
78 self::getApp()->get_hostname() . ":" . $key,
88 public function delete($key)
90 return $this->memcache->delete($key);
96 public function clear()
98 return $this->memcache->flush();
104 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
106 return $this->memcache->add(self::getApp()->get_hostname() . ":" . $key, $value, $ttl);