1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Widgets\Utils; |
4
|
|
|
|
5
|
|
|
class Cache |
6
|
|
|
{ |
7
|
|
|
private $_cacheTag; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Cache constructor. |
11
|
|
|
*/ |
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
$this->_cacheTag = app(CacheTag::class); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Caches the widget output. |
19
|
|
|
* |
20
|
|
|
* @param $args |
21
|
|
|
* @param $phpCode |
22
|
|
|
* @param $widget |
23
|
|
|
* |
24
|
|
|
* @return null |
25
|
|
|
*/ |
26
|
|
|
public function cacheResult($args, $phpCode, $widget) |
27
|
|
|
{ |
28
|
|
|
$key = $this->_makeCacheKey($args, $widget); |
29
|
|
|
|
30
|
|
|
$cache = app('cache'); |
31
|
|
|
|
32
|
|
|
if (!empty($widget->cacheTags) && $this->cacheDriverSupportsTags()) { |
33
|
|
|
$cache = $cache->tags($widget->cacheTags); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($widget->cacheLifeTime === 0) { |
37
|
|
|
return $phpCode(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $cache->remember($key, $widget->cacheLifeTime, $phpCode); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a unique cache key for each possible output. |
45
|
|
|
* |
46
|
|
|
* @param $arg |
47
|
|
|
* @param $widget |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
private function _makeCacheKey($arg, $widget) |
52
|
|
|
{ |
53
|
|
|
if (method_exists($widget, 'cacheKey')) { |
54
|
|
|
return $widget->cacheKey($arg); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$_key = ''; |
58
|
|
|
|
59
|
|
|
if (method_exists($widget, 'extraCacheKeyDependency')) { |
60
|
|
|
$_key = json_encode($widget->extraCacheKeyDependency($arg)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!$this->cacheDriverSupportsTags()) { |
64
|
|
|
$_key .= json_encode($this->getTagTokens($widget->cacheTags)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$_key .= json_encode($arg, JSON_FORCE_OBJECT) . app()->getLocale() . $widget->template . get_class($widget); |
68
|
|
|
|
69
|
|
|
return md5($_key); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
private function cacheDriverSupportsTags() |
76
|
|
|
{ |
77
|
|
|
return !in_array(config('cache.default', 'file'), ['file', 'database']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $cacheTags |
82
|
|
|
* @param $_key |
83
|
|
|
* @return string |
84
|
|
|
* @internal param $widget |
85
|
|
|
*/ |
86
|
|
|
private function getTagTokens($cacheTags) |
87
|
|
|
{ |
88
|
|
|
return array_map(function ($tag) { |
89
|
|
|
return $this->_cacheTag->getTagToken($tag); |
90
|
|
|
}, $cacheTags); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $tags |
95
|
|
|
*/ |
96
|
|
|
public function expireTaggedWidgets($tags) |
97
|
|
|
{ |
98
|
|
|
if ($this->cacheDriverSupportsTags()) { |
99
|
|
|
return \Cache::tags($tags)->flush(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (is_string($tags)) { |
103
|
|
|
$tags = [$tags]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ($tags as $tag) { |
107
|
|
|
$this->_cacheTag->generateNewToken($tag); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|