Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 2b07c1e

Browse files
committed
refactor: use cache store through property
1 parent 892b9cf commit 2b07c1e

File tree

1 file changed

+40
-51
lines changed

1 file changed

+40
-51
lines changed

src/TwinStore.php

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,17 @@ class TwinStore implements Store
1717
/**
1818
* The first cache.
1919
*
20-
* @var string
20+
* @var \Illuminate\Contracts\Cache\Repository
2121
*/
2222
protected $older;
2323

2424
/**
2525
* The second cache.
2626
*
27-
* @var string
27+
* @var \Illuminate\Contracts\Cache\Repository
2828
*/
2929
protected $younger;
3030

31-
/**
32-
* The twin cache.
33-
*
34-
* @var array
35-
*/
36-
protected $store;
37-
3831
/**
3932
* The synced cache ttl.
4033
*
@@ -49,9 +42,10 @@ class TwinStore implements Store
4942
*/
5043
public function __construct()
5144
{
52-
$this->setDriveName();
5345
$this->setPrefix();
5446
$this->setTwinTtl();
47+
$this->older = $this->older();
48+
$this->younger = $this->younger();
5549
}
5650

5751
/**
@@ -62,7 +56,7 @@ public function __construct()
6256
*/
6357
public function get($key)
6458
{
65-
return Cache::store($this->older)->get($key);
59+
return $this->older->get($key);
6660
}
6761

6862
/**
@@ -75,7 +69,7 @@ public function get($key)
7569
*/
7670
public function many(array $keys)
7771
{
78-
return Cache::store($this->older)->many($keys);
72+
return $this->older->many($keys);
7973
}
8074

8175
/**
@@ -88,7 +82,7 @@ public function many(array $keys)
8882
*/
8983
public function put($key, $value, $seconds)
9084
{
91-
return Cache::store($this->older)->put($key, $value, $seconds);
85+
return $this->older->put($key, $value, $seconds);
9286
}
9387

9488
/**
@@ -100,7 +94,7 @@ public function put($key, $value, $seconds)
10094
*/
10195
public function putMany(array $values, $seconds)
10296
{
103-
return Cache::store($this->older)->putMany($values, $seconds);
97+
return $this->older->putMany($values, $seconds);
10498
}
10599

106100
/**
@@ -112,7 +106,7 @@ public function putMany(array $values, $seconds)
112106
*/
113107
public function increment($key, $value = 1)
114108
{
115-
return Cache::store($this->older)->increment($key, $value);
109+
return $this->older->increment($key, $value);
116110
}
117111

118112
/**
@@ -124,7 +118,7 @@ public function increment($key, $value = 1)
124118
*/
125119
public function decrement($key, $value = 1)
126120
{
127-
return Cache::store($this->older)->decrement($key, $value);
121+
return $this->older->decrement($key, $value);
128122
}
129123

130124
/**
@@ -136,7 +130,7 @@ public function decrement($key, $value = 1)
136130
*/
137131
public function forever($key, $value)
138132
{
139-
return Cache::store($this->older)->forever($key, $value);
133+
return $this->older->forever($key, $value);
140134
}
141135

142136
/**
@@ -147,7 +141,7 @@ public function forever($key, $value)
147141
*/
148142
public function forget($key)
149143
{
150-
return Cache::store($this->older)->forget($key);
144+
return $this->older->forget($key);
151145
}
152146

153147
/**
@@ -157,7 +151,7 @@ public function forget($key)
157151
*/
158152
public function flush()
159153
{
160-
return Cache::store($this->older)->flush();
154+
return $this->older->flush();
161155
}
162156

163157
/**
@@ -192,28 +186,23 @@ public function getDriveName($name)
192186
}
193187

194188
/**
195-
* Set the store name of cache.
189+
* Get the older store.
196190
*
197-
* @return void
191+
* @return \Illuminate\Contracts\Cache\Repository
198192
*/
199-
protected function setDriveName()
193+
protected function older()
200194
{
201-
$this->older = $this->getDriveName('older');
202-
$this->younger = $this->getDriveName('younger');
203-
$this->store = [
204-
$this->older,
205-
$this->younger
206-
];
195+
return Cache::store($this->getDriveName('older'));
207196
}
208197

209198
/**
210-
* Get the store.
199+
* Get the older store.
211200
*
212-
* @return array
201+
* @return \Illuminate\Contracts\Cache\Repository
213202
*/
214-
public function getTwinStores()
203+
protected function younger()
215204
{
216-
return $this->store;
205+
return Cache::store($this->getDriveName('younger'));
217206
}
218207

219208
/**
@@ -246,7 +235,7 @@ protected function setTwinTtl()
246235
*/
247236
protected function syncTwin($key, $value, $seconds = null)
248237
{
249-
return Cache::store($this->older)->put($key, $value, $seconds ?? $this->getTwinTtl());
238+
return $this->older->put($key, $value, $seconds ?? $this->getTwinTtl());
250239
}
251240

252241
/**
@@ -257,10 +246,10 @@ protected function syncTwin($key, $value, $seconds = null)
257246
*/
258247
public function getTwin($key)
259248
{
260-
if (Cache::store($this->older)->has($key)) {
261-
return Cache::store($this->older)->get($key);
262-
} elseif (Cache::store($this->younger)->has($key)) {
263-
$this->syncTwin($key, Cache::store($this->younger)->get($key));
249+
if ($this->older->has($key)) {
250+
return $this->older->get($key);
251+
} elseif ($this->younger->has($key)) {
252+
$this->syncTwin($key, $this->younger->get($key));
264253
}
265254
return $this->get($key);
266255
}
@@ -275,8 +264,8 @@ public function getTwin($key)
275264
*/
276265
public function putTwin($key, $value, $seconds = null)
277266
{
278-
return Cache::store($this->older)->put($key, $value, $seconds)
279-
&& Cache::store($this->younger)->put($key, $value, $seconds);
267+
return $this->older->put($key, $value, $seconds)
268+
&& $this->younger->put($key, $value, $seconds);
280269
}
281270

282271
/**
@@ -288,8 +277,8 @@ public function putTwin($key, $value, $seconds = null)
288277
*/
289278
public function putManyTwin(array $values, $seconds)
290279
{
291-
return Cache::store($this->older)->putMany($values, $seconds)
292-
&& Cache::store($this->younger)->putMany($values, $seconds);
280+
return $this->older->putMany($values, $seconds)
281+
&& $this->younger->putMany($values, $seconds);
293282
}
294283

295284
/**
@@ -301,8 +290,8 @@ public function putManyTwin(array $values, $seconds)
301290
*/
302291
public function incrementTwin($key, $value = 1)
303292
{
304-
return Cache::store($this->older)->increment($key, $value)
305-
&& Cache::store($this->younger)->increment($key, $value);
293+
return $this->older->increment($key, $value)
294+
&& $this->younger->increment($key, $value);
306295
}
307296

308297
/**
@@ -314,8 +303,8 @@ public function incrementTwin($key, $value = 1)
314303
*/
315304
public function decrementTwin($key, $value = 1)
316305
{
317-
return Cache::store($this->older)->decrement($key, $value)
318-
&& Cache::store($this->younger)->decrement($key, $value);
306+
return $this->older->decrement($key, $value)
307+
&& $this->younger->decrement($key, $value);
319308
}
320309

321310
/**
@@ -327,8 +316,8 @@ public function decrementTwin($key, $value = 1)
327316
*/
328317
public function foreverTwin($key, $value)
329318
{
330-
return Cache::store($this->older)->forever($key, $value)
331-
&& Cache::store($this->younger)->forever($key, $value);
319+
return $this->older->forever($key, $value)
320+
&& $this->younger->forever($key, $value);
332321
}
333322

334323
/**
@@ -339,8 +328,8 @@ public function foreverTwin($key, $value)
339328
*/
340329
public function forgetTwin($key)
341330
{
342-
return Cache::store($this->older)->forget($key)
343-
&& Cache::store($this->younger)->forget($key);
331+
return $this->older->forget($key)
332+
&& $this->younger->forget($key);
344333
}
345334

346335
/**
@@ -350,7 +339,7 @@ public function forgetTwin($key)
350339
*/
351340
public function flushTwin()
352341
{
353-
return Cache::store($this->older)->flush()
354-
&& Cache::store($this->younger)->flush();
342+
return $this->older->flush()
343+
&& $this->younger->flush();
355344
}
356345
}

0 commit comments

Comments
 (0)