|
1 | 1 | <?php namespace GeneaLabs\LaravelModelCaching\Traits;
|
2 | 2 |
|
| 3 | +use GeneaLabs\LaravelModelCaching\CacheKey; |
| 4 | +use GeneaLabs\LaravelModelCaching\CacheTags; |
| 5 | +use GeneaLabs\LaravelModelCaching\CachedModel; |
3 | 6 | use Illuminate\Cache\TaggableStore;
|
4 | 7 |
|
5 | 8 | trait Cachable
|
6 | 9 | {
|
| 10 | + protected $isCachable = true; |
| 11 | + |
7 | 12 | protected function cache(array $tags = [])
|
8 | 13 | {
|
9 | 14 | $cache = cache();
|
10 | 15 |
|
| 16 | + if (config('laravel-model-caching.store')) { |
| 17 | + $cache = $cache->store(config('laravel-model-caching.store')); |
| 18 | + } |
| 19 | + |
11 | 20 | if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
|
| 21 | + if (is_a($this, CachedModel::class)) { |
| 22 | + array_push($tags, str_slug(get_called_class())); |
| 23 | + } |
| 24 | + |
12 | 25 | $cache = $cache->tags($tags);
|
13 | 26 | }
|
14 | 27 |
|
15 | 28 | return $cache;
|
16 | 29 | }
|
| 30 | + |
| 31 | + public function disableCache() |
| 32 | + { |
| 33 | + session(['genealabs-laravel-model-caching-is-disabled' => true]); |
| 34 | + $this->isCachable = false; |
| 35 | + |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
| 39 | + public function flushCache(array $tags = []) |
| 40 | + { |
| 41 | + $this->cache($tags)->flush(); |
| 42 | + } |
| 43 | + |
| 44 | + protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string |
| 45 | + { |
| 46 | + return (new CacheKey($this->eagerLoad, $this->model, $this->query)) |
| 47 | + ->make($columns, $idColumn); |
| 48 | + } |
| 49 | + |
| 50 | + protected function makeCacheTags() : array |
| 51 | + { |
| 52 | + return (new CacheTags($this->eagerLoad, $this->model)) |
| 53 | + ->make(); |
| 54 | + } |
17 | 55 | }
|
0 commit comments