|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Istyle\LaravelDualCache; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Cache\TaggableStore; |
|
7
|
|
|
use Illuminate\Contracts\Cache\Store; |
|
8
|
|
|
use Illuminate\Cache\RetrievesMultipleKeys; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class DualCacheStore |
|
12
|
|
|
*/ |
|
13
|
|
|
class DualCacheStore extends TaggableStore implements Store |
|
14
|
|
|
{ |
|
15
|
|
|
use RetrievesMultipleKeys; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Store */ |
|
18
|
|
|
protected $primaryStore; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Store */ |
|
21
|
|
|
protected $secondaryStore; |
|
22
|
|
|
|
|
23
|
|
|
/** @var DualCacheHandlerInterface */ |
|
24
|
|
|
protected $cacheHandler; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Store $primaryStore |
|
28
|
|
|
* @param Store $secondaryStore |
|
29
|
|
|
* @param DualCacheHandlerInterface $cacheHandler |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
Store $primaryStore, |
|
33
|
|
|
Store $secondaryStore, |
|
34
|
|
|
DualCacheHandlerInterface $cacheHandler |
|
35
|
|
|
) { |
|
36
|
|
|
$this->primaryStore = $primaryStore; |
|
37
|
|
|
$this->secondaryStore = $secondaryStore; |
|
38
|
|
|
$this->cacheHandler = $cacheHandler; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array|string $key |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
* @throws \Throwable |
|
46
|
|
|
*/ |
|
47
|
|
|
public function get($key) |
|
48
|
|
|
{ |
|
49
|
|
|
$primaryResult = $this->primaryStore->get($key); |
|
50
|
|
|
if (!is_null($primaryResult)) { |
|
51
|
|
|
return $primaryResult; |
|
52
|
|
|
} |
|
53
|
|
|
$secondaryResult = $this->secondaryStore->get($key); |
|
54
|
|
|
if (!is_null($secondaryResult)) { |
|
55
|
|
|
return $secondaryResult; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $key |
|
63
|
|
|
* @param mixed $value |
|
64
|
|
|
* @param float|int $minutes |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \Throwable |
|
67
|
|
|
*/ |
|
68
|
|
|
public function put($key, $value, $minutes) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->cacheHandler->handle(function () use ($key, $value, $minutes) { |
|
71
|
|
|
$this->primaryStore->put($key, $value, $minutes); |
|
72
|
|
|
$this->secondaryStore->put($key, $value, $minutes); |
|
73
|
|
|
}, function () use ($key) { |
|
74
|
|
|
$this->forget($key); |
|
75
|
|
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $key |
|
80
|
|
|
* @param int $value |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool|int|mixed |
|
83
|
|
|
* @throws \Throwable |
|
84
|
|
|
*/ |
|
85
|
|
|
public function increment($key, $value = 1) |
|
86
|
|
|
{ |
|
87
|
|
|
$previousValue = $this->get($key); |
|
88
|
|
|
|
|
89
|
|
|
return $this->cacheHandler->handle(function () use ($key, $value) { |
|
90
|
|
|
$primaryResult = $this->primaryStore->increment($key, $value); |
|
91
|
|
|
$secondaryResult = $this->secondaryStore->increment($key, $value); |
|
92
|
|
|
if (!is_null($primaryResult)) { |
|
93
|
|
|
return $primaryResult; |
|
94
|
|
|
} |
|
95
|
|
|
if (!is_null($secondaryResult)) { |
|
96
|
|
|
return $secondaryResult; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return null; |
|
100
|
|
|
}, function () use ($key, $previousValue) { |
|
101
|
|
|
if ($previousValue) { |
|
102
|
|
|
return $this->forever($key, $previousValue); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->forget($key); |
|
106
|
|
|
}); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $key |
|
111
|
|
|
* @param int $value |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool|int|mixed |
|
114
|
|
|
* @throws \Throwable |
|
115
|
|
|
*/ |
|
116
|
|
|
public function decrement($key, $value = 1) |
|
117
|
|
|
{ |
|
118
|
|
|
$previousValue = $this->get($key); |
|
119
|
|
|
|
|
120
|
|
|
return $this->cacheHandler->handle(function () use ($key, $value) { |
|
121
|
|
|
$primaryResult = $this->primaryStore->decrement($key, $value); |
|
122
|
|
|
$secondaryResult = $this->secondaryStore->decrement($key, $value); |
|
123
|
|
|
if (!is_null($primaryResult)) { |
|
124
|
|
|
return $primaryResult; |
|
125
|
|
|
} |
|
126
|
|
|
if (!is_null($secondaryResult)) { |
|
127
|
|
|
return $secondaryResult; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return null; |
|
131
|
|
|
}, function () use ($key, $previousValue) { |
|
132
|
|
|
if ($previousValue) { |
|
133
|
|
|
return $this->forever($key, $previousValue); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->forget($key); |
|
137
|
|
|
}); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $key |
|
142
|
|
|
* @param mixed $value |
|
143
|
|
|
* |
|
144
|
|
|
* @throws \Throwable |
|
145
|
|
|
*/ |
|
146
|
|
|
public function forever($key, $value) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->put($key, $value, 0); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $key |
|
153
|
|
|
* |
|
154
|
|
|
* @return bool|mixed |
|
155
|
|
|
* @throws \Throwable |
|
156
|
|
|
*/ |
|
157
|
|
|
public function forget($key) |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->cacheHandler->handle(function () use ($key) { |
|
160
|
|
|
$primaryResult = $this->primaryStore->forget($key); |
|
161
|
|
|
$secondaryResult = $this->secondaryStore->forget($key); |
|
162
|
|
|
if ($primaryResult) { |
|
163
|
|
|
return $primaryResult; |
|
164
|
|
|
} |
|
165
|
|
|
if ($secondaryResult) { |
|
166
|
|
|
return $secondaryResult; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return false; |
|
170
|
|
|
}); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return void |
|
175
|
|
|
* @throws \Throwable |
|
176
|
|
|
*/ |
|
177
|
|
|
public function flush() |
|
178
|
|
|
{ |
|
179
|
|
|
$this->cacheHandler->handle(function () { |
|
180
|
|
|
$this->primaryStore->flush(); |
|
181
|
|
|
$this->secondaryStore->flush(); |
|
182
|
|
|
}); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* {@inheritdoc} |
|
187
|
|
|
* @codeCoverageIgnore |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getPrefix(): string |
|
190
|
|
|
{ |
|
191
|
|
|
return ''; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|