|
1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching; |
|
2
|
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable; |
|
4
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
|
5
|
|
|
|
|
6
|
|
|
class CachedBuilder extends EloquentBuilder |
|
7
|
|
|
{ |
|
8
|
|
|
use Cachable; |
|
9
|
|
|
|
|
10
|
|
View Code Duplication |
public function avg($column) |
|
11
|
|
|
{ |
|
12
|
|
|
return $this->cache($this->makeCacheTags()) |
|
13
|
|
|
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) { |
|
14
|
|
|
return parent::avg($column); |
|
|
|
|
|
|
15
|
|
|
}); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
View Code Duplication |
public function count($columns = ['*']) |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->cache($this->makeCacheTags()) |
|
21
|
|
|
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) { |
|
22
|
|
|
return parent::count($columns); |
|
|
|
|
|
|
23
|
|
|
}); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function cursor() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->cache($this->makeCacheTags()) |
|
29
|
|
|
->rememberForever($this->makeCacheKey() . "-cursor", function () { |
|
30
|
|
|
return collect(parent::cursor()); |
|
31
|
|
|
}); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function delete() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->cache($this->makeCacheTags()) |
|
37
|
|
|
->flush(); |
|
38
|
|
|
|
|
39
|
|
|
return parent::delete(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
public function find($id, $columns = ['*']) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->cache($this->makeCacheTags()) |
|
48
|
|
|
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) { |
|
49
|
|
|
return parent::find($id, $columns); |
|
50
|
|
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
View Code Duplication |
public function first($columns = ['*']) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->cache($this->makeCacheTags()) |
|
56
|
|
|
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) { |
|
57
|
|
|
return parent::first($columns); |
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
View Code Duplication |
public function get($columns = ['*']) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->cache($this->makeCacheTags()) |
|
64
|
|
|
->rememberForever($this->makeCacheKey($columns), function () use ($columns) { |
|
65
|
|
|
return parent::get($columns); |
|
66
|
|
|
}); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
View Code Duplication |
public function max($column) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->cache($this->makeCacheTags()) |
|
72
|
|
|
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) { |
|
73
|
|
|
return parent::max($column); |
|
|
|
|
|
|
74
|
|
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
public function min($column) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->cache($this->makeCacheTags()) |
|
80
|
|
|
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) { |
|
81
|
|
|
return parent::min($column); |
|
|
|
|
|
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function pluck($column, $key = null) |
|
86
|
|
|
{ |
|
87
|
|
|
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}"; |
|
88
|
|
|
|
|
89
|
|
|
if ($key) { |
|
90
|
|
|
$cacheKey .= "_{$key}"; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->cache($this->makeCacheTags()) |
|
94
|
|
|
->rememberForever($cacheKey, function () use ($column, $key) { |
|
95
|
|
|
return parent::pluck($column, $key); |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function sum($column) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->cache($this->makeCacheTags()) |
|
102
|
|
|
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) { |
|
103
|
|
|
return parent::sum($column); |
|
|
|
|
|
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string |
|
108
|
|
|
{ |
|
109
|
|
|
return (new CacheKey($this->eagerLoad, $this->model, $this->query)) |
|
110
|
|
|
->make($columns, $idColumn); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function makeCacheTags() : array |
|
114
|
|
|
{ |
|
115
|
|
|
return (new CacheTags($this->eagerLoad, $this->model)) |
|
116
|
|
|
->make(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|