Skip to content

Commit 9697ab7

Browse files
committed
Refactor Builder class
1 parent 6f8a5e6 commit 9697ab7

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

src/Builder.php renamed to src/CachedBuilder.php

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Support\Collection;
88
use Illuminate\Database\Eloquent\Relations\Relation;
99

10-
class Builder extends EloquentBuilder
10+
class CachedBuilder extends EloquentBuilder
1111
{
1212
protected function cache(array $tags = [])
1313
{
@@ -22,32 +22,72 @@ protected function cache(array $tags = [])
2222

2323
protected function getCacheKey(array $columns = ['*'], $idColumn = null) : string
2424
{
25-
$key = str_slug(get_class($this->model));
26-
$key .= $idColumn ? "_{$idColumn}" : '';
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->getOffsetClause();
31+
$key .= $this->getLimitClause();
2732

28-
if ($columns !== ['*']) {
29-
$key .= '_' . implode('_', $columns);
33+
return $key;
34+
}
35+
36+
protected function getIdColumn(string $idColumn) : string
37+
{
38+
return $idColumn ? "_{$idColumn}" : '';
39+
}
40+
41+
protected function getLimitClause() : string
42+
{
43+
if (! $this->query->limit) {
44+
return '';
3045
}
3146

32-
$key .= collect($this->query->wheres)->reduce(function ($carry, $where) {
33-
$value = $where['value'] ?? implode('_', $where['values']) ?? '';
47+
return "-limit_{$this->query->limit}";
48+
}
3449

35-
return "{$carry}-{$where['column']}_{$value}";
36-
});
50+
protected function getModelSlug() : string
51+
{
52+
return str_slug(get_class($this->model));
53+
}
3754

38-
if (collect($this->eagerLoad)->isNotEmpty()) {
39-
$key .= '-' . implode('-', collect($this->eagerLoad)->keys()->toArray());
55+
protected function getOffsetClause() : string
56+
{
57+
if (! $this->query->offset) {
58+
return '';
4059
}
4160

42-
if ($this->query->offset) {
43-
$key .= "-offset_{$this->query->offset}";
61+
return "-offset_{$this->query->offset}";
62+
}
63+
64+
protected function getQueryColumns(array $columns) : string
65+
{
66+
if ($columns === ['*'] || $columns === []) {
67+
return '';
4468
}
4569

46-
if ($this->query->limit) {
47-
$key .= "-limit_{$this->query->limit}";
70+
return '_' . implode('_', $columns);
71+
}
72+
73+
protected function getWhereClauses() : string
74+
{
75+
return collect($this->query->wheres)->reduce(function ($carry, $where) {
76+
$value = $where['value'] ?? implode('_', $where['values']) ?? '';
77+
78+
return "{$carry}-{$where['column']}_{$value}";
79+
}) ?: '';
80+
}
81+
82+
protected function getWithModels() : string
83+
{
84+
$eagerLoads = collect($this->eagerLoad);
85+
86+
if ($eagerLoads->isEmpty()) {
87+
return '';
4888
}
4989

50-
return $key;
90+
return '-' . implode('-', $eagerLoads->keys()->toArray());
5191
}
5292

5393
protected function getCacheTags() : array

src/CachedModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

3-
use GeneaLabs\LaravelModelCaching\Builder;
3+
use GeneaLabs\LaravelModelCaching\CachedBuilder as Builder;
44
use Illuminate\Cache\CacheManager;
55
use Illuminate\Cache\TaggableStore;
66
use Illuminate\Database\Eloquent\Model;

0 commit comments

Comments
 (0)