]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/ArrayCache.php
Merge pull request #1 from friendica/develop
[friendica.git] / src / Core / Cache / ArrayCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Cache;
23
24 use Friendica\Core\BaseCache;
25
26 /**
27  * Implementation of the IMemoryCache mainly for testing purpose
28  */
29 class ArrayCache extends BaseCache implements IMemoryCache
30 {
31         use TraitCompareDelete;
32
33         /** @var array Array with the cached data */
34         protected $cachedData = array();
35
36         /**
37          * (@inheritdoc)
38          */
39         public function getAllKeys($prefix = null)
40         {
41                 return $this->filterArrayKeysByPrefix(array_keys($this->cachedData), $prefix);
42         }
43
44         /**
45          * (@inheritdoc)
46          */
47         public function get($key)
48         {
49                 if (isset($this->cachedData[$key])) {
50                         return $this->cachedData[$key];
51                 }
52                 return null;
53         }
54
55         /**
56          * (@inheritdoc)
57          */
58         public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
59         {
60                 $this->cachedData[$key] = $value;
61                 return true;
62         }
63
64         /**
65          * (@inheritdoc)
66          */
67         public function delete($key)
68         {
69                 unset($this->cachedData[$key]);
70                 return true;
71         }
72
73         /**
74          * (@inheritdoc)
75          */
76         public function clear($outdated = true)
77         {
78                 // Array doesn't support TTL so just don't delete something
79                 if ($outdated) {
80                         return true;
81                 }
82
83                 $this->cachedData = [];
84                 return true;
85         }
86
87         /**
88          * (@inheritdoc)
89          */
90         public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
91         {
92                 if (isset($this->cachedData[$key])) {
93                         return false;
94                 } else {
95                         return $this->set($key, $value, $ttl);
96                 }
97         }
98
99         /**
100          * (@inheritdoc)
101          */
102         public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
103         {
104                 if ($this->get($key) === $oldValue) {
105                         return $this->set($key, $newValue);
106                 } else {
107                         return false;
108                 }
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         public function getName()
115         {
116                 return Type::ARRAY;
117         }
118 }