]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Cache/DatabaseCacheDriver.php
Merge pull request #7255 from annando/issue-7223
[friendica.git] / src / Core / Cache / DatabaseCacheDriver.php
index ca4842a4686db98247fa41fb19bf06da3ffa11de..f6f5b6486cb6b7c3d02d4af991a84cc955758da5 100644 (file)
@@ -13,9 +13,34 @@ use Friendica\Util\DateTimeFormat;
  */
 class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
 {
+       /**
+        * (@inheritdoc)
+        */
+       public function getAllKeys($prefix = null)
+       {
+               if (empty($prefix)) {
+                       $where = ['`expires` >= ?', DateTimeFormat::utcNow()];
+               } else {
+                       $where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
+               }
+
+               $stmt = DBA::select('cache', ['k'], $where);
+
+               $keys = [];
+               while ($key = DBA::fetch($stmt)) {
+                       array_push($keys, $key['k']);
+               }
+               DBA::close($stmt);
+
+               return $keys;
+       }
+
+       /**
+        * (@inheritdoc)
+        */
        public function get($key)
        {
-               $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
+               $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
 
                if (DBA::isResult($cache)) {
                        $cached = $cache['v'];
@@ -32,22 +57,39 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
                return null;
        }
 
+       /**
+        * (@inheritdoc)
+        */
        public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
        {
-               $fields = [
-                       'v'       => serialize($value),
-                       'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
-                       'updated' => DateTimeFormat::utcNow()
-               ];
+               if ($ttl > 0) {
+                       $fields = [
+                               'v' => serialize($value),
+                               'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
+                               'updated' => DateTimeFormat::utcNow()
+                       ];
+               } else {
+                       $fields = [
+                               'v' => serialize($value),
+                               'expires' => -1,
+                               'updated' => DateTimeFormat::utcNow()
+                       ];
+               }
 
                return DBA::update('cache', $fields, ['k' => $key], true);
        }
 
+       /**
+        * (@inheritdoc)
+        */
        public function delete($key)
        {
                return DBA::delete('cache', ['k' => $key]);
        }
 
+       /**
+        * (@inheritdoc)
+        */
        public function clear($outdated = true)
        {
                if ($outdated) {