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