1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching; |
2
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable; |
4
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
8
|
|
|
*/ |
9
|
|
|
class CachedBuilder extends EloquentBuilder |
10
|
|
|
{ |
11
|
|
|
use Cachable; |
12
|
|
|
|
13
|
|
|
public function avg($column) |
14
|
|
|
{ |
15
|
|
|
if (! $this->isCachable) { |
16
|
|
|
return parent::avg($column); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
return $this->cache($this->makeCacheTags()) |
20
|
|
|
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) { |
21
|
|
|
return parent::avg($column); |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function count($columns = ['*']) |
26
|
|
|
{ |
27
|
|
|
if (! $this->isCachable) { |
28
|
|
|
return parent::count($columns); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->cache($this->makeCacheTags()) |
32
|
|
|
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) { |
33
|
|
|
return parent::count($columns); |
34
|
|
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function cursor() |
38
|
|
|
{ |
39
|
|
|
if (! $this->isCachable) { |
40
|
|
|
return collect(parent::cursor()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->cache($this->makeCacheTags()) |
44
|
|
|
->rememberForever($this->makeCacheKey() . "-cursor", function () { |
45
|
|
|
return collect(parent::cursor()); |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function delete() |
50
|
|
|
{ |
51
|
|
|
$this->cache($this->makeCacheTags()) |
52
|
|
|
->flush(); |
53
|
|
|
|
54
|
|
|
return parent::delete(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
59
|
|
|
*/ |
60
|
|
|
public function find($id, $columns = ['*']) |
61
|
|
|
{ |
62
|
|
|
if (! $this->isCachable) { |
63
|
|
|
return parent::find($id, $columns); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->cache($this->makeCacheTags()) |
67
|
|
|
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) { |
68
|
|
|
return parent::find($id, $columns); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function first($columns = ['*']) |
73
|
|
|
{ |
74
|
|
|
if (! $this->isCachable) { |
75
|
|
|
return parent::first($columns); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->cache($this->makeCacheTags()) |
79
|
|
|
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) { |
80
|
|
|
return parent::first($columns); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function get($columns = ['*']) |
85
|
|
|
{ |
86
|
|
|
if (! $this->isCachable) { |
87
|
|
|
return parent::get($columns); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->cache($this->makeCacheTags()) |
91
|
|
|
->rememberForever($this->makeCacheKey($columns), function () use ($columns) { |
92
|
|
|
return parent::get($columns); |
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function max($column) |
97
|
|
|
{ |
98
|
|
|
if (! $this->isCachable) { |
99
|
|
|
return parent::max($column); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->cache($this->makeCacheTags()) |
103
|
|
|
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) { |
104
|
|
|
return parent::max($column); |
105
|
|
|
}); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function min($column) |
109
|
|
|
{ |
110
|
|
|
if (! $this->isCachable) { |
111
|
|
|
return parent::min($column); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->cache($this->makeCacheTags()) |
115
|
|
|
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) { |
116
|
|
|
return parent::min($column); |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function pluck($column, $key = null) |
121
|
|
|
{ |
122
|
|
|
if (! $this->isCachable) { |
123
|
|
|
return parent::pluck($column, $key); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}"; |
127
|
|
|
|
128
|
|
|
if ($key) { |
129
|
|
|
$cacheKey .= "_{$key}"; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->cache($this->makeCacheTags()) |
133
|
|
|
->rememberForever($cacheKey, function () use ($column, $key) { |
134
|
|
|
return parent::pluck($column, $key); |
135
|
|
|
}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function sum($column) |
139
|
|
|
{ |
140
|
|
|
if (! $this->isCachable) { |
141
|
|
|
return parent::sum($column); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->cache($this->makeCacheTags()) |
145
|
|
|
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) { |
146
|
|
|
return parent::sum($column); |
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function value($column) |
151
|
|
|
{ |
152
|
|
|
if (! $this->isCachable) { |
153
|
|
|
return parent::value($column); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->cache($this->makeCacheTags()) |
157
|
|
|
->rememberForever($this->makeCacheKey() . "-value_{$column}", function () use ($column) { |
158
|
|
|
return parent::value($column); |
159
|
|
|
}); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|