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