Skip to content

Commit 44cd713

Browse files
committed
WiP - work on improved caching at a higher level
1 parent cecef4f commit 44cd713

File tree

2 files changed

+92
-11
lines changed

2 files changed

+92
-11
lines changed

src/Builder.php

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,24 @@
99

1010
class Builder extends EloquentBuilder
1111
{
12-
protected function eagerLoadRelation(array $models, $name, Closure $constraints)
12+
protected function cache(array $tags = [])
1313
{
14-
$relation = $this->getRelation($name);
15-
$relation->addEagerConstraints($models);
16-
$constraints($relation);
14+
$cache = cache();
1715

18-
return $this->cacheResults($relation, $models, $name);
16+
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
17+
$cache = $cache->tags($tags);
18+
}
19+
20+
return $cache;
1921
}
2022

2123
protected function cacheResults(Relation $relation, array $models, string $name) : array
2224
{
2325
$parentIds = implode('_', collect($models)->pluck('id')->toArray());
2426
$parentName = str_slug(get_class($relation->getParent()));
2527
$childName = str_slug(get_class($relation->getRelated()));
26-
$cache = cache();
2728

28-
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
29-
$cache = $cache->tags([$parentName, $childName]);
30-
}
31-
32-
$cachedResults = $cache->rememberForever(
29+
$cachedResults = $this->cache([$parentName, $childName])->rememberForever(
3330
"{$parentName}_{$parentIds}-{$childName}s",
3431
function () use ($relation, $models, $name) {
3532
return $relation->match(
@@ -42,4 +39,66 @@ function () use ($relation, $models, $name) {
4239

4340
return $cachedResults;
4441
}
42+
43+
protected function eagerLoadRelation(array $models, $name, Closure $constraints)
44+
{
45+
$relation = $this->getRelation($name);
46+
$relation->addEagerConstraints($models);
47+
$constraints($relation);
48+
49+
return $this->cacheResults($relation, $models, $name);
50+
}
51+
52+
/**
53+
* @SuppressWarnings(PHPMD.ShortVariable)
54+
*/
55+
public function find($id, $columns = ['*'])
56+
{
57+
$tag = str_slug(get_class($this->model));
58+
$key = "{$tag}_{$id}_" . implode('_', $columns);
59+
60+
return $this->cache([$tag])
61+
->rememberForever($key, function () use ($id, $columns) {
62+
return parent::find($id, $columns);
63+
});
64+
}
65+
66+
public function count($columns = '*')
67+
{
68+
$tag = str_slug(get_class($this->model));
69+
$key = "{$tag}_" . implode('_', $columns);
70+
71+
return $this->cache([$tag])
72+
->rememberForever($key, function () use ($id, $columns) {
73+
return parent::count($columns);
74+
});
75+
}
76+
77+
public function first($columns = ['*'])
78+
{
79+
$tag = str_slug(get_class($this->model));
80+
$key = "{$tag}_" . implode('_', $columns);
81+
82+
return $this->cache([$tag])
83+
->rememberForever($key, function () use ($id, $columns) {
84+
return parent::first($columns);
85+
});
86+
}
87+
88+
public function get($columns = ['*'])
89+
{
90+
$tag = str_slug(get_class($this->model));
91+
$key = "{$tag}_" . implode('_', $columns);
92+
$key .= collect($this->query->wheres)->reduce(function ($carry, $where) {
93+
$value = $where['value'] ?? implode('_', $where['values']) ?? '';
94+
95+
return "{$carry}-{$where['column']}_{$value}";
96+
});
97+
$key .= '-' . implode('-', collect($this->eagerLoad)->keys()->toArray());
98+
99+
return $this->cache([$tag])
100+
->rememberForever($key, function () use ($columns) {
101+
return parent::get($columns);
102+
});
103+
}
45104
}

tests/Unit/CacheTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,26 @@ public function testHasOneRelationshipIsCached()
176176
$this->assertNotEmpty($results);
177177
$this->assertEquals($authors->count(), $results->count());
178178
}
179+
180+
// test get()
181+
182+
// test find()
183+
184+
// test all()
185+
186+
// test count()
187+
188+
// test chunk()
189+
190+
// test cursor()
191+
192+
// test max()
193+
194+
// test min()
195+
196+
// test avg()
197+
198+
// test value()
199+
200+
// test pluck()
179201
}

0 commit comments

Comments
 (0)