Skip to content

Laravel 5.5 #13

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 4 commits into from
Oct 12, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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.6] - 2017-10-12
### Added
- orderBy clause to cache key. Thanks @RobMKR for the PR!

## [0.2.5] - 2017-10-04
### Fixed
- parsing of nested, exists, raw, and column where clauses.
Expand Down
9 changes: 9 additions & 0 deletions src/CachedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function getCacheKey(array $columns = ['*'], $idColumn = null) : strin
$key .= $this->getQueryColumns($columns);
$key .= $this->getWhereClauses();
$key .= $this->getWithModels();
$key .= $this->getOrderByClauses();
$key .= $this->getOffsetClause();
$key .= $this->getLimitClause();

Expand Down Expand Up @@ -117,6 +118,14 @@ protected function getWithModels() : string
return '-' . implode('-', $eagerLoads->keys()->toArray());
}

protected function getOrderByClauses(){
$orders = collect($this->query->orders);

return $orders->reduce(function($carry, $order){
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
});
}

protected function getCacheTags() : array
{
return collect($this->eagerLoad)->keys()
Expand Down
18 changes: 17 additions & 1 deletion tests/Unit/CachedBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function testChunkModelResultsCreatesCache()
}

$cachedChunks['authors']->push($chunk);
$cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile{$offset}-limit_3");
$cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile_orderBy_authors.id_asc{$offset}-limit_3");
});

(new UncachedAuthor)->with('books', 'profile')
Expand Down Expand Up @@ -464,6 +464,22 @@ public function testLazyLoadingOnResourceIsCached()
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
}

public function testOrderByClauseParsing()
{
$authors = (new Author)->orderBy('name')->get();

$key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc';
$tags = [
'genealabslaravelmodelcachingtestsfixturesauthor',
];

$cachedResults = cache()->tags($tags)->get($key);
$liveResults = (new UncachedAuthor)->orderBy('name')->get();

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

public function testNestedRelationshipWhereClauseParsing()
{
// -> with('modelA.modelB')
Expand Down