]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/ArrayCache.php
Merge pull request #5762 from JonnyTischbein/2018.08-rc
[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 get($key)
26         {
27                 if (isset($this->cachedData[$key])) {
28                         return $this->cachedData[$key];
29                 }
30                 return null;
31         }
32
33         /**
34          * (@inheritdoc)
35          */
36         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
37         {
38                 $this->cachedData[$key] = $value;
39                 return true;
40         }
41
42         /**
43          * (@inheritdoc)
44          */
45         public function delete($key)
46         {
47                 unset($this->cachedData[$key]);
48                 return true;
49         }
50
51         /**
52          * (@inheritdoc)
53          */
54         public function clear($outdated = true)
55         {
56                 // Array doesn't support TTL so just don't delete something
57                 if ($outdated) {
58                         return true;
59                 }
60
61                 $this->cachedData = [];
62                 return true;
63         }
64
65         /**
66          * (@inheritdoc)
67          */
68         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
69         {
70                 if (isset($this->cachedData[$key])) {
71                         return false;
72                 } else {
73                         return $this->set($key, $value, $ttl);
74                 }
75         }
76
77         /**
78          * (@inheritdoc)
79          */
80         public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
81         {
82                 if ($this->get($key) === $oldValue) {
83                         return $this->set($key, $newValue);
84                 } else {
85                         return false;
86                 }
87         }
88 }