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