Skip to content

Commit f31d607

Browse files
authored
Merge pull request #3 from GeneaLabs/laravel-5.5
Fix Cache Keys for Queries with Where Clauses
2 parents 420192d + b655aec commit f31d607

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ 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.2.1] - 2017-09-29
8+
### Added
9+
- additional unit tests for checking caching of lazy-loaded relationships.
10+
11+
### Fixed
12+
- generation of cache key for queries with where clauses.
13+
714
## [0.2.0] - 2017-09-24
815
### Changed
916
- approach to caching things. Completely rewrote the CachedBuilder class.

src/CachedBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function getWhereClauses() : string
7676
$value = $where['value'] ?? implode('_', ($where['values'] ?? []));
7777

7878
if (! $value) {
79-
return '';
79+
return $carry . '';
8080
}
8181

8282
return "{$carry}-{$where['column']}_{$value}";
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources;
2+
3+
use Illuminate\Http\Resources\Json\Resource;
4+
5+
class Author extends Resource
6+
{
7+
public function toArray($request)
8+
{
9+
return [
10+
'name' => $this->name,
11+
'books' => $this->books,
12+
];
13+
}
14+
}

tests/Unit/CachedBuilderTest.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
1111
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
1212
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
13+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource;
1314
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
1415
use Illuminate\Foundation\Testing\RefreshDatabase;
1516

@@ -53,7 +54,7 @@ public function testCacheIsEmptyBeforeLoadingModels()
5354
'genealabslaravelmodelcachingtestsfixturesauthor',
5455
'genealabslaravelmodelcachingtestsfixturesbook'
5556
])
56-
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
57+
->get('genealabslaravelmodelcachingtestsfixturesauthor-books');
5758

5859
$this->assertNull($results);
5960
}
@@ -438,4 +439,34 @@ public function testNestedRelationshipEagerloading()
438439
$this->assertEmpty($authors->diffAssoc($cachedResults));
439440
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
440441
}
442+
443+
public function testLazyLoadedRelationshipResolvesThroughCachedBuilder()
444+
{
445+
$books = (new Author)->first()->books;
446+
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1';
447+
$tags = [
448+
'genealabslaravelmodelcachingtestsfixturesbook',
449+
];
450+
451+
$cachedResults = cache()->tags($tags)->get($key);
452+
$liveResults = (new UncachedAuthor)->first()->books;
453+
454+
$this->assertEmpty($books->diffAssoc($cachedResults));
455+
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
456+
}
457+
458+
public function testLazyLoadingOnResourceIsCached()
459+
{
460+
$books = (new AuthorResource((new Author)->first()))->books;
461+
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1';
462+
$tags = [
463+
'genealabslaravelmodelcachingtestsfixturesbook',
464+
];
465+
466+
$cachedResults = cache()->tags($tags)->get($key);
467+
$liveResults = (new UncachedAuthor)->first()->books;
468+
469+
$this->assertEmpty($books->diffAssoc($cachedResults));
470+
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
471+
}
441472
}

0 commit comments

Comments
 (0)