Skip to content

Commit e7a6bb0

Browse files
committed
Add functionality to support cross-database model relationships
1 parent e70754a commit e7a6bb0

File tree

12 files changed

+55
-45
lines changed

12 files changed

+55
-45
lines changed

src/CacheKey.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Query\Builder;
66
use Illuminate\Support\Collection;
77
use Illuminate\Database\Query\Expression;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
89

910
class CacheKey
1011
{
@@ -41,7 +42,7 @@ public function make(
4142
$key .= $this->getOffsetClause();
4243
$key .= $this->getLimitClause();
4344
$key .= $keyDifferentiator;
44-
// dump($key);
45+
4546
return $key;
4647
}
4748

@@ -307,6 +308,16 @@ protected function getWithModels() : string
307308
return "";
308309
}
309310

310-
return "-" . implode("-", $eagerLoads->keys()->toArray());
311+
return $eagerLoads->keys()->reduce(function ($carry, $related) {
312+
if (! method_exists($this->model, $related)) {
313+
return "{$carry}-{$related}";
314+
}
315+
316+
$relatedModel = $this->model->$related()->getRelated();
317+
$relatedConnection = $relatedModel->getConnection()->getName();
318+
$relatedDatabase = $relatedModel->getConnection()->getDatabaseName();
319+
320+
return "{$carry}-{$relatedConnection}:{$relatedDatabase}:{$related}";
321+
});
311322
}
312323
}

src/CacheTags.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function make() : array
3737
->prepend($this->getTagName())
3838
->values()
3939
->toArray();
40-
// dump($tags);
40+
4141
return $tags;
4242
}
4343

@@ -63,6 +63,7 @@ protected function getRelation(string $relationName) : Relation
6363

6464
protected function getTagName() : string
6565
{
66-
return $this->getCachePrefix() . str_slug(get_class($this->model));
66+
return $this->getCachePrefix()
67+
. str_slug(get_class($this->model));
6768
}
6869
}

src/Traits/CachePrefixing.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ trait CachePrefixing
55
protected function getCachePrefix() : string
66
{
77
return "genealabs:laravel-model-caching:"
8-
. $this->getDatabaseConnectionName() . ":"
8+
. $this->getConnectionName() . ":"
99
. $this->getDatabaseName() . ":"
1010
. (config("laravel-model-caching.cache-prefix")
1111
? config("laravel-model-caching.cache-prefix", "") . ":"
1212
: "");
1313
}
1414

15-
protected function getDatabaseConnectionName() : string
15+
protected function getDatabaseName() : string
1616
{
17-
return $this->query->connection->getName();
17+
return $this->query->connection->getDatabaseName();
1818
}
1919

20-
protected function getDatabaseName() : string
20+
protected function getConnectionName() : string
2121
{
22-
return $this->query->connection->getDatabaseName();
22+
return $this->model->getConnection()->getName();
2323
}
2424
}

src/Traits/Caching.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function checkCooldownAndRemoveIfExpired(Model $instance)
138138

139139
$instance
140140
->cache()
141-
->forget("{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at");
141+
->forget("{$cachePrefix}:{$modelClassName}-cooldown:seconds");
142142
$instance
143143
->cache()
144144
->forget("{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at");

tests/Integration/CachedBuilder/GetTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
class GetTest extends IntegrationTestCase
1919
{
20-
21-
2220
public function testGetModelResultsCreatesCache()
2321
{
2422
$authors = (new Author)->with('books', 'profile')
2523
->get();
26-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-profile');
24+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-testing::memory::profile');
2725
$tags = [
2826
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
2927
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -34,7 +32,7 @@ public function testGetModelResultsCreatesCache()
3432
->get($key)['value'];
3533
$liveResults = (new UncachedAuthor)->with('books', 'profile')
3634
->get();
37-
35+
// dd($cachedResults);
3836
$this->assertEquals($authors, $cachedResults);
3937
$this->assertEmpty($liveResults->diffKeys($cachedResults));
4038
}

tests/Integration/CachedBuilder/WhereInRawTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class WhereInRawTest extends IntegrationTestCase
1919
{
2020
public function testWhereInRawUsingRelationship()
2121
{
22-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books');
22+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books');
2323
$tags = [
2424
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
2525
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',

tests/Integration/CachedBuilder/WithTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testMultiLevelWithQuery()
6363

6464
public function testWithBelongsToManyRelationshipQuery()
6565
{
66-
$key = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-books.id_=_3-stores-first');
66+
$key = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-books.id_=_3-testing::memory::stores-first');
6767
$tags = [
6868
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
6969
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesstore',

tests/Integration/CachedBuilderRelationshipsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testHasRelationshipResults()
2323
->with("stores")
2424
->has("stores")
2525
->get();
26-
$key = "genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.id_=_book_store.book_id-stores";
26+
$key = "genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.id_=_book_store.book_id-testing::memory::stores";
2727
$tags = [
2828
"genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook",
2929
"genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesstore",

tests/Integration/CachedBuilderTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testCacheIsEmptyBeforeLoadingModels()
2626
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
2727
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook'
2828
])
29-
->get('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books');
29+
->get('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books');
3030

3131
$this->assertNull($results);
3232
}
@@ -39,7 +39,7 @@ public function testCacheIsNotEmptyAfterLoadingModels()
3939
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
4040
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook'
4141
])
42-
->get(sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books'));
42+
->get(sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books'));
4343

4444
$this->assertNotNull($results);
4545
}
@@ -105,7 +105,7 @@ public function testHasManyRelationshipIsCached()
105105
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
106106
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook'
107107
])
108-
->get(sha1("genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books"))['value']);
108+
->get(sha1("genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books"))['value']);
109109

110110
$this->assertNotNull($results);
111111
$this->assertEmpty($authors->diffKeys($results));
@@ -122,7 +122,7 @@ public function testBelongsToRelationshipIsCached()
122122
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
123123
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'
124124
])
125-
->get(sha1("genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-author"))['value']);
125+
->get(sha1("genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-testing::memory::author"))['value']);
126126

127127
$this->assertNotNull($results);
128128
$this->assertEmpty($books->diffKeys($results));
@@ -139,7 +139,7 @@ public function testBelongsToManyRelationshipIsCached()
139139
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
140140
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesstore'
141141
])
142-
->get(sha1("genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-stores"))['value']);
142+
->get(sha1("genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-testing::memory::stores"))['value']);
143143

144144
$this->assertNotNull($results);
145145
$this->assertEmpty($books->diffKeys($results));
@@ -157,7 +157,7 @@ public function testHasOneRelationshipIsCached()
157157
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
158158
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesprofile'
159159
])
160-
->get(sha1("genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-profile"))['value']);
160+
->get(sha1("genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::profile"))['value']);
161161

162162
$this->assertNotNull($results);
163163
$this->assertEmpty($authors->diffKeys($results));
@@ -170,7 +170,7 @@ public function testAvgModelResultsCreatesCache()
170170
{
171171
$authorId = (new Author)->with('books', 'profile')
172172
->avg('id');
173-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-profile-avg_id');
173+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-testing::memory::profile-avg_id');
174174
$tags = [
175175
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
176176
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -224,7 +224,7 @@ public function testCountModelResultsCreatesCache()
224224
$authors = (new Author)
225225
->with('books', 'profile')
226226
->count();
227-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-profile-count');
227+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-testing::memory::profile-count');
228228
$tags = [
229229
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
230230
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -246,7 +246,7 @@ public function testCountWithStringCreatesCache()
246246
$authors = (new Author)
247247
->with('books', 'profile')
248248
->count("id");
249-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor_id-books-profile-count');
249+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor_id-testing::memory::books-testing::memory::profile-count');
250250
$tags = [
251251
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
252252
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -287,7 +287,7 @@ public function testMaxModelResultsCreatesCache()
287287
{
288288
$authorId = (new Author)->with('books', 'profile')
289289
->max('id');
290-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-profile-max_id');
290+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-testing::memory::profile-max_id');
291291
$tags = [
292292
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
293293
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -307,7 +307,7 @@ public function testMinModelResultsCreatesCache()
307307
{
308308
$authorId = (new Author)->with('books', 'profile')
309309
->min('id');
310-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-profile-min_id');
310+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-testing::memory::profile-min_id');
311311
$tags = [
312312
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
313313
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -327,7 +327,7 @@ public function testPluckModelResultsCreatesCache()
327327
{
328328
$authors = (new Author)->with('books', 'profile')
329329
->pluck('name', 'id');
330-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-pluck_name_id');
330+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor_name-testing::memory::books-testing::memory::profile-pluck_name_id');
331331
$tags = [
332332
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
333333
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -347,7 +347,7 @@ public function testSumModelResultsCreatesCache()
347347
{
348348
$authorId = (new Author)->with('books', 'profile')
349349
->sum('id');
350-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-profile-sum_id');
350+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-testing::memory::profile-sum_id');
351351
$tags = [
352352
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
353353
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -367,7 +367,7 @@ public function testValueModelResultsCreatesCache()
367367
{
368368
$authorName = (new Author)->with('books', 'profile')
369369
->value('name');
370-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-profile-value_name');
370+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-testing::memory::profile-value_name');
371371
$tags = [
372372
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
373373
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -388,7 +388,7 @@ public function testNestedRelationshipEagerLoading()
388388
$authors = collect([(new Author)->with('books.publisher')
389389
->first()]);
390390

391-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher-first');
391+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-testing::memory::books.publisher-first');
392392
$tags = [
393393
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
394394
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
@@ -466,7 +466,7 @@ public function testNestedRelationshipWhereClauseParsing()
466466
->with('books.publisher')
467467
->get();
468468

469-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher');
469+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books-books.publisher');
470470
$tags = [
471471
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
472472
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',

tests/Integration/CachedModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testWhereHasIsBeingCached()
118118
})
119119
->get();
120120

121-
$key = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.author_id_=_authors.id-id_=_1-author');
121+
$key = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.author_id_=_authors.id-id_=_1-testing::memory::author');
122122
$tags = [
123123
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
124124
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',

tests/Integration/Console/Commands/FlushTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testExtendedModelIsFlushed()
6969
public function testGivenModelWithRelationshipIsFlushed()
7070
{
7171
$authors = (new Author)->with('books')->get();
72-
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-books');
72+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-testing::memory::books');
7373
$tags = [
7474
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
7575
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',

0 commit comments

Comments
 (0)