Skip to content

Commit

Permalink
Merge pull request #13 from GeneaLabs/laravel-5.5
Browse files Browse the repository at this point in the history
Laravel 5.5
  • Loading branch information
mikebronner committed Oct 12, 2017
2 parents ce2f023 + 0e0add1 commit b7a3cc6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
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
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
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

0 comments on commit b7a3cc6

Please sign in to comment.