|
1 | 1 | <?php namespace GeneaLabs\LaravelModelCaching; |
2 | 2 |
|
3 | | -use GeneaLabs\LaravelModelCaching\CachedBuilder as Builder; |
4 | 3 | use GeneaLabs\LaravelModelCaching\Traits\Cachable; |
5 | | -use Illuminate\Cache\CacheManager; |
6 | | -use Illuminate\Cache\TaggableStore; |
7 | | -use Illuminate\Cache\TaggedCache; |
8 | | -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
9 | 4 | use Illuminate\Database\Eloquent\Model; |
10 | | -use Illuminate\Database\Eloquent\Relations\Relation; |
11 | | -use Illuminate\Support\Collection; |
12 | | -use LogicException; |
13 | 5 |
|
14 | 6 | abstract class CachedModel extends Model |
15 | 7 | { |
16 | 8 | use Cachable; |
17 | | - |
18 | | - public function newEloquentBuilder($query) |
19 | | - { |
20 | | - if (session('genealabs-laravel-model-caching-is-disabled')) { |
21 | | - session()->forget('genealabs-laravel-model-caching-is-disabled'); |
22 | | - |
23 | | - return new EloquentBuilder($query); |
24 | | - } |
25 | | - |
26 | | - return new Builder($query); |
27 | | - } |
28 | | - |
29 | | - public static function boot() |
30 | | - { |
31 | | - parent::boot(); |
32 | | - |
33 | | - $class = get_called_class(); |
34 | | - $instance = new $class; |
35 | | - |
36 | | - static::created(function () use ($instance) { |
37 | | - $instance->flushCache(); |
38 | | - }); |
39 | | - |
40 | | - static::deleted(function () use ($instance) { |
41 | | - $instance->flushCache(); |
42 | | - }); |
43 | | - |
44 | | - static::saved(function () use ($instance) { |
45 | | - $instance->flushCache(); |
46 | | - }); |
47 | | - |
48 | | - static::updated(function () use ($instance) { |
49 | | - $instance->flushCache(); |
50 | | - }); |
51 | | - } |
52 | | - |
53 | | - public static function all($columns = ['*']) |
54 | | - { |
55 | | - $class = get_called_class(); |
56 | | - $instance = new $class; |
57 | | - $tags = [str_slug(get_called_class())]; |
58 | | - $key = $instance->makeCacheKey(); |
59 | | - |
60 | | - return $instance->cache($tags) |
61 | | - ->rememberForever($key, function () use ($columns) { |
62 | | - return parent::all($columns); |
63 | | - }); |
64 | | - } |
65 | 9 | } |
0 commit comments