|
1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\CacheKey; |
|
4
|
|
|
use GeneaLabs\LaravelModelCaching\CacheTags; |
|
5
|
|
|
use GeneaLabs\LaravelModelCaching\CachedModel; |
|
6
|
|
|
use Illuminate\Cache\TaggableStore; |
|
7
|
|
|
use Illuminate\Database\Query\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
|
9
|
|
|
use GeneaLabs\LaravelModelCaching\CachedBuilder; |
|
10
|
|
|
|
|
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
|
|
|
if (is_a($this, CachedModel::class)) { |
|
25
|
|
|
array_push($tags, str_slug(get_called_class())); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$cache = $cache->tags($tags); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $cache; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function disableCache() |
|
35
|
|
|
{ |
|
36
|
|
|
session(['genealabs-laravel-model-caching-is-disabled' => true]); |
|
37
|
|
|
$this->isCachable = false; |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function flushCache(array $tags = []) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->cache($tags)->flush(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function makeCacheKey( |
|
48
|
|
|
array $columns = ['*'], |
|
49
|
|
|
$idColumn = null, |
|
50
|
|
|
string $keyDifferentiator = '' |
|
51
|
|
|
) : string { |
|
52
|
|
|
$eagerLoad = $this->eagerLoad ?? []; |
|
53
|
|
|
$model = $this->model ?? $this; |
|
54
|
|
|
$query = $this->query ?? app(Builder::class); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
return (new CacheKey($eagerLoad, $model, $query)) |
|
|
|
|
|
|
57
|
|
|
->make($columns, $idColumn, $keyDifferentiator); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function makeCacheTags() : array |
|
61
|
|
|
{ |
|
62
|
|
|
return (new CacheTags($this->eagerLoad, $this->model)) |
|
63
|
|
|
->make(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function bootCachable() |
|
67
|
|
|
{ |
|
68
|
|
|
static::saved(function ($instance) { |
|
69
|
|
|
$instance->flushCache(); |
|
70
|
|
|
}); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function all($columns = ['*']) |
|
74
|
|
|
{ |
|
75
|
|
|
$class = get_called_class(); |
|
76
|
|
|
$instance = new $class; |
|
77
|
|
|
$tags = [str_slug(get_called_class())]; |
|
78
|
|
|
$key = $instance->makeCacheKey(); |
|
79
|
|
|
|
|
80
|
|
|
return $instance->cache($tags) |
|
81
|
|
|
->rememberForever($key, function () use ($columns) { |
|
82
|
|
|
return parent::all($columns); |
|
83
|
|
|
}); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function newEloquentBuilder($query) |
|
87
|
|
|
{ |
|
88
|
|
|
if (session('genealabs-laravel-model-caching-is-disabled')) { |
|
89
|
|
|
session()->forget('genealabs-laravel-model-caching-is-disabled'); |
|
90
|
|
|
|
|
91
|
|
|
return new EloquentBuilder($query); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return new CachedBuilder($query); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|