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