1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Support; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Cache; |
6
|
|
|
|
7
|
|
|
abstract class SetupCache |
8
|
|
|
{ |
9
|
|
|
protected string $cachePrefix = 'setup_cache_'; |
10
|
|
|
protected int $cacheDuration = 60; // minutes |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generate a cache key for the given identifier. |
14
|
|
|
*/ |
15
|
|
|
protected function generateCacheKey($identifier): string |
16
|
|
|
{ |
17
|
|
|
return $this->cachePrefix.$identifier; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Store data in the cache. |
22
|
|
|
*/ |
23
|
|
|
public function store($identifier, ...$args) |
24
|
|
|
{ |
25
|
|
|
$cacheKey = $this->generateCacheKey($identifier); |
26
|
|
|
$data = $this->prepareDataForStorage(...$args); |
27
|
|
|
|
28
|
|
|
if ($data !== false && $data !== null) { |
29
|
|
|
Cache::forget($cacheKey); |
30
|
|
|
Cache::put($cacheKey, $data, now()->addMinutes($this->cacheDuration)); |
31
|
|
|
|
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Apply cached data. |
40
|
|
|
*/ |
41
|
|
|
public function apply($identifier, ...$args) |
42
|
|
|
{ |
43
|
|
|
$cacheKey = $this->generateCacheKey($identifier); |
44
|
|
|
$cachedData = Cache::get($cacheKey); |
45
|
|
|
|
46
|
|
|
if (! $cachedData) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->applyFromCache($cachedData, ...$args); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get cached data without applying it. |
55
|
|
|
*/ |
56
|
|
|
public function get($identifier) |
57
|
|
|
{ |
58
|
|
|
$cacheKey = $this->generateCacheKey($identifier); |
59
|
|
|
|
60
|
|
|
return Cache::get($cacheKey); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if cache exists for the given identifier. |
65
|
|
|
*/ |
66
|
|
|
public function has($identifier): bool |
67
|
|
|
{ |
68
|
|
|
$cacheKey = $this->generateCacheKey($identifier); |
69
|
|
|
|
70
|
|
|
return Cache::has($cacheKey); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Remove cached data. |
75
|
|
|
*/ |
76
|
|
|
public function forget($identifier): bool |
77
|
|
|
{ |
78
|
|
|
$cacheKey = $this->generateCacheKey($identifier); |
79
|
|
|
|
80
|
|
|
return Cache::forget($cacheKey); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the cache prefix. |
85
|
|
|
*/ |
86
|
|
|
public function setCachePrefix(string $prefix): self |
87
|
|
|
{ |
88
|
|
|
$this->cachePrefix = $prefix; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the cache duration in minutes. |
95
|
|
|
*/ |
96
|
|
|
public function setCacheDuration(int $minutes): self |
97
|
|
|
{ |
98
|
|
|
$this->cacheDuration = $minutes; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Prepare data for storage in the cache. |
105
|
|
|
* This method should be implemented by child classes. |
106
|
|
|
*/ |
107
|
|
|
abstract protected function prepareDataForStorage(...$args); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Apply data from the cache. |
111
|
|
|
* This method should be implemented by child classes. |
112
|
|
|
*/ |
113
|
|
|
abstract protected function applyFromCache($cachedData, ...$args); |
114
|
|
|
} |
115
|
|
|
|