1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codefocus\ManagedCache; |
4
|
|
|
|
5
|
|
|
use Illuminate\Cache\TaggedCache; |
6
|
|
|
use Illuminate\Contracts\Cache\Store as StoreContract; |
7
|
|
|
|
8
|
|
|
class DefinitionChain implements StoreContract |
9
|
|
|
{ |
10
|
|
|
protected $managedCache = null; |
11
|
|
|
|
12
|
|
|
protected $store = null; |
13
|
|
|
|
14
|
|
|
protected $conditions = []; |
15
|
|
|
|
16
|
|
|
protected $conditionTags = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor. |
20
|
|
|
* |
21
|
|
|
* @param ManagedCache $managedCache |
22
|
|
|
*/ |
23
|
|
|
public function __construct(ManagedCache $managedCache) |
24
|
|
|
{ |
25
|
|
|
$this->managedCache = $managedCache; |
26
|
|
|
$this->store = $managedCache->getStore(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Sets the array of Conditions that trigger the cache key to get flushed. |
31
|
|
|
* |
32
|
|
|
* @param array $conditions An array of Condition instances |
33
|
|
|
* |
34
|
|
|
* @return self |
35
|
|
|
*/ |
36
|
|
|
public function forgetWhen(array $conditions): self |
37
|
|
|
{ |
38
|
|
|
$this->conditions = $conditions; |
39
|
|
|
$this->conditionTags = null; |
40
|
|
|
|
41
|
|
|
// debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Adds a Condition that triggers the cache key to get flushed. |
48
|
|
|
* |
49
|
|
|
* @param Condition $condition |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
|
|
public function addCondition(Condition $condition): self |
54
|
|
|
{ |
55
|
|
|
$this->conditions[] = $condition; |
56
|
|
|
$this->conditionTags = null; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adds an array of Conditions that trigger the cache key to get flushed. |
63
|
|
|
* |
64
|
|
|
* @param array $conditions An array of Condition instances |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
|
|
public function addConditions(array $conditions): self |
69
|
|
|
{ |
70
|
|
|
$this->conditions += $conditions; |
71
|
|
|
$this->conditionTags = null; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns an array of Condition instances. |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getConditions(): array |
82
|
|
|
{ |
83
|
|
|
return $this->conditions; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return an array of cache tags generated from our Conditions. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getConditionTags(): array |
92
|
|
|
{ |
93
|
|
|
if (empty($this->conditionTags)) { |
94
|
|
|
$tags = []; |
95
|
|
|
foreach ($this->conditions as $condition) { |
96
|
|
|
$tags[] = (string) $condition; |
97
|
|
|
} |
98
|
|
|
// @TODO: Remove this. |
99
|
|
|
// Potentially replace with a call to ManagedCache::log() |
100
|
|
|
if ($this->managedCache->isDebugModeEnabled()) { |
101
|
|
|
dump($tags); |
102
|
|
|
} |
103
|
|
|
$this->conditionTags = $tags; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->conditionTags; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return the cache store, after applying our conditions to it, as tags. |
111
|
|
|
* |
112
|
|
|
* @param ?array $tags |
113
|
|
|
* |
114
|
|
|
* @return TaggedCache |
115
|
|
|
*/ |
|
|
|
|
116
|
|
|
public function getTaggedStore(?array $tags = null): TaggedCache |
117
|
|
|
{ |
118
|
|
|
if ($tags !== null) { |
119
|
|
|
return $this->store->tags($tags); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->store->tags($this->getConditionTags()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
|
|
public function get($key, $default = null) |
129
|
|
|
{ |
130
|
|
|
$value = $this->getTaggedStore($this->managedCache->getTagsForKey($key))->get($key); |
131
|
|
|
// If we could not find the cache value, we will get the default value |
132
|
|
|
// for this cache key. This default may be a callback. |
133
|
|
|
if (is_null($value)) { |
134
|
|
|
$value = value($default); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $value; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritdoc |
142
|
|
|
*/ |
143
|
|
|
public function many(array $keys) |
144
|
|
|
{ |
145
|
|
|
// @TODO: $this->managedCache->getTagsForKeys plural |
146
|
|
|
return $this->getTaggedStore()->many($keys); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritdoc |
151
|
|
|
*/ |
152
|
|
|
public function put($key, $value, $minutes) |
153
|
|
|
{ |
154
|
|
|
// Store the cache tags for this key, |
155
|
|
|
// so that we can GET it without specifying the tags. |
156
|
|
|
$this->managedCache->setTagsForKey($key, $this->getConditionTags()); |
157
|
|
|
|
158
|
|
|
return $this->getTaggedStore()->put($key, $value, $minutes); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
|
|
public function putMany(array $values, $minutes) |
165
|
|
|
{ |
166
|
|
|
// @TODO:Store tags for keys plural. |
167
|
|
|
return $this->getTaggedStore()->putMany($values, $minutes); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
|
|
public function increment($key, $value = 1) |
174
|
|
|
{ |
175
|
|
|
return $this->getTaggedStore()->increment($key, $value); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @inheritdoc |
180
|
|
|
*/ |
181
|
|
|
public function decrement($key, $value = 1) |
182
|
|
|
{ |
183
|
|
|
return $this->getTaggedStore()->decrement($key, $value); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritdoc |
188
|
|
|
*/ |
189
|
|
|
public function forever($key, $value) |
190
|
|
|
{ |
191
|
|
|
return $this->getTaggedStore()->forever($key, $value); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritdoc |
196
|
|
|
*/ |
197
|
|
|
public function forget($key) |
198
|
|
|
{ |
199
|
|
|
$this->managedCache->deleteTagsForKey($key); |
200
|
|
|
|
201
|
|
|
return $this->getTaggedStore()->forget($key); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @inheritdoc |
206
|
|
|
*/ |
207
|
|
|
public function flush() |
208
|
|
|
{ |
209
|
|
|
return $this->getTaggedStore()->flush(); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritdoc |
214
|
|
|
*/ |
215
|
|
|
public function getPrefix() |
216
|
|
|
{ |
217
|
|
|
return $this->getTaggedStore()->getPrefix(); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|