Skip to content

Commit 9edd775

Browse files
committed
Do some more refactoring
1 parent 5cae002 commit 9edd775

File tree

3 files changed

+57
-52
lines changed

3 files changed

+57
-52
lines changed

Diff for: src/CachedBuilder.php

+20-20
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ class CachedBuilder extends EloquentBuilder
1313

1414
public function avg($column)
1515
{
16-
return $this->cache($this->makeCacheTags())
17-
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
16+
return $this->cache($this->makeCacheTags($this))
17+
->rememberForever($this->makeCacheKey($this) . "-avg_{$column}", function () use ($column) {
1818
return parent::avg($column);
1919
});
2020
}
2121

2222
public function count($columns = ['*'])
2323
{
24-
return $this->cache($this->makeCacheTags())
25-
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
24+
return $this->cache($this->makeCacheTags($this))
25+
->rememberForever($this->makeCacheKey($this) . "-count", function () use ($columns) {
2626
return parent::count($columns);
2727
});
2828
}
2929

3030
public function cursor()
3131
{
32-
return $this->cache($this->makeCacheTags())
33-
->rememberForever($this->makeCacheKey() . "-cursor", function () {
32+
return $this->cache($this->makeCacheTags($this))
33+
->rememberForever($this->makeCacheKey($this) . "-cursor", function () {
3434
return collect(parent::cursor());
3535
});
3636
}
@@ -40,62 +40,62 @@ public function cursor()
4040
*/
4141
public function find($id, $columns = ['*'])
4242
{
43-
return $this->cache($this->makeCacheTags())
44-
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
43+
return $this->cache($this->makeCacheTags($this))
44+
->rememberForever($this->makeCacheKey($this, $columns, $id), function () use ($id, $columns) {
4545
return parent::find($id, $columns);
4646
});
4747
}
4848

4949
public function first($columns = ['*'])
5050
{
51-
return $this->cache($this->makeCacheTags())
52-
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
51+
return $this->cache($this->makeCacheTags($this))
52+
->rememberForever($this->makeCacheKey($this, $columns) . '-first', function () use ($columns) {
5353
return parent::first($columns);
5454
});
5555
}
5656

5757
public function get($columns = ['*'])
5858
{
59-
return $this->cache($this->makeCacheTags())
60-
->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
59+
return $this->cache($this->makeCacheTags($this))
60+
->rememberForever($this->makeCacheKey($this, $columns), function () use ($columns) {
6161
return parent::get($columns);
6262
});
6363
}
6464

6565
public function max($column)
6666
{
67-
return $this->cache($this->makeCacheTags())
68-
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
67+
return $this->cache($this->makeCacheTags($this))
68+
->rememberForever($this->makeCacheKey($this) . "-max_{$column}", function () use ($column) {
6969
return parent::max($column);
7070
});
7171
}
7272

7373
public function min($column)
7474
{
75-
return $this->cache($this->makeCacheTags())
76-
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
75+
return $this->cache($this->makeCacheTags($this))
76+
->rememberForever($this->makeCacheKey($this) . "-min_{$column}", function () use ($column) {
7777
return parent::min($column);
7878
});
7979
}
8080

8181
public function pluck($column, $key = null)
8282
{
83-
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
83+
$cacheKey = $this->makeCacheKey($this, [$column]) . "-pluck_{$column}";
8484

8585
if ($key) {
8686
$cacheKey .= "_{$key}";
8787
}
8888

89-
return $this->cache($this->makeCacheTags())
89+
return $this->cache($this->makeCacheTags($this))
9090
->rememberForever($cacheKey, function () use ($column, $key) {
9191
return parent::pluck($column, $key);
9292
});
9393
}
9494

9595
public function sum($column)
9696
{
97-
return $this->cache($this->makeCacheTags())
98-
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
97+
return $this->cache($this->makeCacheTags($this))
98+
->rememberForever($this->makeCacheKey($this) . "-sum_{$column}", function () use ($column) {
9999
return parent::sum($column);
100100
});
101101
}

Diff for: src/Traits/CacheKeyable.php

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
22

3+
use GeneaLabs\LaravelModelCaching\CachedBuilder;
34
use Illuminate\Support\Collection;
45

56
trait CacheKeyable
67
{
7-
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
8-
{
9-
$key = $this->getModelSlug();
8+
protected function makeCacheKey(
9+
CachedBuilder $builder,
10+
array $columns = ['*'],
11+
$idColumn = null
12+
) : string {
13+
$key = $this->getModelSlug($builder);
1014
$key .= $this->getIdColumn($idColumn ?: '');
1115
$key .= $this->getQueryColumns($columns);
12-
$key .= $this->getWhereClauses();
13-
$key .= $this->getWithModels();
14-
$key .= $this->getOrderByClauses();
15-
$key .= $this->getOffsetClause();
16-
$key .= $this->getLimitClause();
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);
1721

1822
return $key;
1923
}
@@ -23,32 +27,32 @@ protected function getIdColumn(string $idColumn) : string
2327
return $idColumn ? "_{$idColumn}" : '';
2428
}
2529

26-
protected function getLimitClause() : string
30+
protected function getLimitClause(CachedBuilder $builder) : string
2731
{
28-
if (! $this->query->limit) {
32+
if (! $builder->query->limit) {
2933
return '';
3034
}
3135

32-
return "-limit_{$this->query->limit}";
36+
return "-limit_{$builder->query->limit}";
3337
}
3438

35-
protected function getModelSlug() : string
39+
protected function getModelSlug(CachedBuilder $builder) : string
3640
{
37-
return str_slug(get_class($this->model));
41+
return str_slug(get_class($builder->model));
3842
}
3943

40-
protected function getOffsetClause() : string
44+
protected function getOffsetClause(CachedBuilder $builder) : string
4145
{
42-
if (! $this->query->offset) {
46+
if (! $builder->query->offset) {
4347
return '';
4448
}
4549

46-
return "-offset_{$this->query->offset}";
50+
return "-offset_{$builder->query->offset}";
4751
}
4852

49-
protected function getOrderByClauses() : string
53+
protected function getOrderByClauses(CachedBuilder $builder) : string
5054
{
51-
$orders = collect($this->query->orders);
55+
$orders = collect($builder->query->orders);
5256

5357
return $orders->reduce(function($carry, $order){
5458
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
@@ -79,12 +83,12 @@ protected function getValuesClause(array $where = null) : string
7983
: '';
8084
}
8185

82-
protected function getWhereClauses(array $wheres = []) : string
86+
protected function getWhereClauses(CachedBuilder $builder, array $wheres = []) : string
8387
{
84-
return $this->getWheres($wheres)
85-
->reduce(function ($carry, $where) {
88+
return $this->getWheres($builder, $wheres)
89+
->reduce(function ($carry, $where) use ($builder) {
8690
if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
87-
return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
91+
return '_' . strtolower($where['type']) . $this->getWhereClauses($builder, $where['query']->wheres);
8892
}
8993

9094
if ($where['type'] === 'Column') {
@@ -104,20 +108,20 @@ protected function getWhereClauses(array $wheres = []) : string
104108
. '';
105109
}
106110

107-
protected function getWheres(array $wheres) : Collection
111+
protected function getWheres(CachedBuilder $builder, array $wheres) : Collection
108112
{
109113
$wheres = collect($wheres);
110114

111115
if ($wheres->isEmpty()) {
112-
$wheres = collect($this->query->wheres);
116+
$wheres = collect($builder->query->wheres);
113117
}
114118

115119
return $wheres;
116120
}
117121

118-
protected function getWithModels() : string
122+
protected function getWithModels(CachedBuilder $builder) : string
119123
{
120-
$eagerLoads = collect($this->eagerLoad);
124+
$eagerLoads = collect($builder->eagerLoad);
121125

122126
if ($eagerLoads->isEmpty()) {
123127
return '';

Diff for: src/Traits/CacheTagable.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
22

3+
use GeneaLabs\LaravelModelCaching\CachedBuilder;
34
use Illuminate\Database\Eloquent\Relations\Relation;
45

56
trait CacheTagable
67
{
7-
public function makeCacheTags() : array
8+
public function makeCacheTags(CachedBuilder $builder) : array
89
{
9-
return collect($this->eagerLoad)
10+
return collect($builder->eagerLoad)
1011
->keys()
11-
->map(function ($relationName) {
12+
->map(function ($relationName) use ($builder) {
1213
$relation = collect(explode('.', $relationName))
13-
->reduce(function ($carry, $name) {
14+
->reduce(function ($carry, $name) use ($builder) {
1415
if (! $carry) {
15-
$carry = $this->model;
16+
$carry = $builder->model;
1617
}
1718

1819
if ($carry instanceof Relation) {
@@ -24,7 +25,7 @@ public function makeCacheTags() : array
2425

2526
return str_slug(get_class($relation->getQuery()->model));
2627
})
27-
->prepend(str_slug(get_class($this->model)))
28+
->prepend(str_slug(get_class($builder->model)))
2829
->values()
2930
->toArray();
3031
}

0 commit comments

Comments
 (0)