]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Cache/MemcacheCacheDriver.php
Merge pull request #7255 from annando/issue-7223
[friendica.git] / src / Core / Cache / MemcacheCacheDriver.php
index 563447ef1e438b0a193399bbdd3f348766599fac..fd928c6fcc15ed2a5245f2bca27c664a6f696531 100644 (file)
-<?php\r
-\r
-namespace Friendica\Core\Cache;\r
-\r
-use Friendica\BaseObject;\r
-use Friendica\Core\Cache;\r
-\r
-/**\r
- * Memcache Cache Driver\r
- *\r
- * @author Hypolite Petovan <mrpetovan@gmail.com>\r
- */\r
-class MemcacheCacheDriver extends BaseObject implements ICacheDriver\r
-{\r
-       /**\r
-        * @var Memcache\r
-        */\r
-       private $memcache;\r
-\r
-       public function __construct($memcache_host, $memcache_port)\r
-       {\r
-               if (!class_exists('Memcache', false)) {\r
-                       throw new \Exception('Memcache class isn\'t available');\r
-               }\r
-\r
-               $this->memcache = new \Memcache();\r
-\r
-               if (!$this->memcache->connect($memcache_host, $memcache_port)) {\r
-                       throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');\r
-               }\r
-       }\r
-\r
-       public function get($key)\r
-       {\r
-               $return = null;\r
-\r
-               // We fetch with the hostname as key to avoid problems with other applications\r
-               $cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key);\r
-\r
-               // @see http://php.net/manual/en/memcache.get.php#84275\r
-               if (is_bool($cached) || is_double($cached) || is_long($cached)) {\r
-                       return $return;\r
-               }\r
-\r
-               $value = @unserialize($cached);\r
-\r
-               // Only return a value if the serialized value is valid.\r
-               // We also check if the db entry is a serialized\r
-               // boolean 'false' value (which we want to return).\r
-               if ($cached === serialize(false) || $value !== false) {\r
-                       $return = $value;\r
-               }\r
-\r
-               return $return;\r
-       }\r
-\r
-       public function set($key, $value, $duration = Cache::MONTH)\r
-       {\r
-               // We store with the hostname as key to avoid problems with other applications\r
-               return $this->memcache->set(\r
-                       self::getApp()->get_hostname() . ":" . $key,\r
-                       serialize($value),\r
-                       MEMCACHE_COMPRESSED,\r
-                       time() + $duration\r
-               );\r
-       }\r
-\r
-       public function delete($key)\r
-       {\r
-               return $this->memcache->delete($key);\r
-       }\r
-\r
-       public function clear()\r
-       {\r
-               return true;\r
-       }\r
-}\r
+<?php
+
+namespace Friendica\Core\Cache;
+
+use Friendica\Core\Cache;
+
+use Exception;
+use Memcache;
+
+/**
+ * Memcache Cache Driver
+ *
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
+ */
+class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
+{
+       use TraitCompareSet;
+       use TraitCompareDelete;
+
+       /**
+        * @var Memcache
+        */
+       private $memcache;
+
+       /**
+        * @param string $memcache_host
+        * @param int    $memcache_port
+        * @throws Exception
+        */
+       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 getAllKeys($prefix = null)
+       {
+               $keys = [];
+               $allSlabs = $this->memcache->getExtendedStats('slabs');
+               foreach ($allSlabs as $slabs) {
+                       foreach (array_keys($slabs) as $slabId) {
+                               $cachedump = $this->memcache->getExtendedStats('cachedump', (int)$slabId);
+                               foreach ($cachedump as $key => $arrVal) {
+                                       if (!is_array($arrVal)) {
+                                               continue;
+                                       }
+                                       $keys = array_merge($keys, array_keys($arrVal));
+                               }
+                       }
+               }
+
+               $keys = $this->getOriginalKeys($keys);
+
+               return $this->filterArrayKeysByPrefix($keys, $prefix);
+       }
+
+       /**
+        * (@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);
+       }
+}