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