]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/ArrayCache.php
update PL translations THX waldis (#5590)
[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                 $this->cachedData = [];
57                 return true;
58         }
59
60         /**
61          * (@inheritdoc)
62          */
63         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
64         {
65                 if (isset($this->cachedData[$key])) {
66                         return false;
67                 } else {
68                         return $this->set($key, $value, $ttl);
69                 }
70         }
71
72         /**
73          * (@inheritdoc)
74          */
75         public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
76         {
77                 if ($this->get($key) === $oldValue) {
78                         return $this->set($key, $newValue);
79                 } else {
80                         return false;
81                 }
82         }
83 }