From 53b684d1f7bc1460c87af9e5b1aaade6761a0617 Mon Sep 17 00:00:00 2001 From: Rob Date: Thu, 12 Oct 2017 14:40:00 +0400 Subject: [PATCH 1/3] Add Builder orders to Cachable Key --- src/CachedBuilder.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index f01b59c..afb4e5d 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -27,6 +27,7 @@ protected function getCacheKey(array $columns = ['*'], $idColumn = null) : strin $key .= $this->getQueryColumns($columns); $key .= $this->getWhereClauses(); $key .= $this->getWithModels(); + $key .= $this->getOrderClauses(); $key .= $this->getOffsetClause(); $key .= $this->getLimitClause(); @@ -117,6 +118,16 @@ protected function getWithModels() : string return '-' . implode('-', $eagerLoads->keys()->toArray()); } + protected function getOrderClauses(){ + $orders = collect($this->query->orders); + + return $orders->reduce(function($carry, $order){ + $carry .= '_sort_' . array_get($order, 'column') . '_' . array_get($order, 'direction'); + + return $carry; + }); + } + protected function getCacheTags() : array { return collect($this->eagerLoad)->keys() From 157d5469abf24f7bf389d498712fd02b96db27b4 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 12 Oct 2017 10:09:39 -0700 Subject: [PATCH 2/3] Add orderBy clause to cache key --- src/CachedBuilder.php | 10 ++++------ tests/Unit/CachedBuilderTest.php | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index afb4e5d..6e6de17 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -27,7 +27,7 @@ protected function getCacheKey(array $columns = ['*'], $idColumn = null) : strin $key .= $this->getQueryColumns($columns); $key .= $this->getWhereClauses(); $key .= $this->getWithModels(); - $key .= $this->getOrderClauses(); + $key .= $this->getOrderByClauses(); $key .= $this->getOffsetClause(); $key .= $this->getLimitClause(); @@ -118,16 +118,14 @@ protected function getWithModels() : string return '-' . implode('-', $eagerLoads->keys()->toArray()); } - protected function getOrderClauses(){ + protected function getOrderByClauses(){ $orders = collect($this->query->orders); return $orders->reduce(function($carry, $order){ - $carry .= '_sort_' . array_get($order, 'column') . '_' . array_get($order, 'direction'); - - return $carry; + return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction']; }); } - + protected function getCacheTags() : array { return collect($this->eagerLoad)->keys() diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 99a30ff..1a995df 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -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') @@ -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') From 0e0add1063ef184ee83e30a3798fd81bfc83e715 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 12 Oct 2017 10:11:17 -0700 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21be263..608d21f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.