Skip to content

Commit e3356a5

Browse files
committed
Add more tests
1 parent c428899 commit e3356a5

File tree

7 files changed

+436
-182
lines changed

7 files changed

+436
-182
lines changed

src/Builder.php

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,83 +20,128 @@ protected function cache(array $tags = [])
2020
return $cache;
2121
}
2222

23-
protected function cacheResults(Relation $relation, array $models, string $name) : array
23+
// protected function cacheResults(Relation $relation, array $models, string $name) : array
24+
// {
25+
// $parentIds = implode('_', collect($models)->pluck('id')->toArray());
26+
// $parentName = str_slug(get_class($relation->getParent()));
27+
// $childName = str_slug(get_class($relation->getRelated()));
28+
//
29+
// $cachedResults = $this->cache([$parentName, $childName])->rememberForever(
30+
// "{$parentName}_{$parentIds}-{$childName}s",
31+
// function () use ($relation, $models, $name) {
32+
// return $relation->match(
33+
// $relation->initRelation($models, $name),
34+
// $relation->getEager(),
35+
// $name
36+
// );
37+
// }
38+
// );
39+
//
40+
// return $cachedResults;
41+
// }
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+
protected function getCacheKey(array $columns = ['*'], $ids = null) : string
2453
{
25-
$parentIds = implode('_', collect($models)->pluck('id')->toArray());
26-
$parentName = str_slug(get_class($relation->getParent()));
27-
$childName = str_slug(get_class($relation->getRelated()));
28-
29-
$cachedResults = $this->cache([$parentName, $childName])->rememberForever(
30-
"{$parentName}_{$parentIds}-{$childName}s",
31-
function () use ($relation, $models, $name) {
32-
return $relation->match(
33-
$relation->initRelation($models, $name),
34-
$relation->getEager(),
35-
$name
36-
);
37-
}
38-
);
39-
40-
return $cachedResults;
54+
$key = str_slug(get_class($this->model));
55+
56+
if ($ids) {
57+
$key .= '_' . (is_array($ids)
58+
? implode('_', $ids)
59+
: $ids);
60+
}
61+
62+
if ($columns !== ['*']) {
63+
$key .= '_' . implode('_', $columns);
64+
}
65+
66+
$key .= collect($this->query->wheres)->reduce(function ($carry, $where) {
67+
$value = $where['value'] ?? implode('_', $where['values']) ?? '';
68+
69+
return "{$carry}-{$where['column']}_{$value}";
70+
});
71+
72+
if (collect($this->eagerLoad)->isNotEmpty()) {
73+
$key .= '-' . implode('-', collect($this->eagerLoad)->keys()->toArray());
74+
}
75+
76+
if ($this->query->offset) {
77+
$key .= "-offset_{$this->query->offset}";
78+
}
79+
80+
if ($this->query->limit) {
81+
$key .= "-limit_{$this->query->limit}";
82+
}
83+
84+
return $key;
4185
}
4286

43-
protected function eagerLoadRelation(array $models, $name, Closure $constraints)
87+
protected function getCacheTags() : array
4488
{
45-
$relation = $this->getRelation($name);
46-
$relation->addEagerConstraints($models);
47-
$constraints($relation);
89+
return collect($this->eagerLoad)->keys()
90+
->map(function ($name) {
91+
return str_slug(get_class(
92+
$this->model
93+
->{$name}()
94+
->getQuery()
95+
->model
96+
));
97+
})
98+
->prepend(str_slug(get_class($this->model)))
99+
->values()
100+
->toArray();
101+
}
48102

49-
return $this->cacheResults($relation, $models, $name);
103+
public function count($columns = ['*'])
104+
{
105+
$tags = [str_slug(get_class($this->model))];
106+
$key = str_slug(get_class($this->model)) ."-count";
107+
108+
return $this->cache($tags)
109+
->rememberForever($key, function () use ($columns) {
110+
return parent::count($columns);
111+
});
50112
}
51113

52114
/**
53115
* @SuppressWarnings(PHPMD.ShortVariable)
54116
*/
55117
public function find($id, $columns = ['*'])
56118
{
57-
$tag = str_slug(get_class($this->model));
58-
$key = "{$tag}_{$id}_" . implode('_', $columns);
119+
$tags = $this->getCacheTags();
120+
$key = $this->getCacheKey($columns, $id);
59121

60-
return $this->cache([$tag])
122+
return $this->cache($tags)
61123
->rememberForever($key, function () use ($id, $columns) {
62124
return parent::find($id, $columns);
63125
});
64126
}
65127

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-
77128
public function first($columns = ['*'])
78129
{
79-
$tag = str_slug(get_class($this->model));
80-
$key = "{$tag}_" . implode('_', $columns);
130+
$tags = $this->getCacheTags();
131+
$key = $this->getCacheKey($columns);
81132

82-
return $this->cache([$tag])
83-
->rememberForever($key, function () use ($id, $columns) {
133+
return $this->cache($tags)
134+
->rememberForever($key, function () use ($columns) {
84135
return parent::first($columns);
85136
});
86137
}
87138

88139
public function get($columns = ['*'])
89140
{
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());
141+
$tags = $this->getCacheTags();
142+
$key = $this->getCacheKey($columns);
98143

99-
return $this->cache([$tag])
144+
return $this->cache($tags)
100145
->rememberForever($key, function () use ($columns) {
101146
return parent::get($columns);
102147
});

src/CachedModel.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use LogicException;
99

1010
use Illuminate\Cache\TaggedCache;
11+
use Illuminate\Support\Collection;
1112

1213
abstract class CachedModel extends Model
1314
{
@@ -55,4 +56,17 @@ public function flushCache(array $tags = [])
5556
{
5657
$this->cache($tags)->flush();
5758
}
59+
60+
public static function all($columns = ['*'])
61+
{
62+
$class = get_called_class();
63+
$instance = new $class;
64+
$tags = [str_slug(get_called_class())];
65+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
66+
67+
return $instance->cache($tags)
68+
->rememberForever($key, function () use ($columns) {
69+
return parent::all($columns);
70+
});
71+
}
5872
}

tests/Fixtures/UncachedAuthor.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use Illuminate\Database\Eloquent\Model;
4+
use Illuminate\Database\Eloquent\Relations\HasMany;
5+
use Illuminate\Database\Eloquent\Relations\HasOne;
6+
7+
class UncachedAuthor extends Model
8+
{
9+
protected $fillable = [
10+
'name',
11+
'email',
12+
];
13+
protected $table = 'authors';
14+
15+
public function books() : HasMany
16+
{
17+
return $this->hasMany(UncachedBook::class, 'author_id', 'id');
18+
}
19+
20+
public function profile() : HasOne
21+
{
22+
return $this->hasOne(UncachedProfile::class, 'author_id', 'id');
23+
}
24+
}

tests/Fixtures/UncachedBook.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\CachedModel;
4+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
5+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6+
7+
class UncachedBook extends CachedModel
8+
{
9+
protected $dates = [
10+
'published_at',
11+
];
12+
protected $fillable = [
13+
'description',
14+
'published_at',
15+
'title',
16+
];
17+
protected $table = 'books';
18+
19+
public function author() : BelongsTo
20+
{
21+
return $this->belongsTo(UncachedAuthor::class);
22+
}
23+
24+
public function stores() : BelongsToMany
25+
{
26+
return $this->belongsToMany(UncachedStore::class);
27+
}
28+
}

tests/Fixtures/UncachedProfile.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\CachedModel;
4+
use Illuminate\Database\Eloquent\Relations\HasMany;
5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
7+
class UncachedProfile extends CachedModel
8+
{
9+
protected $fillable = [
10+
'first_name',
11+
'last_name',
12+
];
13+
protected $table = 'profiles';
14+
15+
public function author() : BelongsTo
16+
{
17+
return $this->belongsTo(UnachedAuthor::class);
18+
}
19+
}

tests/Fixtures/UncachedStore.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\CachedModel;
4+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
5+
6+
class UncachedStore extends CachedModel
7+
{
8+
protected $fillable = [
9+
'address',
10+
'name',
11+
];
12+
protected $table = 'stores';
13+
14+
public function books() : BelongsToMany
15+
{
16+
return $this->belongsToMany(UncachedBook::class);
17+
}
18+
}

0 commit comments

Comments
 (0)