X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FCache%2FMemcacheCacheDriver.php;h=bff543b542478ed4db0f80f9e7488f49cae758ed;hb=1e96faca4c9825bf6a8d8a50dc37498b764b9a3d;hp=563447ef1e438b0a193399bbdd3f348766599fac;hpb=7acb4b04343df31c2cc78214fae5429c66d95fb2;p=friendica.git diff --git a/src/Core/Cache/MemcacheCacheDriver.php b/src/Core/Cache/MemcacheCacheDriver.php index 563447ef1e..bff543b542 100644 --- a/src/Core/Cache/MemcacheCacheDriver.php +++ b/src/Core/Cache/MemcacheCacheDriver.php @@ -1,77 +1,116 @@ - - */ -class MemcacheCacheDriver extends BaseObject implements ICacheDriver -{ - /** - * @var Memcache - */ - private $memcache; - - public function __construct($memcache_host, $memcache_port) - { - if (!class_exists('Memcache', false)) { - throw new \Exception('Memcache class isn\'t available'); - } - - $this->memcache = new \Memcache(); - - if (!$this->memcache->connect($memcache_host, $memcache_port)) { - throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available'); - } - } - - public function get($key) - { - $return = null; - - // We fetch with the hostname as key to avoid problems with other applications - $cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key); - - // @see http://php.net/manual/en/memcache.get.php#84275 - if (is_bool($cached) || is_double($cached) || is_long($cached)) { - return $return; - } - - $value = @unserialize($cached); - - // Only return a value if the serialized value is valid. - // We also check if the db entry is a serialized - // boolean 'false' value (which we want to return). - if ($cached === serialize(false) || $value !== false) { - $return = $value; - } - - return $return; - } - - public function set($key, $value, $duration = Cache::MONTH) - { - // We store with the hostname as key to avoid problems with other applications - return $this->memcache->set( - self::getApp()->get_hostname() . ":" . $key, - serialize($value), - MEMCACHE_COMPRESSED, - time() + $duration - ); - } - - public function delete($key) - { - return $this->memcache->delete($key); - } - - public function clear() - { - return true; - } -} + + */ +class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver +{ + use TraitCompareSet; + use TraitCompareDelete; + + /** + * @var \Memcache + */ + private $memcache; + + public function __construct($memcache_host, $memcache_port) + { + if (!class_exists('Memcache', false)) { + throw new \Exception('Memcache class isn\'t available'); + } + + $this->memcache = new \Memcache(); + + if (!$this->memcache->connect($memcache_host, $memcache_port)) { + throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available'); + } + } + + /** + * (@inheritdoc) + */ + public function get($key) + { + $return = null; + $cachekey = $this->getCacheKey($key); + + // We fetch with the hostname as key to avoid problems with other applications + $cached = $this->memcache->get($cachekey); + + // @see http://php.net/manual/en/memcache.get.php#84275 + if (is_bool($cached) || is_double($cached) || is_long($cached)) { + return $return; + } + + $value = @unserialize($cached); + + // Only return a value if the serialized value is valid. + // We also check if the db entry is a serialized + // boolean 'false' value (which we want to return). + if ($cached === serialize(false) || $value !== false) { + $return = $value; + } + + return $return; + } + + /** + * (@inheritdoc) + */ + public function set($key, $value, $ttl = Cache::FIVE_MINUTES) + { + $cachekey = $this->getCacheKey($key); + + // We store with the hostname as key to avoid problems with other applications + if ($ttl > 0) { + return $this->memcache->set( + $cachekey, + serialize($value), + MEMCACHE_COMPRESSED, + time() + $ttl + ); + } else { + return $this->memcache->set( + $cachekey, + serialize($value), + MEMCACHE_COMPRESSED + ); + } + } + + /** + * (@inheritdoc) + */ + public function delete($key) + { + $cachekey = $this->getCacheKey($key); + return $this->memcache->delete($cachekey); + } + + /** + * (@inheritdoc) + */ + public function clear($outdated = true) + { + if ($outdated) { + return true; + } else { + return $this->memcache->flush(); + } + } + + /** + * (@inheritdoc) + */ + public function add($key, $value, $ttl = Cache::FIVE_MINUTES) + { + $cachekey = $this->getCacheKey($key); + return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl); + } +}