Skip to content

Commit fd5f2b2

Browse files
committed
Add database name to cache keys and tags for multi-tenancy
Fixes #120
1 parent 789493c commit fd5f2b2

13 files changed

+176
-170
lines changed

src/Traits/CachePrefixing.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ protected function getCachePrefix() : string
66
{
77
return "genealabs:laravel-model-caching:"
88
. $this->getDatabaseConnectionName() . ":"
9+
. $this->getDatabaseName() . ":"
910
. (config("laravel-model-caching.cache-prefix")
1011
? config("laravel-model-caching.cache-prefix", "") . ":"
1112
: "");
@@ -15,4 +16,9 @@ protected function getDatabaseConnectionName() : string
1516
{
1617
return $this->query->connection->getName();
1718
}
19+
20+
protected function getDatabaseName() : string
21+
{
22+
return $this->query->connection->getDatabaseName();
23+
}
1824
}

tests/Integration/CachedBuilder/FindOrFailTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function testFindOrFailCachesModels()
2424
$author = (new Author)
2525
->findOrFail(1);
2626

27-
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-find_1');
27+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor-find_1');
2828
$tags = [
29-
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
29+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
3030
];
3131

3232
$cachedResults = $this->cache()

tests/Integration/CachedBuilder/FindTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class FindTest extends IntegrationTestCase
2222
public function testFindModelResultsCreatesCache()
2323
{
2424
$author = collect()->push((new Author)->find(1));
25-
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor_1');
25+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor_1');
2626
$tags = [
27-
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
27+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
2828
];
2929

3030
$cachedResults = collect()->push($this->cache()->tags($tags)

tests/Integration/CachedBuilder/WhereRawTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function testRawWhereClauseParsing()
2525
->whereRaw('name <> \'\'')
2626
->first()]);
2727

28-
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor_and_name-first');
29-
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
28+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor_and_name-first');
29+
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'];
3030

3131
$cachedResults = collect([$this->cache()->tags($tags)->get($key)['value']]);
3232

@@ -45,8 +45,8 @@ public function testWhereRawWithQueryParameters()
4545
->whereRaw("name != 'test3'")
4646
->whereRaw('name = ? AND name != ?', [$authorName, "test2"])
4747
->get();
48-
$key = sha1("genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthorname_!=_test_and_name_!=_'test3'_and_name_=_Guido_Feest__AND_name_!=_test2");
49-
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
48+
$key = sha1("genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthorname_!=_test_and_name_!=_'test3'_and_name_=_Guido_Feest__AND_name_!=_test2");
49+
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'];
5050

5151
$cachedResults = collect([$this->cache()->tags($tags)->get($key)['value']]);
5252
$liveResults = (new UncachedAuthor)

tests/Integration/CachedBuilder/WhereTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public function testColumnsRelationshipWhereClauseParsing()
4545
$authors = (new Author)
4646
->where('name', '=', $author->name)
4747
->get();
48-
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-name_=_' .
48+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor-name_=_' .
4949
$author->name);
50-
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
50+
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'];
5151

5252
$cachedResults = $this->cache()
5353
->tags($tags)
@@ -67,14 +67,14 @@ private function processWhereClauseTestWithOperator(string $operator)
6767
->where('name', $operator, $author->name)
6868
->get();
6969
$keyParts = [
70-
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-name',
70+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor-name',
7171
'_',
7272
str_replace(' ', '_', strtolower($operator)),
7373
'_',
7474
$author->name,
7575
];
7676
$key = sha1(implode('', $keyParts));
77-
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
77+
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'];
7878

7979
$cachedResults = $this->cache()
8080
->tags($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:testing:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
30+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
3131
$tags = [
32-
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
32+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
3333
];
3434

3535
$cachedResults = $this

tests/Integration/CachedBuilderRelationshipsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function testHasRelationshipResults()
2525
->with("stores")
2626
->has("stores")
2727
->get();
28-
$key = "genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.id_=_book_store.book_id-stores";
28+
$key = "genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.id_=_book_store.book_id-stores";
2929
$tags = [
30-
"genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook",
31-
"genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore",
30+
"genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook",
31+
"genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesstore",
3232
];
3333
$cachedResults = $this
3434
->cache()

0 commit comments

Comments
 (0)