1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Resta\Cache;
|
4
|
|
|
|
5
|
|
|
use Resta\Contracts\ApplicationContracts;
|
6
|
|
|
|
7
|
|
|
class CacheManager extends CacheAdapter
|
8
|
|
|
{
|
9
|
|
|
/**
|
10
|
|
|
* @var string
|
11
|
|
|
*/
|
12
|
|
|
protected $cache;
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* @var int $expire
|
16
|
|
|
*/
|
17
|
|
|
protected $expire;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @var string $file
|
21
|
|
|
*/
|
22
|
|
|
protected $adapter;
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* @var null $path
|
26
|
|
|
*/
|
27
|
|
|
protected $path;
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @var string $name
|
31
|
|
|
*/
|
32
|
|
|
protected $name;
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @var bool
|
36
|
|
|
*/
|
37
|
|
|
protected $cacheServiceProvider = false;
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* CacheManager constructor.
|
41
|
|
|
* @param ApplicationContracts $app
|
42
|
|
|
*/
|
43
|
|
|
public function __construct(ApplicationContracts $app)
|
44
|
|
|
{
|
45
|
|
|
parent::__construct($app);
|
46
|
|
|
|
47
|
|
|
//get configuration variables from application
|
48
|
|
|
$config = $this->app->resolve(CacheConfigDetector::class)->getConfig();
|
49
|
|
|
|
50
|
|
|
$this->adapter = $config['adapter'];
|
51
|
|
|
$this->path = $config['path'];
|
52
|
|
|
$this->expire = $config['expire'];
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* change cache adapter
|
57
|
|
|
*
|
58
|
|
|
* @param null|string $adapter
|
59
|
|
|
* @return $this
|
60
|
|
|
*/
|
61
|
|
|
public function adapter($adapter)
|
62
|
|
|
{
|
63
|
|
|
if(!is_null($adapter)){
|
64
|
|
|
$this->adapter = $adapter;
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
return $this;
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
/**
|
71
|
|
|
* cache service provider register
|
72
|
|
|
*
|
73
|
|
|
* @param $cacheItem
|
74
|
|
|
* @param $cacheProvider
|
75
|
|
|
* @param $data
|
76
|
|
|
*/
|
77
|
|
|
private function cacheServiceProvider($cacheItem,$cacheProvider,$data)
|
78
|
|
|
{
|
79
|
|
|
if($cacheProvider($data)){
|
80
|
|
|
$cacheItem->set($data);
|
81
|
|
|
$this->cache->save($cacheItem);
|
82
|
|
|
$this->cacheServiceProvider = true;
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
return $data;
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
/**
|
89
|
|
|
* get container cache service provider
|
90
|
|
|
*
|
91
|
|
|
* @param callable $callback
|
92
|
|
|
* @param $cacheItem
|
93
|
|
|
* @param $data
|
94
|
|
|
* @return bool|mixed
|
95
|
|
|
*/
|
96
|
|
|
private function containerCacheServiceProvider(callable $callback,$cacheItem,$data)
|
97
|
|
|
{
|
98
|
|
|
if($this->app->has('cache.class')){
|
99
|
|
|
|
100
|
|
|
$class = $this->app->get('cache.class');
|
101
|
|
|
|
102
|
|
|
if(is_callable(
|
103
|
|
|
$cacheProvider = $this->app->get('cache.'.class_basename($class[0]).':'.$class[1]))
|
104
|
|
|
){
|
105
|
|
|
|
106
|
|
|
return $this->cacheServiceProvider($cacheItem,$cacheProvider,$data);
|
107
|
|
|
}
|
108
|
|
|
elseif(is_callable(
|
109
|
|
|
$cacheProvider = $this->app->get('cache.'.class_basename($class[0])))
|
110
|
|
|
)
|
111
|
|
|
{
|
112
|
|
|
return $this->cacheServiceProvider($cacheItem,$cacheProvider,$data);
|
113
|
|
|
}
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
return call_user_func($callback);
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* cache name
|
121
|
|
|
*
|
122
|
|
|
* @param null|string $name
|
123
|
|
|
* @return $this
|
124
|
|
|
*/
|
125
|
|
|
public function name($name)
|
126
|
|
|
{
|
127
|
|
|
//name variable is
|
128
|
|
|
//the name of the cache data set to be created.
|
129
|
|
|
if(!is_null($name)){
|
130
|
|
|
$this->name = $name;
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
return $this;
|
134
|
|
|
}
|
135
|
|
|
|
136
|
|
|
/**
|
137
|
|
|
* cache expire
|
138
|
|
|
*
|
139
|
|
|
* @param int|mixed $expire
|
140
|
|
|
* @return $this
|
141
|
|
|
*/
|
142
|
|
|
public function expire($expire)
|
143
|
|
|
{
|
144
|
|
|
//Cache data is set at the time.
|
145
|
|
|
//Data will be valid in this time.
|
146
|
|
|
if(is_numeric($expire)){
|
147
|
|
|
$this->expire = $expire;
|
148
|
|
|
}
|
149
|
|
|
|
150
|
|
|
return $this;
|
151
|
|
|
}
|
152
|
|
|
|
153
|
|
|
/**
|
154
|
|
|
* get cache
|
155
|
|
|
*
|
156
|
|
|
* @param callable $callback
|
157
|
|
|
* @return mixed
|
158
|
|
|
*
|
159
|
|
|
* @throws \Psr\Cache\InvalidArgumentException
|
160
|
|
|
*/
|
161
|
|
|
public function get(callable $callback)
|
162
|
|
|
{
|
163
|
|
|
// this class has a macro that can be managed by the user.
|
164
|
|
|
// macros work as an extensible version of the classes.
|
165
|
|
|
$macro = $this->app['macro']->with(config('kernel.macros.cache'),$this,$this->adapter);
|
166
|
|
|
|
167
|
|
|
//set cache macroable object
|
168
|
|
|
$this->cache = $macro->{$this->adapter}($callback);
|
169
|
|
|
|
170
|
|
|
//With backtrace, we can specify an automatic name.
|
171
|
|
|
//This will automatically detect which service is running in the service.
|
172
|
|
|
$backtrace = debug_backtrace()[1];
|
173
|
|
|
|
174
|
|
|
//If name is null, we name it with backtrace.
|
175
|
|
|
if($this->name===null) {
|
176
|
|
|
$this->name = md5($backtrace['function'].'_'.$backtrace['class']);
|
177
|
|
|
}
|
178
|
|
|
|
179
|
|
|
//this method may show continuity depending on the macro.
|
180
|
|
|
if(false === $this instanceof $macro) return ;
|
181
|
|
|
|
182
|
|
|
// retrieve the cache item
|
183
|
|
|
$cacheItem = $this->cache->getItem($this->name);
|
184
|
|
|
|
185
|
|
|
if (!$cacheItem->isHit()) {
|
186
|
|
|
|
187
|
|
|
$data = call_user_func($callback);
|
188
|
|
|
|
189
|
|
|
return $this->containerCacheServiceProvider(function() use ($data,$cacheItem){
|
190
|
|
|
$cacheItem->set($data);
|
191
|
|
|
$this->cache->save($cacheItem);
|
192
|
|
|
|
193
|
|
|
return $data;
|
194
|
|
|
|
195
|
|
|
},$cacheItem,$data);
|
196
|
|
|
}
|
197
|
|
|
|
198
|
|
|
$this->app->register('illuminator','cache',['name'=>$this->name]);
|
199
|
|
|
|
200
|
|
|
// retrieve the value stored by the item
|
201
|
|
|
return $cacheItem->get();
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
/**
|
205
|
|
|
* check macro availability for adapter method
|
206
|
|
|
*
|
207
|
|
|
* @param $class
|
208
|
|
|
* @return mixed
|
209
|
|
|
*/
|
210
|
|
|
private function macro($class)
|
211
|
|
|
{
|
212
|
|
|
return app()['macro'](Cache::class)->isMacro($class)->get(function() use($class){
|
213
|
|
|
return $class;
|
214
|
|
|
});
|
215
|
|
|
}
|
216
|
|
|
} |