]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Cache/AbstractCacheDriver.php
Merge pull request #7250 from MrPetovan/bug/6410-normalize-message-button
[friendica.git] / src / Core / Cache / AbstractCacheDriver.php
index 15b822dc3b519f8e0fac9412429991a1f06384f7..f238a7819cfc2a1d63181e9c78c11838f87ed4f6 100644 (file)
@@ -14,11 +14,71 @@ use Friendica\BaseObject;
 abstract class AbstractCacheDriver extends BaseObject
 {
        /**
-        * @param string $key   The original key
-        * @return string               The cache key used for the cache
+        * Returns the prefix (to avoid namespace conflicts)
+        *
+        * @return string
+        * @throws \Exception
         */
-       protected function getCacheKey($key) {
+       protected function getPrefix()
+       {
                // We fetch with the hostname as key to avoid problems with other applications
-               return self::getApp()->get_hostname() . ":" . $key;
+               return self::getApp()->getHostName();
+       }
+
+       /**
+        * @param string $key The original key
+        * @return string        The cache key used for the cache
+        * @throws \Exception
+        */
+       protected function getCacheKey($key)
+       {
+               return $this->getPrefix() . ":" . $key;
+       }
+
+       /**
+        * @param array $keys   A list of cached keys
+        * @return array        A list of original keys
+        */
+       protected function getOriginalKeys($keys)
+       {
+               if (empty($keys)) {
+                       return [];
+               } else {
+                       // Keys are prefixed with the node hostname, let's remove it
+                       array_walk($keys, function (&$value) {
+                               $value = preg_replace('/^' . self::getApp()->getHostName() . ':/', '', $value);
+                       });
+
+                       sort($keys);
+
+                       return $keys;
+               }
+       }
+
+       /**
+        * Filters the keys of an array with a given prefix
+        * Returns the filtered keys as an new array
+        *
+        * @param array $array The array, which should get filtered
+        * @param string|null $prefix The prefix (if null, all keys will get returned)
+        *
+        * @return array The filtered array with just the keys
+        */
+       protected function filterArrayKeysByPrefix($array, $prefix = null)
+       {
+               if (empty($prefix)) {
+                       return array_keys($array);
+               } else {
+                       $result = [];
+
+                       foreach (array_keys($array) as $key) {
+                               if (strpos($key, $prefix) === 0) {
+                                       array_push($result, $key);
+                               }
+                       }
+
+                       return $result;
+               }
+
        }
 }