Skip to content

Commit 157d546

Browse files
committed
Add orderBy clause to cache key
1 parent 2184d07 commit 157d546

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/CachedBuilder.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function getCacheKey(array $columns = ['*'], $idColumn = null) : strin
2727
$key .= $this->getQueryColumns($columns);
2828
$key .= $this->getWhereClauses();
2929
$key .= $this->getWithModels();
30-
$key .= $this->getOrderClauses();
30+
$key .= $this->getOrderByClauses();
3131
$key .= $this->getOffsetClause();
3232
$key .= $this->getLimitClause();
3333

@@ -118,16 +118,14 @@ protected function getWithModels() : string
118118
return '-' . implode('-', $eagerLoads->keys()->toArray());
119119
}
120120

121-
protected function getOrderClauses(){
121+
protected function getOrderByClauses(){
122122
$orders = collect($this->query->orders);
123123

124124
return $orders->reduce(function($carry, $order){
125-
$carry .= '_sort_' . array_get($order, 'column') . '_' . array_get($order, 'direction');
126-
127-
return $carry;
125+
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
128126
});
129127
}
130-
128+
131129
protected function getCacheTags() : array
132130
{
133131
return collect($this->eagerLoad)->keys()

tests/Unit/CachedBuilderTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public function testChunkModelResultsCreatesCache()
227227
}
228228

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

233233
(new UncachedAuthor)->with('books', 'profile')
@@ -464,6 +464,22 @@ public function testLazyLoadingOnResourceIsCached()
464464
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
465465
}
466466

467+
public function testOrderByClauseParsing()
468+
{
469+
$authors = (new Author)->orderBy('name')->get();
470+
471+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc';
472+
$tags = [
473+
'genealabslaravelmodelcachingtestsfixturesauthor',
474+
];
475+
476+
$cachedResults = cache()->tags($tags)->get($key);
477+
$liveResults = (new UncachedAuthor)->orderBy('name')->get();
478+
479+
$this->assertEmpty($authors->diffAssoc($cachedResults));
480+
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
481+
}
482+
467483
public function testNestedRelationshipWhereClauseParsing()
468484
{
469485
// -> with('modelA.modelB')

0 commit comments

Comments
 (0)