Skip to content

Commit d8b8492

Browse files
committed
Refactor traits out to classes
1 parent 9edd775 commit d8b8492

File tree

4 files changed

+105
-78
lines changed

4 files changed

+105
-78
lines changed

src/Traits/CacheKeyable.php renamed to src/CacheKey.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
1+
<?php namespace GeneaLabs\LaravelModelCaching;
22

3-
use GeneaLabs\LaravelModelCaching\CachedBuilder;
3+
use Illuminate\Database\Eloquent\Model;
4+
use Illuminate\Database\Query\Builder;
45
use Illuminate\Support\Collection;
56

6-
trait CacheKeyable
7+
class CacheKey
78
{
8-
protected function makeCacheKey(
9-
CachedBuilder $builder,
10-
array $columns = ['*'],
11-
$idColumn = null
12-
) : string {
13-
$key = $this->getModelSlug($builder);
9+
protected $eagerLoad;
10+
protected $model;
11+
protected $query;
12+
13+
public function __construct(array $eagerLoad, Model $model, Builder $query)
14+
{
15+
$this->eagerLoad = $eagerLoad;
16+
$this->model = $model;
17+
$this->query = $query;
18+
}
19+
20+
public function make(array $columns = ['*'], $idColumn = null) : string
21+
{
22+
$key = $this->getModelSlug();
1423
$key .= $this->getIdColumn($idColumn ?: '');
1524
$key .= $this->getQueryColumns($columns);
16-
$key .= $this->getWhereClauses($builder);
17-
$key .= $this->getWithModels($builder);
18-
$key .= $this->getOrderByClauses($builder);
19-
$key .= $this->getOffsetClause($builder);
20-
$key .= $this->getLimitClause($builder);
25+
$key .= $this->getWhereClauses();
26+
$key .= $this->getWithModels();
27+
$key .= $this->getOrderByClauses();
28+
$key .= $this->getOffsetClause();
29+
$key .= $this->getLimitClause();
2130

2231
return $key;
2332
}
@@ -27,32 +36,32 @@ protected function getIdColumn(string $idColumn) : string
2736
return $idColumn ? "_{$idColumn}" : '';
2837
}
2938

30-
protected function getLimitClause(CachedBuilder $builder) : string
39+
protected function getLimitClause() : string
3140
{
32-
if (! $builder->query->limit) {
41+
if (! $this->query->limit) {
3342
return '';
3443
}
3544

36-
return "-limit_{$builder->query->limit}";
45+
return "-limit_{$this->query->limit}";
3746
}
3847

39-
protected function getModelSlug(CachedBuilder $builder) : string
48+
protected function getModelSlug() : string
4049
{
41-
return str_slug(get_class($builder->model));
50+
return str_slug(get_class($this->model));
4251
}
4352

44-
protected function getOffsetClause(CachedBuilder $builder) : string
53+
protected function getOffsetClause() : string
4554
{
46-
if (! $builder->query->offset) {
55+
if (! $this->query->offset) {
4756
return '';
4857
}
4958

50-
return "-offset_{$builder->query->offset}";
59+
return "-offset_{$this->query->offset}";
5160
}
5261

53-
protected function getOrderByClauses(CachedBuilder $builder) : string
62+
protected function getOrderByClauses() : string
5463
{
55-
$orders = collect($builder->query->orders);
64+
$orders = collect($this->query->orders);
5665

5766
return $orders->reduce(function($carry, $order){
5867
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
@@ -83,12 +92,12 @@ protected function getValuesClause(array $where = null) : string
8392
: '';
8493
}
8594

86-
protected function getWhereClauses(CachedBuilder $builder, array $wheres = []) : string
95+
protected function getWhereClauses(array $wheres = []) : string
8796
{
88-
return $this->getWheres($builder, $wheres)
89-
->reduce(function ($carry, $where) use ($builder) {
97+
return $this->getWheres($wheres)
98+
->reduce(function ($carry, $where) {
9099
if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
91-
return '_' . strtolower($where['type']) . $this->getWhereClauses($builder, $where['query']->wheres);
100+
return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
92101
}
93102

94103
if ($where['type'] === 'Column') {
@@ -108,20 +117,20 @@ protected function getWhereClauses(CachedBuilder $builder, array $wheres = []) :
108117
. '';
109118
}
110119

111-
protected function getWheres(CachedBuilder $builder, array $wheres) : Collection
120+
protected function getWheres(array $wheres) : Collection
112121
{
113122
$wheres = collect($wheres);
114123

115124
if ($wheres->isEmpty()) {
116-
$wheres = collect($builder->query->wheres);
125+
$wheres = collect($this->query->wheres);
117126
}
118127

119128
return $wheres;
120129
}
121130

122-
protected function getWithModels(CachedBuilder $builder) : string
131+
protected function getWithModels() : string
123132
{
124-
$eagerLoads = collect($builder->eagerLoad);
133+
$eagerLoads = collect($this->eagerLoad);
125134

126135
if ($eagerLoads->isEmpty()) {
127136
return '';

src/CacheTags.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching;
2+
3+
use GeneaLabs\LaravelModelCaching\CachedBuilder;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\Relation;
6+
7+
class CacheTags
8+
{
9+
protected $eagerLoad;
10+
protected $model;
11+
12+
public function __construct(array $eagerLoad, Model $model)
13+
{
14+
$this->eagerLoad = $eagerLoad;
15+
$this->model = $model;
16+
}
17+
18+
public function make() : array
19+
{
20+
return collect($this->eagerLoad)
21+
->keys()
22+
->map(function ($relationName) {
23+
$relation = collect(explode('.', $relationName))
24+
->reduce(function ($carry, $name) {
25+
if (! $carry) {
26+
$carry = $this->model;
27+
}
28+
29+
if ($carry instanceof Relation) {
30+
$carry = $carry->getQuery()->getModel();
31+
}
32+
33+
return $carry->{$name}();
34+
});
35+
36+
return str_slug(get_class($relation->getQuery()->getModel()));
37+
})
38+
->prepend(str_slug(get_class($this->model)))
39+
->values()
40+
->toArray();
41+
}
42+
}

src/CachedBuilder.php

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

33
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4-
use GeneaLabs\LaravelModelCaching\Traits\CacheKeyable;
5-
use GeneaLabs\LaravelModelCaching\Traits\CacheTagable;
64
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
75

86
class CachedBuilder extends EloquentBuilder
97
{
108
use Cachable;
11-
use CacheKeyable;
12-
use CacheTagable;
139

1410
public function avg($column)
1511
{
1612
return $this->cache($this->makeCacheTags($this))
17-
->rememberForever($this->makeCacheKey($this) . "-avg_{$column}", function () use ($column) {
13+
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
1814
return parent::avg($column);
1915
});
2016
}
2117

2218
public function count($columns = ['*'])
2319
{
2420
return $this->cache($this->makeCacheTags($this))
25-
->rememberForever($this->makeCacheKey($this) . "-count", function () use ($columns) {
21+
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
2622
return parent::count($columns);
2723
});
2824
}
2925

3026
public function cursor()
3127
{
3228
return $this->cache($this->makeCacheTags($this))
33-
->rememberForever($this->makeCacheKey($this) . "-cursor", function () {
29+
->rememberForever($this->makeCacheKey() . "-cursor", function () {
3430
return collect(parent::cursor());
3531
});
3632
}
@@ -41,46 +37,46 @@ public function cursor()
4137
public function find($id, $columns = ['*'])
4238
{
4339
return $this->cache($this->makeCacheTags($this))
44-
->rememberForever($this->makeCacheKey($this, $columns, $id), function () use ($id, $columns) {
40+
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
4541
return parent::find($id, $columns);
4642
});
4743
}
4844

4945
public function first($columns = ['*'])
5046
{
5147
return $this->cache($this->makeCacheTags($this))
52-
->rememberForever($this->makeCacheKey($this, $columns) . '-first', function () use ($columns) {
48+
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
5349
return parent::first($columns);
5450
});
5551
}
5652

5753
public function get($columns = ['*'])
5854
{
5955
return $this->cache($this->makeCacheTags($this))
60-
->rememberForever($this->makeCacheKey($this, $columns), function () use ($columns) {
56+
->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
6157
return parent::get($columns);
6258
});
6359
}
6460

6561
public function max($column)
6662
{
6763
return $this->cache($this->makeCacheTags($this))
68-
->rememberForever($this->makeCacheKey($this) . "-max_{$column}", function () use ($column) {
64+
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
6965
return parent::max($column);
7066
});
7167
}
7268

7369
public function min($column)
7470
{
7571
return $this->cache($this->makeCacheTags($this))
76-
->rememberForever($this->makeCacheKey($this) . "-min_{$column}", function () use ($column) {
72+
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
7773
return parent::min($column);
7874
});
7975
}
8076

8177
public function pluck($column, $key = null)
8278
{
83-
$cacheKey = $this->makeCacheKey($this, [$column]) . "-pluck_{$column}";
79+
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
8480

8581
if ($key) {
8682
$cacheKey .= "_{$key}";
@@ -95,8 +91,20 @@ public function pluck($column, $key = null)
9591
public function sum($column)
9692
{
9793
return $this->cache($this->makeCacheTags($this))
98-
->rememberForever($this->makeCacheKey($this) . "-sum_{$column}", function () use ($column) {
94+
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
9995
return parent::sum($column);
10096
});
10197
}
98+
99+
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
100+
{
101+
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
102+
->make($columns, $idColumn);
103+
}
104+
105+
protected function makeCacheTags() : array
106+
{
107+
return (new CacheTags($this->eagerLoad, $this->model))
108+
->make();
109+
}
102110
}

src/Traits/CacheTagable.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)