|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Cache\Facade; |
|
4
|
|
|
|
|
5
|
|
|
// From PSR-6 |
|
6
|
|
|
use Psr\Cache\CacheItemInterface; |
|
7
|
|
|
|
|
8
|
|
|
// From 'charcoal-cache' |
|
9
|
|
|
use Charcoal\Cache\CacheConfig; |
|
10
|
|
|
use Charcoal\Cache\CachePoolAwareTrait; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Cache Pool Facade |
|
14
|
|
|
* |
|
15
|
|
|
* The facade provides a simpler interface to work with the cache pool. |
|
16
|
|
|
*/ |
|
17
|
|
|
class CachePoolFacade |
|
18
|
|
|
{ |
|
19
|
|
|
use CachePoolAwareTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Default maximum time an item will be cached. |
|
23
|
|
|
* |
|
24
|
|
|
* Either an integer, date interval, or date. |
|
25
|
|
|
* |
|
26
|
|
|
* @var mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
private $defaultTtl = CacheConfig::HOUR_IN_SECONDS; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a cache pool facade. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $data The facade dependencies. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(array $data) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->setCachePool($data['cache']); |
|
38
|
|
|
|
|
39
|
|
|
if (isset($data['default_ttl'])) { |
|
40
|
|
|
$this->setDefaultTtl($data['default_ttl']); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retrieve the value associated with the specified key from the pool. |
|
46
|
|
|
* |
|
47
|
|
|
* This method will call $lambada if the cache item representing $key resulted in a cache miss. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key The key for which to return the associated value. |
|
50
|
|
|
* @param callable|null $resolve The function to execute if the cached value does not exist |
|
51
|
|
|
* or is considered expired. The function must return a value which will be stored |
|
52
|
|
|
* in the cache before being returned by the method. |
|
53
|
|
|
* |
|
54
|
|
|
* ``` |
|
55
|
|
|
* $resolve ( mixed $data, CacheItemInterface $item ) : mixed |
|
56
|
|
|
* ``` |
|
57
|
|
|
* |
|
58
|
|
|
* The $resolve takes on two parameters: |
|
59
|
|
|
* |
|
60
|
|
|
* 1. The expired value or NULL if no value was stored. |
|
61
|
|
|
* 2. The cache item of the specified key. |
|
62
|
|
|
* @param mixed $ttl An integer, interval, date, or NULL to use the facade's default value. |
|
63
|
|
|
* @return mixed The value corresponding to this cache item's $key, or NULL if not found. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get($key, callable $resolve = null, $ttl = null) |
|
66
|
|
|
{ |
|
67
|
|
|
$pool = $this->cachePool(); |
|
68
|
|
|
$item = $pool->getItem($key); |
|
69
|
|
|
$data = $item->get(); |
|
70
|
|
|
|
|
71
|
|
|
if ($item->isHit()) { |
|
72
|
|
|
return $data; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (is_callable($resolve)) { |
|
76
|
|
|
$data = $resolve($data, $item); |
|
77
|
|
|
$this->save($item, $data, $ttl); |
|
78
|
|
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Determine if the specified key results in a cache hit. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $key The key for which to check existence. |
|
88
|
|
|
* @return boolean TRUE if item exists in the cache, FALSE otherwise. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function has($key) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->cachePool()->getItem($key)->isHit(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Store a value with the specified key to be saved immediately. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $key The key to save the value on. |
|
99
|
|
|
* @param mixed $value The serializable value to be stored. |
|
100
|
|
|
* @param mixed $ttl An integer, interval, date, or NULL to use the facade's default value. |
|
101
|
|
|
* @return boolean TRUE if the item was successfully persisted. FALSE if there was an error. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function set($key, $value, $ttl = null) |
|
104
|
|
|
{ |
|
105
|
|
|
$item = $this->cachePool()->getItem($key); |
|
106
|
|
|
|
|
107
|
|
|
return $this->save($item, $value, $ttl); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set a value on a cache item to be saved immediately. |
|
112
|
|
|
* |
|
113
|
|
|
* @param CacheItemInterface $item The cache item to save. |
|
114
|
|
|
* @param mixed $value The serializable value to be stored. |
|
115
|
|
|
* @param mixed $ttl An integer, interval, date, or NULL to use the facade's default value. |
|
116
|
|
|
* @return boolean TRUE if the item was successfully persisted. FALSE if there was an error. |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function save(CacheItemInterface $item, $value, $ttl = null) |
|
119
|
|
|
{ |
|
120
|
|
|
if ($ttl === null) { |
|
121
|
|
|
$ttl = $this->defaultTtl(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (is_numeric($ttl) || ($ttl instanceof \DateInterval)) { |
|
125
|
|
|
$item->expiresAfter($ttl); |
|
126
|
|
|
} elseif ($ttl instanceof \DateTimeInterface) { |
|
127
|
|
|
$item->expiresAt($ttl); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$item->set($value); |
|
131
|
|
|
|
|
132
|
|
|
return $this->cachePool()->save($item); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Removes one or more items from the pool. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string ...$keys One or many keys to delete. |
|
139
|
|
|
* @return bool TRUE if the item was successfully removed. FALSE if there was an error. |
|
140
|
|
|
*/ |
|
141
|
|
|
public function delete(...$keys) |
|
142
|
|
|
{ |
|
143
|
|
|
$pool = $this->cachePool(); |
|
144
|
|
|
|
|
145
|
|
|
$results = true; |
|
146
|
|
|
foreach ($keys as $key) { |
|
147
|
|
|
$results = $pool->deleteItem($key) && $results; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $results; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Retrieve the facade's default time-to-live for cached items. |
|
155
|
|
|
* |
|
156
|
|
|
* @return mixed An integer, date interval, or date. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function defaultTtl() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->defaultTtl; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Set the facade's default time-to-live for cached items. |
|
165
|
|
|
* |
|
166
|
|
|
* @param mixed $ttl An integer, date interval, or date. |
|
167
|
|
|
* @return void |
|
168
|
|
|
*/ |
|
169
|
|
|
public function setDefaultTtl($ttl) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->defaultTtl = $ttl; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|