Total Complexity | 16 |
Total Lines | 104 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php namespace GeneaLabs\LaravelModelCaching\Traits; |
||
11 | trait Cachable |
||
12 | { |
||
13 | protected $isCachable = true; |
||
14 | |||
15 | protected function cache(array $tags = []) |
||
16 | { |
||
17 | $cache = cache(); |
||
18 | |||
19 | if (config('laravel-model-caching.store')) { |
||
20 | $cache = $cache->store(config('laravel-model-caching.store')); |
||
21 | } |
||
22 | |||
23 | if (is_subclass_of($cache->getStore(), TaggableStore::class)) { |
||
24 | $tags = $this->addTagsWhenCalledFromCachedBuilder($tags); |
||
25 | $cache = $cache->tags($tags); |
||
26 | } |
||
27 | |||
28 | return $cache; |
||
29 | } |
||
30 | |||
31 | protected function addTagsWhenCalledFromCachedBuilder(array $tags) : array |
||
32 | { |
||
33 | $usesCachableTrait = collect(class_uses($this)) |
||
34 | ->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable"); |
||
35 | |||
36 | if (! $usesCachableTrait) { |
||
37 | array_push($tags, str_slug(get_called_class())); |
||
38 | } |
||
39 | |||
40 | return $tags; |
||
41 | } |
||
42 | |||
43 | public function disableCache() |
||
44 | { |
||
45 | $this->isCachable = false; |
||
46 | |||
47 | return $this; |
||
48 | } |
||
49 | |||
50 | public function flushCache(array $tags = []) |
||
51 | { |
||
52 | if (count($tags) === 0) { |
||
53 | $tags = $this->makeCacheTags(); |
||
54 | } |
||
55 | |||
56 | $this->cache($tags)->flush(); |
||
57 | } |
||
58 | |||
59 | protected function makeCacheKey( |
||
60 | array $columns = ['*'], |
||
61 | $idColumn = null, |
||
62 | string $keyDifferentiator = '' |
||
63 | ) : string { |
||
64 | $eagerLoad = $this->eagerLoad ?? []; |
||
65 | $model = $this->model ?? $this; |
||
66 | $query = $this->query ?? app(Builder::class); |
||
1 ignored issue
–
show
|
|||
67 | |||
68 | return (new CacheKey($eagerLoad, $model, $query)) |
||
1 ignored issue
–
show
|
|||
69 | ->make($columns, $idColumn, $keyDifferentiator); |
||
70 | } |
||
71 | |||
72 | protected function makeCacheTags() : array |
||
73 | { |
||
74 | $tags = (new CacheTags($this->eagerLoad ?? [], $this->model ?? $this)) |
||
1 ignored issue
–
show
|
|||
75 | ->make(); |
||
76 | |||
77 | return $tags; |
||
78 | } |
||
79 | |||
80 | public static function bootCachable() |
||
81 | { |
||
82 | static::saved(function ($instance) { |
||
83 | $instance->flushCache(); |
||
84 | }); |
||
85 | } |
||
86 | |||
87 | public static function all($columns = ['*']) |
||
88 | { |
||
89 | $class = get_called_class(); |
||
90 | $instance = new $class; |
||
91 | $tags = [str_slug(get_called_class())]; |
||
92 | $key = $instance->makeCacheKey(); |
||
93 | |||
94 | return $instance->cache($tags) |
||
95 | ->rememberForever($key, function () use ($columns) { |
||
96 | return parent::all($columns); |
||
97 | }); |
||
98 | } |
||
99 | |||
100 | public function newEloquentBuilder($query) |
||
109 | } |
||
110 | |||
111 | public function isCachable() : bool |
||
112 | { |
||
113 | return $this->isCachable |
||
114 | && ! config('laravel-model-caching.disabled'); |
||
115 | } |
||
116 | } |
||
117 |