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