Skip to content

Commit 5cae002

Browse files
committed
Refactor methods out into traits, fix cache tags and keys for aggregate functions
1 parent 1e808ab commit 5cae002

File tree

5 files changed

+236
-202
lines changed

5 files changed

+236
-202
lines changed

src/CachedBuilder.php

Lines changed: 26 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -1,175 +1,36 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

3-
use Closure;
4-
use Illuminate\Cache\TaggableStore;
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use GeneaLabs\LaravelModelCaching\Traits\CacheKeyable;
5+
use GeneaLabs\LaravelModelCaching\Traits\CacheTagable;
56
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;
97

108
class CachedBuilder extends EloquentBuilder
119
{
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 getCacheKey(array $columns = ['*'], $idColumn = null) : string
24-
{
25-
$key = $this->getModelSlug();
26-
$key .= $this->getIdColumn($idColumn ?: '');
27-
$key .= $this->getQueryColumns($columns);
28-
$key .= $this->getWhereClauses();
29-
$key .= $this->getWithModels();
30-
$key .= $this->getOrderByClauses();
31-
$key .= $this->getOffsetClause();
32-
$key .= $this->getLimitClause();
33-
34-
return $key;
35-
}
36-
37-
protected function getIdColumn(string $idColumn) : string
38-
{
39-
40-
return $idColumn ? "_{$idColumn}" : '';
41-
}
42-
43-
protected function getLimitClause() : string
44-
{
45-
if (! $this->query->limit) {
46-
return '';
47-
}
48-
49-
return "-limit_{$this->query->limit}";
50-
}
51-
52-
protected function getModelSlug() : string
53-
{
54-
return str_slug(get_class($this->model));
55-
}
56-
57-
protected function getOffsetClause() : string
58-
{
59-
if (! $this->query->offset) {
60-
return '';
61-
}
62-
63-
return "-offset_{$this->query->offset}";
64-
}
65-
66-
protected function getQueryColumns(array $columns) : string
67-
{
68-
if ($columns === ['*'] || $columns === []) {
69-
return '';
70-
}
71-
72-
return '_' . implode('_', $columns);
73-
}
74-
75-
protected function getWhereClauses(array $wheres = []) : string
76-
{
77-
return $this->getWheres($wheres)
78-
->reduce(function ($carry, $where) {
79-
if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
80-
return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
81-
}
82-
83-
if ($where['type'] === 'Column') {
84-
return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}";
85-
}
86-
87-
if ($where['type'] === 'raw') {
88-
return "_{$where['boolean']}_" . str_slug($where['sql']);
89-
}
90-
91-
$value = array_get($where, 'value');
92-
$value .= $this->getTypeClause($where);
93-
$value .= $this->getValuesClause($where);
94-
95-
return "{$carry}-{$where['column']}_{$value}";
96-
})
97-
. '';
98-
}
99-
100-
protected function getWithModels() : string
101-
{
102-
$eagerLoads = collect($this->eagerLoad);
103-
104-
if ($eagerLoads->isEmpty()) {
105-
return '';
106-
}
107-
108-
return '-' . implode('-', $eagerLoads->keys()->toArray());
109-
}
110-
111-
protected function getOrderByClauses(){
112-
$orders = collect($this->query->orders);
113-
114-
return $orders->reduce(function($carry, $order){
115-
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
116-
});
117-
}
118-
119-
protected function getMethodKey(string $postfix = null) : string
120-
{
121-
return str_slug(get_class($this->model)) . $postfix;
122-
}
123-
124-
protected function getModelTag() : array
125-
{
126-
return [str_slug(get_class($this->model))];
127-
}
128-
129-
protected function getCacheTags() : array
130-
{
131-
return collect($this->eagerLoad)->keys()
132-
->map(function ($relationName) {
133-
$relation = collect(explode('.', $relationName))
134-
->reduce(function ($carry, $name) {
135-
if (! $carry) {
136-
$carry = $this->model;
137-
}
138-
139-
if ($carry instanceof Relation) {
140-
$carry = $carry->getQuery()->model;
141-
}
142-
143-
return $carry->{$name}();
144-
});
145-
146-
return str_slug(get_class($relation->getQuery()->model));
147-
})
148-
->prepend(str_slug(get_class($this->model)))
149-
->values()
150-
->toArray();
151-
}
10+
use Cachable;
11+
use CacheKeyable;
12+
use CacheTagable;
15213

15314
public function avg($column)
15415
{
155-
return $this->cache($this->getModelTag())
156-
->rememberForever($this->getMethodKey("-avg_{$column}"), function () use ($column) {
16+
return $this->cache($this->makeCacheTags())
17+
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
15718
return parent::avg($column);
15819
});
15920
}
16021

16122
public function count($columns = ['*'])
16223
{
163-
return $this->cache($this->getModelTag())
164-
->rememberForever($this->getMethodKey("-count"), function () use ($columns) {
24+
return $this->cache($this->makeCacheTags())
25+
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
16526
return parent::count($columns);
16627
});
16728
}
16829

16930
public function cursor()
17031
{
171-
return $this->cache($this->getModelTag())
172-
->rememberForever($this->getMethodKey("-cursor"), function () {
32+
return $this->cache($this->makeCacheTags())
33+
->rememberForever($this->makeCacheKey() . "-cursor", function () {
17334
return collect(parent::cursor());
17435
});
17536
}
@@ -179,88 +40,63 @@ public function cursor()
17940
*/
18041
public function find($id, $columns = ['*'])
18142
{
182-
return $this->cache($this->getCacheTags())
183-
->rememberForever($this->getCacheKey($columns, $id), function () use ($id, $columns) {
43+
return $this->cache($this->makeCacheTags())
44+
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
18445
return parent::find($id, $columns);
18546
});
18647
}
18748

18849
public function first($columns = ['*'])
18950
{
190-
return $this->cache($this->getCacheTags())
191-
->rememberForever($this->getCacheKey($columns) . '-first', function () use ($columns) {
51+
return $this->cache($this->makeCacheTags())
52+
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
19253
return parent::first($columns);
19354
});
19455
}
19556

19657
public function get($columns = ['*'])
19758
{
198-
return $this->cache($this->getCacheTags())
199-
->rememberForever($this->getCacheKey($columns), function () use ($columns) {
59+
return $this->cache($this->makeCacheTags())
60+
->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
20061
return parent::get($columns);
20162
});
20263
}
20364

20465
public function max($column)
20566
{
206-
return $this->cache($this->getModelTag())
207-
->rememberForever($this->getMethodKey("-max_{$column}"), function () use ($column) {
67+
return $this->cache($this->makeCacheTags())
68+
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
20869
return parent::max($column);
20970
});
21071
}
21172

21273
public function min($column)
21374
{
214-
return $this->cache($this->getModelTag())
215-
->rememberForever($this->getMethodKey("-min_{$column}"), function () use ($column) {
75+
return $this->cache($this->makeCacheTags())
76+
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
21677
return parent::min($column);
21778
});
21879
}
21980

22081
public function pluck($column, $key = null)
22182
{
222-
$cacheKey = $this->getCacheKey([$column]) . "-pluck_{$column}";
83+
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
22384

22485
if ($key) {
22586
$cacheKey .= "_{$key}";
22687
}
22788

228-
return $this->cache($this->getCacheTags())
89+
return $this->cache($this->makeCacheTags())
22990
->rememberForever($cacheKey, function () use ($column, $key) {
23091
return parent::pluck($column, $key);
23192
});
23293
}
23394

23495
public function sum($column)
23596
{
236-
return $this->cache($this->getModelTag())
237-
->rememberForever($this->getMethodKey("-sum_{$column}"), function () use ($column) {
97+
return $this->cache($this->makeCacheTags())
98+
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
23899
return parent::sum($column);
239100
});
240101
}
241-
242-
protected function getTypeClause($where)
243-
{
244-
return in_array($where['type'], ['In', 'Null', 'NotNull'])
245-
? strtolower($where['type'])
246-
: '';
247-
}
248-
249-
protected function getValuesClause($where)
250-
{
251-
return is_array(array_get($where, 'values'))
252-
? '_' . implode('_', $where['values'])
253-
: '';
254-
}
255-
256-
protected function getWheres(array $wheres) : Collection
257-
{
258-
$wheres = collect($wheres);
259-
260-
if ($wheres->isEmpty()) {
261-
$wheres = collect($this->query->wheres);
262-
}
263-
264-
return $wheres;
265-
}
266102
}

src/Traits/Cachable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2+
3+
use Illuminate\Cache\TaggableStore;
4+
5+
trait Cachable
6+
{
7+
protected function cache(array $tags = [])
8+
{
9+
$cache = cache();
10+
11+
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
12+
$cache = $cache->tags($tags);
13+
}
14+
15+
return $cache;
16+
}
17+
}

0 commit comments

Comments
 (0)