|
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 $cacheCooldownSeconds = 0; |
|
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 = null |
|
92
|
|
|
) : EloquentBuilder { |
|
93
|
|
|
if (! $seconds) { |
|
|
|
|
|
|
94
|
|
|
$seconds = $this->cacheCooldownSeconds; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$cachePrefix = $this->getCachePrefix(); |
|
98
|
|
|
$modelClassName = get_class($this); |
|
99
|
|
|
$cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds"; |
|
100
|
|
|
|
|
101
|
|
|
$this->cache() |
|
102
|
|
|
->rememberForever($cacheKey, function () use ($seconds) { |
|
103
|
|
|
return $seconds; |
|
104
|
|
|
}); |
|
105
|
|
|
|
|
106
|
|
|
$cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at"; |
|
107
|
|
|
$this->cache() |
|
108
|
|
|
->rememberForever($cacheKey, function () { |
|
109
|
|
|
return (new Carbon)->now(); |
|
110
|
|
|
}); |
|
111
|
|
|
|
|
112
|
|
|
return $query; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getcacheCooldownSecondsAttribute() : bool |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->cacheCooldownSeconds; |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: