|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sulao\EasyCache; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Cache\Repository; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class CachePlug |
|
9
|
|
|
* |
|
10
|
|
|
* @package Sulao\EasyCache |
|
11
|
|
|
*/ |
|
12
|
|
|
class CachePlug |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var object |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $instance; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Repository |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $cache; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $ttl; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $key; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string|null |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $prefix; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $refresh; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* CacheMethod constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param object $instance |
|
48
|
|
|
* @param int|null $ttl |
|
49
|
|
|
* @param string|null $key |
|
50
|
|
|
* @param string|null $store |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function __construct( |
|
53
|
|
|
$instance, |
|
54
|
|
|
$ttl = null, |
|
55
|
|
|
$key = null, |
|
56
|
|
|
$store = null |
|
57
|
|
|
) { |
|
58
|
4 |
|
$this->instance = $instance; |
|
59
|
|
|
|
|
60
|
4 |
|
$this->ttl = !is_null($ttl) ? $ttl : (config('easy-cache.ttl') ?: 3600); |
|
61
|
4 |
|
$this->key = $key; |
|
62
|
|
|
|
|
63
|
4 |
|
$store = !is_null($store) ? $store : config('easy-cache.store'); |
|
64
|
4 |
|
$this->cache = app('cache')->store($store); |
|
65
|
|
|
|
|
66
|
4 |
|
$prefix = config('easy-cache.prefix'); |
|
67
|
4 |
|
$this->prefix = $prefix ? $prefix . '::' : ''; |
|
68
|
|
|
|
|
69
|
4 |
|
$refreshKey = config('easy-cache.refresh_key'); |
|
70
|
4 |
|
$this->refresh = $refreshKey && app('request')->get($refreshKey); |
|
71
|
4 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Static call method |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
* @param array $arguments |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
*/ |
|
81
|
4 |
|
public function __call($name, $arguments) |
|
82
|
|
|
{ |
|
83
|
4 |
|
$cacheKey = $this->key ?: md5( |
|
84
|
4 |
|
get_class($this->instance) . '::' . serialize(func_get_args()) |
|
85
|
|
|
); |
|
86
|
4 |
|
$cacheKey = $this->prefix . $cacheKey; |
|
87
|
|
|
|
|
88
|
4 |
|
if (empty($this->refresh)) { |
|
89
|
4 |
|
$result = $this->cache->get($cacheKey); |
|
90
|
|
|
|
|
91
|
4 |
|
if (!is_null($result)) { |
|
92
|
4 |
|
return $result; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
$result = call_user_func_array([$this->instance, $name], $arguments); |
|
97
|
4 |
|
$this->cache->put($cacheKey, $result, $this->ttl); |
|
98
|
|
|
|
|
99
|
4 |
|
return $result; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|