1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\Cache\Psr16; |
4
|
|
|
|
5
|
|
|
use Psr\Log\NullLogger; |
6
|
|
|
use Psr\SimpleCache\DateInterval; |
7
|
|
|
|
8
|
|
|
class ArrayCacheEngine extends BaseCacheEngine |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected $cache = array(); |
12
|
|
|
|
13
|
|
|
protected $logger = null; |
14
|
|
|
|
15
|
|
|
public function __construct($logger = null) |
16
|
|
|
{ |
17
|
|
|
$this->logger = $logger; |
18
|
|
|
if (is_null($logger)) { |
19
|
|
|
$this->logger = new NullLogger(); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Determines whether an item is present in the cache. |
25
|
|
|
* NOTE: It is recommended that has() is only to be used for cache warming type purposes |
26
|
|
|
* and not to be used within your live applications operations for get/set, as this method |
27
|
|
|
* is subject to a race condition where your has() will return true and immediately after, |
28
|
|
|
* another script can remove it making the state of your app out of date. |
29
|
|
|
* |
30
|
|
|
* @param string $key The cache item key. |
31
|
|
|
* @return bool |
32
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
33
|
|
|
* MUST be thrown if the $key string is not a legal value. |
34
|
|
|
*/ |
35
|
|
|
public function has($key) |
36
|
|
|
{ |
37
|
|
|
if (isset($this->cache[$key])) { |
38
|
|
|
if (isset($this->cache["$key.ttl"]) && time() >= $this->cache["$key.ttl"]) { |
39
|
|
|
$this->delete($key); |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $key The object KEY |
51
|
|
|
* @param mixed $default IGNORED IN MEMCACHED. |
52
|
|
|
* @return mixed Description |
53
|
|
|
*/ |
54
|
|
|
public function get($key, $default = null) |
55
|
|
|
{ |
56
|
|
|
if ($this->has($key)) { |
57
|
|
|
$this->logger->info("[Array cache] Get '$key' from L1 Cache"); |
58
|
|
|
return $this->cache[$key]; |
59
|
|
|
} else { |
60
|
|
|
$this->logger->info("[Array cache] Not found '$key'"); |
61
|
|
|
return $default; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
67
|
|
|
* |
68
|
|
|
* @param string $key The key of the item to store. |
69
|
|
|
* @param mixed $value The value of the item to store, must be serializable. |
70
|
|
|
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
71
|
|
|
* the driver supports TTL then the library may set a default value |
72
|
|
|
* for it or let the driver take care of that. |
73
|
|
|
* |
74
|
|
|
* @return bool True on success and false on failure. |
75
|
|
|
* |
76
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
77
|
|
|
* MUST be thrown if the $key string is not a legal value. |
78
|
|
|
*/ |
79
|
|
|
public function set($key, $value, $ttl = null) |
80
|
|
|
{ |
81
|
|
|
$this->logger->info("[Array cache] Set '$key' in L1 Cache"); |
82
|
|
|
|
83
|
|
|
$this->cache[$key] = $value; |
84
|
|
|
if (!empty($ttl)) { |
85
|
|
|
$this->cache["$key.ttl"] = $this->addToNow($ttl); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function clear() |
92
|
|
|
{ |
93
|
|
|
$this->cache = []; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Unlock resource |
98
|
|
|
* |
99
|
|
|
* @param string $key |
100
|
|
|
* @return bool|void |
101
|
|
|
*/ |
102
|
|
|
public function delete($key) |
103
|
|
|
{ |
104
|
|
|
unset($this->cache[$key]); |
105
|
|
|
unset($this->cache["$key.ttl"]); |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function isAvailable() |
110
|
|
|
{ |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|