1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching; |
2
|
|
|
|
3
|
|
|
use Closure; |
4
|
|
|
use Illuminate\Cache\TaggableStore; |
5
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Pivot; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
9
|
|
|
|
10
|
|
|
class Builder extends EloquentBuilder |
11
|
|
|
{ |
12
|
|
|
protected function cache(array $tags = []) |
13
|
|
|
{ |
14
|
|
|
$cache = cache(); |
15
|
|
|
|
16
|
|
|
if (is_subclass_of($cache->getStore(), TaggableStore::class)) { |
17
|
|
|
$cache = $cache->tags($tags); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return $cache; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// protected function cacheResults(Relation $relation, array $models, string $name) : array |
24
|
|
|
// { |
25
|
|
|
// $parentIds = implode('_', collect($models)->pluck('id')->toArray()); |
26
|
|
|
// $parentName = str_slug(get_class($relation->getParent())); |
27
|
|
|
// $childName = str_slug(get_class($relation->getRelated())); |
28
|
|
|
// |
29
|
|
|
// $cachedResults = $this->cache([$parentName, $childName])->rememberForever( |
30
|
|
|
// "{$parentName}_{$parentIds}-{$childName}s", |
31
|
|
|
// function () use ($relation, $models, $name) { |
32
|
|
|
// return $relation->match( |
33
|
|
|
// $relation->initRelation($models, $name), |
34
|
|
|
// $relation->getEager(), |
35
|
|
|
// $name |
36
|
|
|
// ); |
37
|
|
|
// } |
38
|
|
|
// ); |
39
|
|
|
// |
40
|
|
|
// return $cachedResults; |
41
|
|
|
// } |
42
|
|
|
// |
43
|
|
|
// protected function eagerLoadRelation(array $models, $name, Closure $constraints) |
44
|
|
|
// { |
45
|
|
|
// $relation = $this->getRelation($name); |
46
|
|
|
// $relation->addEagerConstraints($models); |
47
|
|
|
// $constraints($relation); |
48
|
|
|
// |
49
|
|
|
// return $this->cacheResults($relation, $models, $name); |
50
|
|
|
// } |
51
|
|
|
|
52
|
|
|
protected function getCacheKey(array $columns = ['*'], $ids = null) : string |
53
|
|
|
{ |
54
|
|
|
$key = str_slug(get_class($this->model)); |
55
|
|
|
|
56
|
|
|
if ($ids) { |
57
|
|
|
$key .= '_' . (is_array($ids) |
58
|
|
|
? implode('_', $ids) |
59
|
|
|
: $ids); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($columns !== ['*']) { |
63
|
|
|
$key .= '_' . implode('_', $columns); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$key .= collect($this->query->wheres)->reduce(function ($carry, $where) { |
67
|
|
|
$value = $where['value'] ?? implode('_', $where['values']) ?? ''; |
68
|
|
|
|
69
|
|
|
return "{$carry}-{$where['column']}_{$value}"; |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
if (collect($this->eagerLoad)->isNotEmpty()) { |
73
|
|
|
$key .= '-' . implode('-', collect($this->eagerLoad)->keys()->toArray()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($this->query->offset) { |
77
|
|
|
$key .= "-offset_{$this->query->offset}"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->query->limit) { |
81
|
|
|
$key .= "-limit_{$this->query->limit}"; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $key; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getCacheTags() : array |
88
|
|
|
{ |
89
|
|
|
return collect($this->eagerLoad)->keys() |
90
|
|
|
->map(function ($name) { |
91
|
|
|
return str_slug(get_class( |
92
|
|
|
$this->model |
93
|
|
|
->{$name}() |
94
|
|
|
->getQuery() |
95
|
|
|
->model |
96
|
|
|
)); |
97
|
|
|
}) |
98
|
|
|
->prepend(str_slug(get_class($this->model))) |
99
|
|
|
->values() |
100
|
|
|
->toArray(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function count($columns = ['*']) |
104
|
|
|
{ |
105
|
|
|
$tags = [str_slug(get_class($this->model))]; |
106
|
|
|
$key = str_slug(get_class($this->model)) ."-count"; |
107
|
|
|
|
108
|
|
|
return $this->cache($tags) |
109
|
|
|
->rememberForever($key, function () use ($columns) { |
110
|
|
|
return parent::count($columns); |
|
|
|
|
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function find($id, $columns = ['*']) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$tags = $this->getCacheTags(); |
120
|
|
|
$key = $this->getCacheKey($columns, $id); |
121
|
|
|
|
122
|
|
|
return $this->cache($tags) |
123
|
|
|
->rememberForever($key, function () use ($id, $columns) { |
124
|
|
|
return parent::find($id, $columns); |
125
|
|
|
}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
public function first($columns = ['*']) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$tags = $this->getCacheTags(); |
131
|
|
|
$key = $this->getCacheKey($columns); |
132
|
|
|
|
133
|
|
|
return $this->cache($tags) |
134
|
|
|
->rememberForever($key, function () use ($columns) { |
135
|
|
|
return parent::first($columns); |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function get($columns = ['*']) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$tags = $this->getCacheTags(); |
142
|
|
|
$key = $this->getCacheKey($columns); |
143
|
|
|
|
144
|
|
|
return $this->cache($tags) |
145
|
|
|
->rememberForever($key, function () use ($columns) { |
146
|
|
|
return parent::get($columns); |
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|