1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Caching\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use Sfneal\Helpers\Redis\RedisCache; |
8
|
|
|
|
9
|
|
|
trait Cacheable |
10
|
|
|
{ |
11
|
|
|
use IsCacheable; |
12
|
|
|
|
13
|
|
|
// todo: add method for overwriting cache |
14
|
|
|
// todo: add get ttl method for seeing how long before expiration |
15
|
|
|
// todo: create new trait that replaces execute with `get()`? |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int Time to live |
19
|
|
|
*/ |
20
|
|
|
public $ttl = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Retrieve the cached data. |
24
|
|
|
* |
25
|
|
|
* @return Collection|int|mixed |
26
|
|
|
*/ |
27
|
|
|
abstract public function execute(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Fetch cached Query results. |
31
|
|
|
* |
32
|
|
|
* - use cacheKey() method to retrieve correct key to use for storing in cache |
33
|
|
|
* - use base class's execute() method for retrieving query results |
34
|
|
|
* |
35
|
|
|
* @param int|null $ttl |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function fetch(int $ttl = null) |
39
|
|
|
{ |
40
|
|
|
return Cache::remember($this->cacheKey(), $this->getTTL($ttl), function () { |
41
|
|
|
return $this->execute(); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Retrieve the time to live for the cached values |
47
|
|
|
* - 1. passed $ttl parameter |
48
|
|
|
* - 2. initialized $this->ttl property |
49
|
|
|
* - 3. application default cache ttl. |
50
|
|
|
* |
51
|
|
|
* @param int|null $ttl |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
private function getTTL(int $ttl = null): int |
55
|
|
|
{ |
56
|
|
|
return intval($ttl ?? $this->ttl ?? config('redis-helpers.ttl')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Invalidate the Query Cache for this Query. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function invalidateCache(): array |
65
|
|
|
{ |
66
|
|
|
return array_merge( |
67
|
|
|
[$this->cacheKey() => Cache::forget($this->cacheKey())], |
68
|
|
|
RedisCache::delete($this->cacheKeyPrefix()) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieve the cache key prefix by removing the trailing 'id' portion of the key. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function cacheKeyPrefix(): string |
78
|
|
|
{ |
79
|
|
|
// Explode the cache key into an array split by a colon |
80
|
|
|
$pieces = explode(':', $this->cacheKey()); |
81
|
|
|
|
82
|
|
|
// Only remove ID suffix if the cache key contains multiple segments |
83
|
|
|
if (count($pieces) == 1) { |
84
|
|
|
return $this->cacheKey(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Isolate the 'ID' portion of the cache (last segment) |
88
|
|
|
$id = array_reverse($pieces)[0]; |
89
|
|
|
|
90
|
|
|
// Remove the ID from the cache key to retrieve the prefix |
91
|
|
|
return str_replace($id, '', $this->cacheKey()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|