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
|
|
|
/** |
53
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function find($id, $columns = ['*']) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$tag = str_slug(get_class($this->model)); |
58
|
|
|
$key = "{$tag}_{$id}_" . implode('_', $columns); |
59
|
|
|
|
60
|
|
|
return $this->cache([$tag]) |
61
|
|
|
->rememberForever($key, function () use ($id, $columns) { |
62
|
|
|
return parent::find($id, $columns); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function count($columns = '*') |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$tag = str_slug(get_class($this->model)); |
69
|
|
|
$key = "{$tag}_" . implode('_', $columns); |
70
|
|
|
|
71
|
|
|
return $this->cache([$tag]) |
72
|
|
|
->rememberForever($key, function () use ($id, $columns) { |
|
|
|
|
73
|
|
|
return parent::count($columns); |
|
|
|
|
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function first($columns = ['*']) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$tag = str_slug(get_class($this->model)); |
80
|
|
|
$key = "{$tag}_" . implode('_', $columns); |
81
|
|
|
|
82
|
|
|
return $this->cache([$tag]) |
83
|
|
|
->rememberForever($key, function () use ($id, $columns) { |
|
|
|
|
84
|
|
|
return parent::first($columns); |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function get($columns = ['*']) |
89
|
|
|
{ |
90
|
|
|
$tag = str_slug(get_class($this->model)); |
91
|
|
|
$key = "{$tag}_" . implode('_', $columns); |
92
|
|
|
$key .= collect($this->query->wheres)->reduce(function ($carry, $where) { |
93
|
|
|
$value = $where['value'] ?? implode('_', $where['values']) ?? ''; |
94
|
|
|
|
95
|
|
|
return "{$carry}-{$where['column']}_{$value}"; |
96
|
|
|
}); |
97
|
|
|
$key .= '-' . implode('-', collect($this->eagerLoad)->keys()->toArray()); |
98
|
|
|
|
99
|
|
|
return $this->cache([$tag]) |
100
|
|
|
->rememberForever($key, function () use ($columns) { |
101
|
|
|
return parent::get($columns); |
102
|
|
|
}); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.