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