Skip to content

Commit 0991c5f

Browse files
authored
Merge pull request #26 from GeneaLabs/laravel-5.5
WIP - code cleanup
2 parents 24f0d70 + 758ac2c commit 0991c5f

File tree

1 file changed

+35
-55
lines changed

1 file changed

+35
-55
lines changed

src/CachedBuilder.php

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,12 @@ protected function getWhereClauses(array $wheres = []) : string
9494
}
9595

9696
$value = array_get($where, 'value');
97-
98-
if (in_array($where['type'], ['In', 'Null', 'NotNull'])) {
99-
$value = strtolower($where['type']);
100-
}
101-
102-
if (is_array(array_get($where, 'values'))) {
103-
$value .= '_' . implode('_', $where['values']);
104-
}
97+
$value .= in_array($where['type'], ['In', 'Null', 'NotNull'])
98+
? strtolower($where['type'])
99+
: '';
100+
$value .= is_array(array_get($where, 'values'))
101+
? '_' . implode('_', $where['values'])
102+
: '';
105103

106104
return "{$carry}-{$where['column']}_{$value}";
107105
}) ?: '';
@@ -126,6 +124,16 @@ protected function getOrderByClauses(){
126124
});
127125
}
128126

127+
protected function getMethodKey(string $postfix = null) : string
128+
{
129+
return str_slug(get_class($this->model)) . $postfix;
130+
}
131+
132+
protected function getModelTag() : array
133+
{
134+
return [str_slug(get_class($this->model))];
135+
}
136+
129137
protected function getCacheTags() : array
130138
{
131139
return collect($this->eagerLoad)->keys()
@@ -152,33 +160,24 @@ protected function getCacheTags() : array
152160

153161
public function avg($column)
154162
{
155-
$tags = [str_slug(get_class($this->model))];
156-
$key = str_slug(get_class($this->model)) ."-avg_{$column}";
157-
158-
return $this->cache($tags)
159-
->rememberForever($key, function () use ($column) {
163+
return $this->cache($this->getModelTag())
164+
->rememberForever($this->getMethodKey("-avg_{$column}"), function () use ($column) {
160165
return parent::avg($column);
161166
});
162167
}
163168

164169
public function count($columns = ['*'])
165170
{
166-
$tags = [str_slug(get_class($this->model))];
167-
$key = str_slug(get_class($this->model)) ."-count";
168-
169-
return $this->cache($tags)
170-
->rememberForever($key, function () use ($columns) {
171+
return $this->cache($this->getModelTag())
172+
->rememberForever($this->getMethodKey("-count"), function () use ($columns) {
171173
return parent::count($columns);
172174
});
173175
}
174176

175177
public function cursor()
176178
{
177-
$tags = [str_slug(get_class($this->model))];
178-
$key = str_slug(get_class($this->model)) ."-cursor";
179-
180-
return $this->cache($tags)
181-
->rememberForever($key, function () {
179+
return $this->cache($this->getModelTag())
180+
->rememberForever($this->getMethodKey("-cursor"), function () {
182181
return collect(parent::cursor());
183182
});
184183
}
@@ -188,81 +187,62 @@ public function cursor()
188187
*/
189188
public function find($id, $columns = ['*'])
190189
{
191-
$tags = $this->getCacheTags();
192-
$key = $this->getCacheKey($columns, $id);
193-
194-
return $this->cache($tags)
195-
->rememberForever($key, function () use ($id, $columns) {
190+
return $this->cache($this->getCacheTags())
191+
->rememberForever($this->getCacheKey($columns, $id), function () use ($id, $columns) {
196192
return parent::find($id, $columns);
197193
});
198194
}
199195

200196
public function first($columns = ['*'])
201197
{
202-
$tags = $this->getCacheTags();
203-
$key = $this->getCacheKey($columns) . '-first';
204-
205-
return $this->cache($tags)
206-
->rememberForever($key, function () use ($columns) {
198+
return $this->cache($this->getCacheTags())
199+
->rememberForever($this->getCacheKey($columns) . '-first', function () use ($columns) {
207200
return parent::first($columns);
208201
});
209202
}
210203

211204
public function get($columns = ['*'])
212205
{
213-
$tags = $this->getCacheTags();
214-
$key = $this->getCacheKey($columns);
215-
216-
return $this->cache($tags)
217-
->rememberForever($key, function () use ($columns) {
206+
return $this->cache($this->getCacheTags())
207+
->rememberForever($this->getCacheKey($columns), function () use ($columns) {
218208
return parent::get($columns);
219209
});
220210
}
221211

222212
public function max($column)
223213
{
224-
$tags = [str_slug(get_class($this->model))];
225-
$key = str_slug(get_class($this->model)) ."-max_{$column}";
226-
227-
return $this->cache($tags)
228-
->rememberForever($key, function () use ($column) {
214+
return $this->cache($this->getModelTag())
215+
->rememberForever($this->getMethodKey("-max_{$column}"), function () use ($column) {
229216
return parent::max($column);
230217
});
231218
}
232219

233220
public function min($column)
234221
{
235-
$tags = [str_slug(get_class($this->model))];
236-
$key = str_slug(get_class($this->model)) ."-min_{$column}";
237-
238-
return $this->cache($tags)
239-
->rememberForever($key, function () use ($column) {
222+
return $this->cache($this->getModelTag())
223+
->rememberForever($this->getMethodKey("-min_{$column}"), function () use ($column) {
240224
return parent::min($column);
241225
});
242226
}
243227

244228
public function pluck($column, $key = null)
245229
{
246-
$tags = $this->getCacheTags();
247230
$cacheKey = $this->getCacheKey([$column]) . "-pluck_{$column}";
248231

249232
if ($key) {
250233
$cacheKey .= "_{$key}";
251234
}
252235

253-
return $this->cache($tags)
236+
return $this->cache($this->getCacheTags())
254237
->rememberForever($cacheKey, function () use ($column, $key) {
255238
return parent::pluck($column, $key);
256239
});
257240
}
258241

259242
public function sum($column)
260243
{
261-
$tags = [str_slug(get_class($this->model))];
262-
$key = str_slug(get_class($this->model)) ."-sum_{$column}";
263-
264-
return $this->cache($tags)
265-
->rememberForever($key, function () use ($column) {
244+
return $this->cache($this->getModelTag())
245+
->rememberForever($this->getMethodKey("-sum_{$column}"), function () use ($column) {
266246
return parent::sum($column);
267247
});
268248
}

0 commit comments

Comments
 (0)