1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching\Traits; |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use GeneaLabs\LaravelModelCaching\CachedBuilder; |
5
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
6
|
|
|
|
7
|
|
|
trait ModelCaching |
8
|
|
|
{ |
9
|
|
|
protected $useCacheCooldown = false; |
10
|
|
|
|
11
|
|
|
public static function all($columns = ['*']) |
12
|
|
|
{ |
13
|
|
|
if (config('laravel-model-caching.disabled')) { |
14
|
|
|
return parent::all($columns); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
$class = get_called_class(); |
18
|
|
|
$instance = new $class; |
19
|
|
|
$tags = $instance->makeCacheTags(); |
20
|
|
|
$key = $instance->makeCacheKey(); |
21
|
|
|
|
22
|
|
|
return $instance->cache($tags) |
23
|
|
|
->rememberForever($key, function () use ($columns) { |
24
|
|
|
return parent::all($columns); |
25
|
|
|
}); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function bootCachable() |
29
|
|
|
{ |
30
|
|
|
static::created(function ($instance) { |
31
|
|
|
$instance->checkCooldownAndFlushAfterPersisting($instance); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
static::deleted(function ($instance) { |
35
|
|
|
$instance->checkCooldownAndFlushAfterPersisting($instance); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
static::saved(function ($instance) { |
39
|
|
|
$instance->checkCooldownAndFlushAfterPersisting($instance); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
// TODO: figure out how to add this listener |
43
|
|
|
// static::restored(function ($instance) { |
44
|
|
|
// $instance->checkCooldownAndFlushAfterPersisting($instance); |
45
|
|
|
// }); |
46
|
|
|
|
47
|
|
|
static::pivotAttached(function ($instance) { |
48
|
|
|
$instance->checkCooldownAndFlushAfterPersisting($instance); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
static::pivotDetached(function ($instance) { |
52
|
|
|
$instance->checkCooldownAndFlushAfterPersisting($instance); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
static::pivotUpdated(function ($instance) { |
56
|
|
|
$instance->checkCooldownAndFlushAfterPersisting($instance); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function destroy($ids) |
61
|
|
|
{ |
62
|
|
|
$class = get_called_class(); |
63
|
|
|
$instance = new $class; |
64
|
|
|
$instance->flushCache(); |
65
|
|
|
|
66
|
|
|
return parent::destroy($ids); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function newEloquentBuilder($query) |
70
|
|
|
{ |
71
|
|
|
if (! $this->isCachable()) { |
72
|
|
|
$this->isCachable = true; |
73
|
|
|
|
74
|
|
|
return new EloquentBuilder($query); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return new CachedBuilder($query); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder |
81
|
|
|
{ |
82
|
|
|
if ($this->isCachable()) { |
83
|
|
|
$query = $query->disableModelCaching(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $query; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function scopeWithCacheCooldownSeconds( |
90
|
|
|
EloquentBuilder $query, |
91
|
|
|
int $seconds |
92
|
|
|
) : EloquentBuilder { |
93
|
|
|
$cachePrefix = $this->getCachePrefix(); |
94
|
|
|
$modelClassName = get_class($this); |
95
|
|
|
$cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds"; |
96
|
|
|
|
97
|
|
|
$this->cache() |
98
|
|
|
->rememberForever($cacheKey, function () use ($seconds) { |
99
|
|
|
return $seconds; |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
$cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at"; |
103
|
|
|
$this->cache() |
104
|
|
|
->rememberForever($cacheKey, function () { |
105
|
|
|
return (new Carbon)->now(); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
return $query; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getUseCacheCooldownAttribute() : bool |
112
|
|
|
{ |
113
|
|
|
return $this->useCacheCooldown; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|