From 2923ea57cbcbcc868770628b04505c2a6e8603f2 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Wed, 27 Sep 2017 19:02:42 -0700 Subject: [PATCH 1/4] Add test to check that non-eagerloaded relationships will also be cached --- tests/Unit/CachedBuilderTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 8f8552f..69efb66 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -438,4 +438,19 @@ public function testNestedRelationshipEagerloading() $this->assertEmpty($authors->diffAssoc($cachedResults)); $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } + + public function testNonEagerloadedRelationshipResolvesThroughCachedBuilder() + { + $books = (new Author)->first()->books; + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_'; + $tags = [ + 'genealabslaravelmodelcachingtestsfixturesbook', + ]; + + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor)->first()->books; + + $this->assertEmpty($books->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); + } } From 5f76c1935f163ae6dcb60def1d279a8dc2fe13ce Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Wed, 27 Sep 2017 19:32:09 -0700 Subject: [PATCH 2/4] Add test to check that lazy loading is also cached in resource objects --- tests/Fixtures/Http/Resources/Author.php | 14 ++++++++++++++ tests/Unit/CachedBuilderTest.php | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/Http/Resources/Author.php diff --git a/tests/Fixtures/Http/Resources/Author.php b/tests/Fixtures/Http/Resources/Author.php new file mode 100644 index 0000000..c598f77 --- /dev/null +++ b/tests/Fixtures/Http/Resources/Author.php @@ -0,0 +1,14 @@ + $this->name, + 'books' => $this->books, + ]; + } +} diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 69efb66..fc6e802 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -10,6 +10,7 @@ use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile; use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher; use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore; +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource; use GeneaLabs\LaravelModelCaching\Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -53,7 +54,7 @@ public function testCacheIsEmptyBeforeLoadingModels() 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook' ]) - ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); + ->get('genealabslaravelmodelcachingtestsfixturesauthor-books'); $this->assertNull($results); } @@ -453,4 +454,20 @@ public function testNonEagerloadedRelationshipResolvesThroughCachedBuilder() $this->assertEmpty($books->diffAssoc($cachedResults)); $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } + + public function testLazyLoadingOnResourceIsCached() + { + $books = (new AuthorResource((new Author)->first()))->books; + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_'; + $tags = [ + 'genealabslaravelmodelcachingtestsfixturesbook', + ]; + + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor)->first()->books; + + $this->assertEmpty($books->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); + } + } From 76a825abd7802bebc074bcaeca7232587bf8cfcb Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Fri, 29 Sep 2017 17:50:32 -0700 Subject: [PATCH 3/4] Fix generation of cache keys for queries with where clauses --- src/CachedBuilder.php | 2 +- tests/Unit/CachedBuilderTest.php | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index 9f53bf0..7166e50 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -76,7 +76,7 @@ protected function getWhereClauses() : string $value = $where['value'] ?? implode('_', ($where['values'] ?? [])); if (! $value) { - return ''; + return $carry . ''; } return "{$carry}-{$where['column']}_{$value}"; diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index fc6e802..d8e104b 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -440,10 +440,10 @@ public function testNestedRelationshipEagerloading() $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } - public function testNonEagerloadedRelationshipResolvesThroughCachedBuilder() + public function testLazyLoadedRelationshipResolvesThroughCachedBuilder() { $books = (new Author)->first()->books; - $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_'; + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1'; $tags = [ 'genealabslaravelmodelcachingtestsfixturesbook', ]; @@ -458,7 +458,7 @@ public function testNonEagerloadedRelationshipResolvesThroughCachedBuilder() public function testLazyLoadingOnResourceIsCached() { $books = (new AuthorResource((new Author)->first()))->books; - $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_'; + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1'; $tags = [ 'genealabslaravelmodelcachingtestsfixturesbook', ]; @@ -469,5 +469,4 @@ public function testLazyLoadingOnResourceIsCached() $this->assertEmpty($books->diffAssoc($cachedResults)); $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } - } From b655aec8c20157cbcad0c710b4e924ccbb7e0517 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Fri, 29 Sep 2017 17:56:09 -0700 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9a483a..e35c299 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.2.1] - 2017-09-29 +### Added +- additional unit tests for checking caching of lazy-loaded relationships. + +### Fixed +- generation of cache key for queries with where clauses. + ## [0.2.0] - 2017-09-24 ### Changed - approach to caching things. Completely rewrote the CachedBuilder class.