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