Skip to content

Commit 9599faf

Browse files
committed
Update model cache prefix functionality
1 parent 2eaef04 commit 9599faf

File tree

15 files changed

+152
-47
lines changed

15 files changed

+152
-47
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.5.0] - 20 May 2019
8+
### Changed
9+
- implementation of model-based cache prefix.
10+
711
## [0.4.24] - 18 May 2019
812
### Fixed
913
- BelongsToMany relationship to not cache if the target model is not also cachable.

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ Be sure to not require a specific version of this package when requiring it:
5050
composer require genealabs/laravel-model-caching:*
5151
```
5252

53+
## Upgrade Notes
54+
### 0.5.0
55+
The following implementations have changed (see the respective sections below):
56+
- model-specific cache prefix
57+
5358
## Configuration
5459
### Recommended (Optional) Custom Cache Store
5560
If you would like to use a different cache store than the default one used by
@@ -99,10 +104,6 @@ prefixing to keep cache entries separate for multi-tenant applications. For this
99104
it is recommended to add the Cachable trait to a base model, then set the cache
100105
key prefix config value there.
101106

102-
**Note that the config setting is included before the parent method is called,
103-
so that the setting is available in the parent as well. If you are developing a
104-
multi-tenant application, see the note above.**
105-
106107
Here's is an example:
107108
```php
108109
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
@@ -116,15 +117,16 @@ class BaseModel extends Model
116117
{
117118
use Cachable;
118119

119-
public function __construct($attributes = [])
120-
{
121-
config(['laravel-model-caching.cache-prefix' => 'test-prefix']);
122-
123-
parent::__construct($attributes);
124-
}
120+
protected $cachePrefix = "test-prefix";
125121
}
126122
```
127123

124+
The cache prefix can also be set in the configuration to prefix all cached
125+
models across the board:
126+
```php
127+
'cache-prefix' => 'test-prefix',
128+
```
129+
128130
### Exception: User Model
129131
I would not recommend caching the user model, as it is a special case, since it
130132
extends `Illuminate\Foundation\Auth\User`. Overriding that would break functionality.

src/CacheKey.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
44
use Illuminate\Support\Arr;
55
use Illuminate\Support\Collection;
6-
use Illuminate\Database\Query\Expression;
76
use Illuminate\Support\Str;
87

98
class CacheKey

src/Traits/CachePrefixing.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ trait CachePrefixing
44
{
55
protected function getCachePrefix() : string
66
{
7+
$cachePrefix = config("laravel-model-caching.cache-prefix", "");
8+
9+
if ($this->model
10+
&& property_exists($this->model, "cachePrefix")
11+
) {
12+
$cachePrefix = $this->model->cachePrefix;
13+
}
14+
15+
$cachePrefix = $cachePrefix
16+
? "{$cachePrefix}:"
17+
: "";
18+
719
return "genealabs:laravel-model-caching:"
820
. $this->getConnectionName() . ":"
921
. $this->getDatabaseName() . ":"
10-
. (config("laravel-model-caching.cache-prefix")
11-
? config("laravel-model-caching.cache-prefix", "") . ":"
12-
: "");
22+
. $cachePrefix;
1323
}
1424

1525
protected function getDatabaseName() : string

src/Traits/Caching.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,20 @@ public function flushCache(array $tags = [])
5757

5858
protected function getCachePrefix() : string
5959
{
60+
$cachePrefix = config("laravel-model-caching.cache-prefix", "");
61+
62+
if ($this->model
63+
&& property_exists($this->model, "cachePrefix")
64+
) {
65+
$cachePrefix = $this->model->cachePrefix;
66+
}
67+
68+
$cachePrefix = $cachePrefix
69+
? "{$cachePrefix}:"
70+
: "";
71+
6072
return "genealabs:laravel-model-caching:"
61-
. (config('laravel-model-caching.cache-prefix')
62-
? config('laravel-model-caching.cache-prefix', '') . ":"
63-
: "");
73+
. $cachePrefix;
6474
}
6575

6676
protected function makeCacheKey(

src/Traits/ModelCaching.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,33 @@
99

1010
trait ModelCaching
1111
{
12-
protected $cacheCooldownSeconds = 0;
12+
public function __get($key)
13+
{
14+
if ($key === "cachePrefix") {
15+
return $this->cachePrefix
16+
?? "";
17+
}
18+
19+
if ($key === "cacheCooldownSeconds") {
20+
return $this->cacheCooldownSeconds
21+
?? 0;
22+
}
23+
24+
return parent::__get($key);
25+
}
26+
27+
public function __set($key, $value)
28+
{
29+
if ($key === "cachePrefix") {
30+
$this->cachePrefix = $value;
31+
}
32+
33+
if ($key === "cacheCooldownSeconds") {
34+
$this->cacheCooldownSeconds = $value;
35+
}
36+
37+
parent::__set($key, $value);
38+
}
1339

1440
public static function all($columns = ['*'])
1541
{
@@ -151,9 +177,4 @@ public function scopeWithCacheCooldownSeconds(
151177

152178
return $query;
153179
}
154-
155-
public function getcacheCooldownSecondsAttribute() : int
156-
{
157-
return $this->cacheCooldownSeconds;
158-
}
159180
}

tests/Fixtures/BaseModel.php

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

tests/Fixtures/PrefixedAuthor.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,53 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
22

3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
34
use Illuminate\Database\Eloquent\Builder;
5+
use Illuminate\Database\Eloquent\Model;
46
use Illuminate\Database\Eloquent\Relations\HasMany;
57
use Illuminate\Database\Eloquent\Relations\HasOne;
8+
use Illuminate\Database\Eloquent\SoftDeletes;
69

7-
class PrefixedAuthor extends BaseModel
10+
class PrefixedAuthor extends Model
811
{
12+
use Cachable;
13+
use SoftDeletes;
14+
15+
protected $cachePrefix = "model-prefix";
16+
protected $casts = [
17+
"finances" => "array",
18+
];
919
protected $fillable = [
1020
'name',
1121
'email',
22+
"finances",
1223
];
1324
protected $table = "authors";
25+
26+
public function books() : HasMany
27+
{
28+
return $this->hasMany(Book::class);
29+
}
30+
31+
public function profile() : HasOne
32+
{
33+
return $this->hasOne(Profile::class);
34+
}
35+
36+
public function getLatestBookAttribute()
37+
{
38+
return $this
39+
->books()
40+
->latest("id")
41+
->first();
42+
}
43+
44+
public function scopeStartsWithA(Builder $query) : Builder
45+
{
46+
return $query->where('name', 'LIKE', 'A%');
47+
}
48+
49+
public function scopeNameStartsWith(Builder $query, string $startOfName) : Builder
50+
{
51+
return $query->where("name", "LIKE", "{$startOfName}%");
52+
}
1453
}

tests/Integration/CachedBuilder/MorphOneTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function setUp() : void
2323
public function testMorphTo()
2424
{
2525
$key1 = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-author_id_=_1-testing:{$this->testingSqlitePath}testing.sqlite:image");
26-
$key2 = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-author_id_=_2-testing:{$this->testingSqlitePath}testing.sqlite:image");
26+
$key2 = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-author_id_=_4-testing:{$this->testingSqlitePath}testing.sqlite:image");
2727
$tags = [
2828
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
2929
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesimage",
@@ -38,7 +38,7 @@ public function testMorphTo()
3838
->get($key1)['value'];
3939
$books2 = (new Book)
4040
->with("image")
41-
->where("author_id", 2)
41+
->where("author_id", 4)
4242
->get();
4343
$cachedResults2 = $this->cache()
4444
->tags($tags)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\PrefixedAuthor;
6+
7+
class PrefixTest extends IntegrationTestCase
8+
{
9+
public function testCachePrefixIsAddedForPrefixedModel()
10+
{
11+
$prefixKey = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:model-prefix:authors:genealabslaravelmodelcachingtestsfixturesprefixedauthor-first");
12+
$prefixTags = [
13+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:model-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor",
14+
];
15+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-first");
16+
$tags = [
17+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
18+
];
19+
20+
$prefixAuthor = (new PrefixedAuthor)
21+
->first();
22+
$author = (new Author)
23+
->first();
24+
$prefixCachedResults = $this
25+
->cache()
26+
->tags($prefixTags)
27+
->get($prefixKey)['value'];
28+
$nonPrefixCachedResults = $this
29+
->cache()
30+
->tags($tags)
31+
->get($key)['value'];
32+
33+
$this->assertEquals($prefixCachedResults, $prefixAuthor);
34+
$this->assertEquals($nonPrefixCachedResults, $author);
35+
$this->assertNotNull($author);
36+
}
37+
}

tests/Integration/CachedBuilderTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public function testDeletingModelClearsCache()
8989
$this->assertNull($results);
9090
}
9191

92-
/** @group test */
9392
public function testHasManyRelationshipIsCached()
9493
{
9594
$authors = (new Author)->with('books')->get();

tests/Integration/Console/Commands/FlushTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function testExtendedModelIsFlushed()
4646
$authors = (new PrefixedAuthor)
4747
->get();
4848

49-
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:test-prefix:authors:genealabslaravelmodelcachingtestsfixturesprefixedauthor");
50-
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor"];
49+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:model-prefix:authors:genealabslaravelmodelcachingtestsfixturesprefixedauthor");
50+
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:model-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor"];
5151

5252
$cachedResults = $this
5353
->cache

tests/Integration/Traits/CachableTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public function testSetCachePrefixAttribute()
5151
$results = $this->
5252
cache()
5353
->tags([
54-
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor",
54+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:model-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor",
5555
])
56-
->get(sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:test-prefix:authors:genealabslaravelmodelcachingtestsfixturesprefixedauthor"))['value'];
56+
->get(sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:model-prefix:authors:genealabslaravelmodelcachingtestsfixturesprefixedauthor"))['value'];
5757

5858
$this->assertNotNull($results);
5959
}

tests/database/baseline.sqlite

-8 KB
Binary file not shown.

tests/database/testing.sqlite

-8 KB
Binary file not shown.

0 commit comments

Comments
 (0)