Skip to content

Fix Cache Keys for Queries with Where Clauses #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/CachedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function getWhereClauses() : string
$value = $where['value'] ?? implode('_', ($where['values'] ?? []));

if (! $value) {
return '';
return $carry . '';
}

return "{$carry}-{$where['column']}_{$value}";
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/Http/Resources/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class Author extends Resource
{
public function toArray($request)
{
return [
'name' => $this->name,
'books' => $this->books,
];
}
}
33 changes: 32 additions & 1 deletion tests/Unit/CachedBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -438,4 +439,34 @@ public function testNestedRelationshipEagerloading()
$this->assertEmpty($authors->diffAssoc($cachedResults));
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
}

public function testLazyLoadedRelationshipResolvesThroughCachedBuilder()
{
$books = (new Author)->first()->books;
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1';
$tags = [
'genealabslaravelmodelcachingtestsfixturesbook',
];

$cachedResults = cache()->tags($tags)->get($key);
$liveResults = (new UncachedAuthor)->first()->books;

$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';
$tags = [
'genealabslaravelmodelcachingtestsfixturesbook',
];

$cachedResults = cache()->tags($tags)->get($key);
$liveResults = (new UncachedAuthor)->first()->books;

$this->assertEmpty($books->diffAssoc($cachedResults));
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
}
}