1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Suitmedia\Cacheable; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use ErrorException; |
7
|
|
|
use Illuminate\Cache\ArrayStore; |
8
|
|
|
use Illuminate\Cache\CacheManager; |
9
|
|
|
use Illuminate\Cache\TaggableStore; |
10
|
|
|
use Illuminate\Contracts\Cache\Store; |
11
|
|
|
use Suitmedia\Cacheable\Contracts\CacheableRepository; |
12
|
|
|
|
13
|
|
|
class CacheableService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Cache manager object. |
17
|
|
|
* |
18
|
|
|
* @var \Illuminate\Contracts\Cache\Store |
19
|
|
|
*/ |
20
|
|
|
protected $cache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Runtime cache. |
24
|
|
|
* |
25
|
|
|
* @var \Illuminate\Cache\ArrayStore |
26
|
|
|
*/ |
27
|
|
|
protected $runtimeCache; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class constructor. |
31
|
|
|
* |
32
|
|
|
* @param \Illuminate\Cache\CacheManager $cache |
33
|
|
|
* @param \Illuminate\Cache\ArrayStore $runtimeCache |
34
|
|
|
*/ |
35
|
|
|
public function __construct(CacheManager $cache, ArrayStore $runtimeCache) |
36
|
|
|
{ |
37
|
|
|
$this->cache = $cache->store()->getStore(); |
38
|
|
|
$this->runtimeCache = $runtimeCache; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Just an alias of wrap() method. |
43
|
|
|
* |
44
|
|
|
* @param mixed $repository |
45
|
|
|
* |
46
|
|
|
* @return \Suitmedia\Cacheable\CacheableDecorator |
47
|
|
|
*/ |
48
|
|
|
public function build($repository): CacheableDecorator |
49
|
|
|
{ |
50
|
|
|
return $this->wrap($repository); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Flush cache. |
55
|
|
|
* |
56
|
|
|
* @param mixed $tags |
57
|
|
|
* |
58
|
|
|
* @throws ErrorException |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function flush($tags = null): void |
63
|
|
|
{ |
64
|
|
|
$this->taggedCache($this->cache, $tags)->flush(); |
65
|
|
|
$this->taggedCache($this->runtimeCache, $tags)->flush(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get runtime cached object. |
70
|
|
|
* |
71
|
|
|
* @param mixed $tags |
72
|
|
|
* @param string $key |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
protected function getRuntimeCache($tags, $key) |
77
|
|
|
{ |
78
|
|
|
return $this->runtimeCache->tags($tags)->get($key); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Retrieve cached items. |
83
|
|
|
* |
84
|
|
|
* @param mixed $tags |
85
|
|
|
* @param string $key |
86
|
|
|
* @param int $duration |
87
|
|
|
* @param Closure $callable |
88
|
|
|
* |
89
|
|
|
* @throws ErrorException |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function retrieve($tags, $key, $duration, Closure $callable) |
94
|
|
|
{ |
95
|
|
|
if ($data = $this->getRuntimeCache($tags, $key)) { |
96
|
|
|
return $data; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$cache = $this->taggedCache($this->cache, $tags); |
100
|
|
|
|
101
|
|
|
$data = ($duration > 0) ? |
102
|
|
|
$cache->remember($key, $duration, $callable) : |
103
|
|
|
$cache->rememberForever($key, $callable); |
104
|
|
|
|
105
|
|
|
$this->setRuntimeCache($tags, $key, $data); |
106
|
|
|
|
107
|
|
|
return $data; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set runtime cache object. |
112
|
|
|
* |
113
|
|
|
* @param mixed $tags |
114
|
|
|
* @param string $key |
115
|
|
|
* @param mixed $value |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
protected function setRuntimeCache($tags, $key, $value): void |
120
|
|
|
{ |
121
|
|
|
$this->runtimeCache->tags($tags)->forever($key, $value); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get tagged cache object. |
126
|
|
|
* |
127
|
|
|
* @param Store $cache |
128
|
|
|
* @param mixed $tags |
129
|
|
|
* |
130
|
|
|
* @throws ErrorException |
131
|
|
|
* |
132
|
|
|
* @return \Illuminate\Cache\TaggedCache|TaggableStore |
133
|
|
|
*/ |
134
|
|
|
protected function taggedCache(Store $cache, $tags) |
135
|
|
|
{ |
136
|
|
|
if (!($cache instanceof TaggableStore)) { |
137
|
|
|
throw new ErrorException('Laravel Cacheable requires taggable cache store.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return !empty($tags) ? $cache->tags($tags) : $cache; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Build CacheableDecorator and wrap the given class name |
145
|
|
|
* or repository object. |
146
|
|
|
* |
147
|
|
|
* @param mixed $repository |
148
|
|
|
* |
149
|
|
|
* @return \Suitmedia\Cacheable\CacheableDecorator |
150
|
|
|
*/ |
151
|
|
|
public function wrap($repository): CacheableDecorator |
152
|
|
|
{ |
153
|
|
|
if (is_string($repository)) { |
154
|
|
|
$repository = \App::make($repository); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->wrapWithDecorator($repository); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Wrap the given CacheableRepository with a new CacheableDecorator. |
162
|
|
|
* |
163
|
|
|
* @param \Suitmedia\Cacheable\Contracts\CacheableRepository $repository |
164
|
|
|
* |
165
|
|
|
* @return \Suitmedia\Cacheable\CacheableDecorator |
166
|
|
|
*/ |
167
|
|
|
protected function wrapWithDecorator(CacheableRepository $repository): CacheableDecorator |
168
|
|
|
{ |
169
|
|
|
return new CacheableDecorator($this, $repository); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|