]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/ArrayCache.php
Merge pull request #6942 from annando/worker-fast-commands
[friendica.git] / src / Core / Cache / ArrayCache.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5
6 use Friendica\Core\Cache;
7
8 /**
9  * Implementation of the IMemoryCacheDriver mainly for testing purpose
10  *
11  * Class ArrayCache
12  *
13  * @package Friendica\Core\Cache
14  */
15 class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
16 {
17         use TraitCompareDelete;
18
19         /** @var array Array with the cached data */
20         protected $cachedData = array();
21
22         /**
23          * (@inheritdoc)
24          */
25         public function getAllKeys($prefix = null)
26         {
27                 return $this->filterArrayKeysByPrefix($this->cachedData, $prefix);
28         }
29
30         /**
31          * (@inheritdoc)
32          */
33         public function get($key)
34         {
35                 if (isset($this->cachedData[$key])) {
36                         return $this->cachedData[$key];
37                 }
38                 return null;
39         }
40
41         /**
42          * (@inheritdoc)
43          */
44         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
45         {
46                 $this->cachedData[$key] = $value;
47                 return true;
48         }
49
50         /**
51          * (@inheritdoc)
52          */
53         public function delete($key)
54         {
55                 unset($this->cachedData[$key]);
56                 return true;
57         }
58
59         /**
60          * (@inheritdoc)
61          */
62         public function clear($outdated = true)
63         {
64                 // Array doesn't support TTL so just don't delete something
65                 if ($outdated) {
66                         return true;
67                 }
68
69                 $this->cachedData = [];
70                 return true;
71         }
72
73         /**
74          * (@inheritdoc)
75          */
76         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
77         {
78                 if (isset($this->cachedData[$key])) {
79                         return false;
80                 } else {
81                         return $this->set($key, $value, $ttl);
82                 }
83         }
84
85         /**
86          * (@inheritdoc)
87          */
88         public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
89         {
90                 if ($this->get($key) === $oldValue) {
91                         return $this->set($key, $newValue);
92                 } else {
93                         return false;
94                 }
95         }
96 }