Skip to content

Commit b4e003d

Browse files
committed
Add connection name to cache prefix
Fixes #99
1 parent e9210ff commit b4e003d

File tree

11 files changed

+202
-181
lines changed

11 files changed

+202
-181
lines changed

src/CacheKey.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

3+
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
34
use Illuminate\Database\Eloquent\Model;
45
use Illuminate\Database\Query\Builder;
56
use Illuminate\Support\Collection;
67

78
class CacheKey
89
{
10+
use CachePrefixing;
11+
912
protected $eagerLoad;
1013
protected $model;
1114
protected $query;
1215

13-
protected function getCachePrefix() : string
14-
{
15-
return "genealabs:laravel-model-caching:"
16-
. (config("laravel-model-caching.cache-prefix")
17-
? config("laravel-model-caching.cache-prefix", "") . ":"
18-
: "");
19-
}
20-
2116
public function __construct(
2217
array $eagerLoad,
2318
Model $model,

src/CacheTags.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

33
use GeneaLabs\LaravelModelCaching\CachedBuilder;
4+
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
45
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Database\Eloquent\Relations\Relation;
7+
use Illuminate\Database\Query\Builder;
68

79
class CacheTags
810
{
11+
use CachePrefixing;
12+
913
protected $eagerLoad;
1014
protected $model;
15+
protected $query;
1116

12-
public function __construct(array $eagerLoad, Model $model)
13-
{
17+
public function __construct(
18+
array $eagerLoad,
19+
Model $model,
20+
Builder $query
21+
) {
1422
$this->eagerLoad = $eagerLoad;
1523
$this->model = $model;
16-
}
17-
18-
protected function getCachePrefix() : string
19-
{
20-
return "genealabs:laravel-model-caching:"
21-
. (config('laravel-model-caching.cache-prefix')
22-
? config('laravel-model-caching.cache-prefix', '') . ":"
23-
: "");
24+
$this->query = $query;
2425
}
2526

2627
public function make() : array

src/Console/Commands/Flush.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ protected function flushModelCache(string $option) : int
4949
return 0;
5050
}
5151

52-
protected function getAllTraitsUsedByClass(string $classname, bool $autoload = true) : Collection
53-
{
52+
protected function getAllTraitsUsedByClass(
53+
string $classname,
54+
bool $autoload = true
55+
) : Collection {
5456
$traits = collect();
5557

5658
if (class_exists($classname, $autoload)) {
@@ -60,7 +62,8 @@ protected function getAllTraitsUsedByClass(string $classname, bool $autoload = t
6062
$parentClass = get_parent_class($classname);
6163

6264
if ($parentClass) {
63-
$traits = $traits->merge($this->getAllTraitsUsedByClass($parentClass, $autoload));
65+
$traits = $traits
66+
->merge($this->getAllTraitsUsedByClass($parentClass, $autoload));
6467
}
6568

6669
return $traits;

src/Traits/CachePrefixing.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2+
3+
trait CachePrefixing
4+
{
5+
protected function getCachePrefix() : string
6+
{
7+
return "genealabs:laravel-model-caching:"
8+
. $this->getDatabaseConnectionName() . ":"
9+
. (config("laravel-model-caching.cache-prefix")
10+
? config("laravel-model-caching.cache-prefix", "") . ":"
11+
: "");
12+
}
13+
14+
protected function getDatabaseConnectionName() : string
15+
{
16+
return $this->query->connection->getName();
17+
}
18+
}

src/Traits/Caching.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ protected function makeCacheKey(
7777

7878
protected function makeCacheTags() : array
7979
{
80-
$tags = (new CacheTags($this->eagerLoad ?? [], $this->model ?? $this))
80+
$eagerLoad = $this->eagerLoad ?? [];
81+
$model = $this->model ?? $this;
82+
$query = $this->query ?? app(Builder::class);
83+
84+
$tags = (new CacheTags($eagerLoad, $model, $query))
8185
->make();
8286

8387
return $tags;

tests/Integration/CachedBuilderPaginationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public function testPaginationIsCached()
2727
$authors = (new Author)
2828
->paginate(3);
2929

30-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
30+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
3131
$tags = [
32-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
32+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
3333
];
3434

3535
$cachedResults = $this->cache()

tests/Integration/CachedBuilderTest.php

Lines changed: 115 additions & 115 deletions
Large diffs are not rendered by default.

tests/Integration/CachedModelTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class CachedModelTest extends IntegrationTestCase
2121
public function testAllModelResultsCreatesCache()
2222
{
2323
$authors = (new Author)->all();
24-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
24+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
2525
$tags = [
26-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
26+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
2727
];
2828

2929
$cachedResults = $this
@@ -39,8 +39,8 @@ public function testAllModelResultsCreatesCache()
3939

4040
public function testScopeDisablesCaching()
4141
{
42-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
43-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
42+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
43+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
4444
$authors = (new Author)
4545
->where("name", "Bruno")
4646
->disableCache()
@@ -56,8 +56,8 @@ public function testScopeDisablesCaching()
5656

5757
public function testScopeDisablesCachingWhenCalledOnModel()
5858
{
59-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
60-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
59+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
60+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
6161
$authors = (new PrefixedAuthor)
6262
->disableCache()
6363
->where("name", "Bruno")
@@ -74,8 +74,8 @@ public function testScopeDisablesCachingWhenCalledOnModel()
7474
public function testScopeDisableCacheDoesntCrashWhenCachingIsDisabledInConfig()
7575
{
7676
config(['laravel-model-caching.disabled' => true]);
77-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
78-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
77+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
78+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
7979
$authors = (new PrefixedAuthor)
8080
->where("name", "Bruno")
8181
->disableCache()
@@ -94,9 +94,9 @@ public function testAllMethodCachingCanBeDisabledViaConfig()
9494
config(['laravel-model-caching.disabled' => true]);
9595
$authors = (new Author)
9696
->all();
97-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
97+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
9898
$tags = [
99-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
99+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
100100
];
101101
config(['laravel-model-caching.disabled' => false]);
102102

@@ -119,10 +119,10 @@ public function testWhereHasIsBeingCached()
119119
})
120120
->get();
121121

122-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook_exists_and_books.author_id_=_authors.id-id_=_1-author');
122+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook_exists_and_books.author_id_=_authors.id-id_=_1-author');
123123
$tags = [
124-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook',
125-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
124+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook',
125+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
126126
];
127127

128128
$cachedResults = $this

tests/Integration/Console/Commands/FlushTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class FlushTest extends IntegrationTestCase
1717
public function testGivenModelIsFlushed()
1818
{
1919
$authors = (new Author)->all();
20-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
21-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
20+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
21+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
2222

2323
$cachedResults = $this->cache
2424
->tags($tags)
@@ -38,8 +38,8 @@ public function testExtendedModelIsFlushed()
3838
$authors = (new PrefixedAuthor)
3939
->get();
4040

41-
$key = sha1('genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor');
42-
$tags = ['genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'];
41+
$key = sha1('genealabs:laravel-model-caching:testing:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor');
42+
$tags = ['genealabs:laravel-model-caching:testing:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'];
4343

4444
$cachedResults = $this
4545
->cache
@@ -59,10 +59,10 @@ public function testExtendedModelIsFlushed()
5959
public function testGivenModelWithRelationshipIsFlushed()
6060
{
6161
$authors = (new Author)->with('books')->get();
62-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor-books');
62+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-books');
6363
$tags = [
64-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
65-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook',
64+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
65+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook',
6666
];
6767

6868
$cachedResults = $this->cache
@@ -97,18 +97,18 @@ public function testAllModelsAreFlushed()
9797
(new Book)->all();
9898
(new Store)->all();
9999

100-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
101-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
100+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
101+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
102102
$cachedAuthors = $this->cache
103103
->tags($tags)
104104
->get($key)['value'];
105-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook');
106-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook'];
105+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook');
106+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook'];
107107
$cachedBooks = $this->cache
108108
->tags($tags)
109109
->get($key)['value'];
110-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore');
111-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore'];
110+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore');
111+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore'];
112112
$cachedStores = $this->cache
113113
->tags($tags)
114114
->get($key)['value'];
@@ -119,18 +119,18 @@ public function testAllModelsAreFlushed()
119119

120120
$this->artisan('modelCache:flush');
121121

122-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
123-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
122+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
123+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
124124
$cachedAuthors = $this->cache
125125
->tags($tags)
126126
->get($key)['value'];
127-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook');
128-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook'];
127+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook');
128+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook'];
129129
$cachedBooks = $this->cache
130130
->tags($tags)
131131
->get($key)['value'];
132-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore');
133-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore'];
132+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore');
133+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore'];
134134
$cachedStores = $this->cache
135135
->tags($tags)
136136
->get($key)['value'];

tests/Integration/DisabledCachedBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ public function testPaginationIsCached()
315315
->disableCache()
316316
->paginate(3);
317317

318-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
318+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
319319
$tags = [
320-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
320+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
321321
];
322322

323323
$cachedResults = $this->cache()

tests/Integration/Traits/CachableTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public function testSpecifyingAlternateCacheDriver()
2626
// TODO: make sure the alternate cache is actually loaded
2727
config(['cache.stores' => $configCacheStores]);
2828
config(['laravel-model-caching.store' => 'customCache']);
29-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
30-
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
29+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
30+
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
3131

3232
$authors = (new Author)
3333
->all();
@@ -53,9 +53,9 @@ public function testSetCachePrefixAttribute()
5353
$results = $this->
5454
cache()
5555
->tags([
56-
'genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor',
56+
'genealabs:laravel-model-caching:testing:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor',
5757
])
58-
->get(sha1('genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'))['value'];
58+
->get(sha1('genealabs:laravel-model-caching:testing:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'))['value'];
5959

6060
$this->assertNotNull($results);
6161
}
@@ -69,9 +69,9 @@ public function testAllReturnsCollection()
6969
$cachedResults = $this
7070
->cache()
7171
->tags([
72-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
72+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
7373
])
74-
->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
74+
->get(sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
7575
$liveResults = (new UncachedAuthor)->all();
7676

7777
$this->assertInstanceOf(Collection::class, $authors);
@@ -87,9 +87,9 @@ public function testsCacheFlagDisablesCaching()
8787
$cachedAuthors = $this
8888
->cache()
8989
->tags([
90-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
90+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
9191
])
92-
->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'));
92+
->get(sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'));
9393

9494
config(['laravel-model-caching.disabled' => false]);
9595

0 commit comments

Comments
 (0)