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