From f9c76f214c854ae143e2c48e0c2333168e18844e Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Tue, 9 Jan 2018 18:08:20 +0100 Subject: [PATCH 1/9] WIP - centralizing key generation to enable key hashing --- src/CacheKey.php | 9 ++- src/CachedModel.php | 2 +- src/Traits/Cachable.php | 7 +- tests/Unit/CachedBuilderTest.php | 78 +++++++++++++---------- tests/Unit/CachedModelTest.php | 4 +- tests/Unit/Console/Commands/FlushTest.php | 4 +- tests/Unit/DisabledCachedBuilderTest.php | 25 ++++---- tests/Unit/DisabledCachedModelTest.php | 2 +- tests/Unit/Traits/CachableTest.php | 2 +- 9 files changed, 78 insertions(+), 55 deletions(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index 9e3cd50..b7227b2 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -17,8 +17,11 @@ public function __construct(array $eagerLoad, Model $model, Builder $query) $this->query = $query; } - public function make(array $columns = ['*'], $idColumn = null) : string - { + public function make( + array $columns = ['*'], + $idColumn = null, + string $suffix = '' + ) : string { $key = $this->getModelSlug(); $key .= $this->getIdColumn($idColumn ?: ''); $key .= $this->getQueryColumns($columns); @@ -27,6 +30,8 @@ public function make(array $columns = ['*'], $idColumn = null) : string $key .= $this->getOrderByClauses(); $key .= $this->getOffsetClause(); $key .= $this->getLimitClause(); + $key .= $suffix; + $key = sha1($key); return $key; } diff --git a/src/CachedModel.php b/src/CachedModel.php index a2620f6..edd9648 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -55,7 +55,7 @@ public static function all($columns = ['*']) $class = get_called_class(); $instance = new $class; $tags = [str_slug(get_called_class())]; - $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor'); return $instance->cache($tags) ->rememberForever($key, function () use ($columns) { diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index 08d4119..132cd33 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -43,8 +43,11 @@ public function flushCache(array $tags = []) protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string { - return (new CacheKey($this->eagerLoad, $this->model, $this->query)) - ->make($columns, $idColumn); + $cacheKey = (new CacheKey($this->eagerLoad, $this->model, $this->query)); + if ($suffix) { + $cacheKey->setSuffix($suffix); + } + return $cacheKey->make($columns, $idColumn, $suffix); } protected function makeCacheTags() : array diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index fbaaa28..24910ba 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -68,7 +68,7 @@ public function testCacheIsNotEmptyAfterLoadingModels() 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook' ]) - ->get('genealabslaravelmodelcachingtestsfixturesauthor-books'); + ->get(sha1('genealabslaravelmodelcachingtestsfixturesauthor-books')); $this->assertNotNull($results); } @@ -83,7 +83,10 @@ public function testCreatingModelClearsCache() 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook' ]) - ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); + ->get( + 'genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_' . + '7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks' + ); $this->assertNull($results); } @@ -98,7 +101,10 @@ public function testUpdatingModelClearsCache() 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook' ]) - ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); + ->get( + 'genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_' . + '7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks' + ); $this->assertNull($results); } @@ -112,7 +118,10 @@ public function testDeletingModelClearsCache() 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook' ]) - ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); + ->get( + 'genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_' . + '7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks' + ); $this->assertNull($results); } @@ -125,7 +134,7 @@ public function testHasManyRelationshipIsCached() 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook' ]) - ->get("genealabslaravelmodelcachingtestsfixturesauthor-books")); + ->get(sha1("genealabslaravelmodelcachingtestsfixturesauthor-books"))); $this->assertNotNull($results); $this->assertEmpty($authors->diffAssoc($results)); @@ -142,7 +151,7 @@ public function testBelongsToRelationshipIsCached() 'genealabslaravelmodelcachingtestsfixturesbook', 'genealabslaravelmodelcachingtestsfixturesauthor' ]) - ->get("genealabslaravelmodelcachingtestsfixturesbook-author")); + ->get(sha1("genealabslaravelmodelcachingtestsfixturesbook-author"))); $this->assertNotNull($results); $this->assertEmpty($books->diffAssoc($results)); @@ -159,7 +168,7 @@ public function testBelongsToManyRelationshipIsCached() 'genealabslaravelmodelcachingtestsfixturesbook', 'genealabslaravelmodelcachingtestsfixturesstore' ]) - ->get("genealabslaravelmodelcachingtestsfixturesbook-stores")); + ->get(sha1("genealabslaravelmodelcachingtestsfixturesbook-stores"))); $this->assertNotNull($results); $this->assertEmpty($books->diffAssoc($results)); @@ -177,7 +186,7 @@ public function testHasOneRelationshipIsCached() 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesprofile' ]) - ->get("genealabslaravelmodelcachingtestsfixturesauthor-profile")); + ->get(sha1("genealabslaravelmodelcachingtestsfixturesauthor-profile"))); $this->assertNotNull($results); $this->assertEmpty($authors->diffAssoc($results)); @@ -190,7 +199,7 @@ public function testAvgModelResultsCreatesCache() { $authorId = (new Author)->with('books', 'profile') ->avg('id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-avg_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-avg_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -230,7 +239,10 @@ public function testChunkModelResultsCreatesCache() } $cachedChunks['authors']->push($chunk); - $cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile_orderBy_authors.id_asc{$offset}-limit_3"); + $cachedChunks['keys']->push(sha1( + "genealabslaravelmodelcachingtestsfixturesauthor-books-pr" . + "ofile_orderBy_authors.id_asc{$offset}-limit_3" + )); }); (new UncachedAuthor)->with('books', 'profile') @@ -253,7 +265,7 @@ public function testCountModelResultsCreatesCache() $authors = (new Author) ->with('books', 'profile') ->count(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-count'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-count'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -274,7 +286,7 @@ public function testCursorModelResultsCreatesCache() $authors = (new Author) ->with('books', 'profile') ->cursor(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-cursor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-cursor'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -297,7 +309,7 @@ public function testCursorModelResultsCreatesCache() public function testFindModelResultsCreatesCache() { $author = collect()->push((new Author)->find(1)); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_1'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_1'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', ]; @@ -314,7 +326,7 @@ public function testGetModelResultsCreatesCache() { $authors = (new Author)->with('books', 'profile') ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -334,7 +346,7 @@ public function testMaxModelResultsCreatesCache() { $authorId = (new Author)->with('books', 'profile') ->max('id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-max_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-max_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -354,7 +366,7 @@ public function testMinModelResultsCreatesCache() { $authorId = (new Author)->with('books', 'profile') ->min('id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-min_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-min_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -374,7 +386,7 @@ public function testPluckModelResultsCreatesCache() { $authors = (new Author)->with('books', 'profile') ->pluck('name', 'id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-pluck_name_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-pluck_name_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -394,7 +406,7 @@ public function testSumModelResultsCreatesCache() { $authorId = (new Author)->with('books', 'profile') ->sum('id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-sum_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-sum_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -414,7 +426,7 @@ public function testValueModelResultsCreatesCache() { $authors = (new Author)->with('books', 'profile') ->value('name'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -437,7 +449,7 @@ public function testNestedRelationshipEagerLoading() $authors = collect([(new Author)->with('books.publisher') ->first()]); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher-first'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher-first'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -456,7 +468,7 @@ public function testNestedRelationshipEagerLoading() public function testLazyLoadedRelationshipResolvesThroughCachedBuilder() { $books = (new Author)->first()->books; - $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesbook', ]; @@ -471,7 +483,7 @@ public function testLazyLoadedRelationshipResolvesThroughCachedBuilder() public function testLazyLoadingOnResourceIsCached() { $books = (new AuthorResource((new Author)->first()))->books; - $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesbook', ]; @@ -487,7 +499,7 @@ public function testOrderByClauseParsing() { $authors = (new Author)->orderBy('name')->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', ]; @@ -504,7 +516,7 @@ public function testNestedRelationshipWhereClauseParsing() $authors = (new Author)->with('books.publisher') ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -526,7 +538,7 @@ public function testExistsRelationshipWhereClauseParsing() $authors = (new Author)->whereHas('books') ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_exists_and_authors.id_=_books.author_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_exists_and_authors.id_=_books.author_id'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = cache()->tags($tags)->get($key); @@ -543,7 +555,7 @@ public function testDoesntHaveWhereClauseParsing() ->doesntHave('books') ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_notexists_and_authors.id_=_books.author_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_notexists_and_authors.id_=_books.author_id'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = cache() @@ -565,8 +577,8 @@ public function testColumnsRelationshipWhereClauseParsing() $authors = (new Author) ->where('name', '=', $author->name) ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-name_' . - $author->name; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-name_' . + $author->name); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = cache() @@ -586,7 +598,7 @@ public function testRawWhereClauseParsing() ->whereRaw('name <> \'\'') ->first()]); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_name-first'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_and_name-first'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = collect([cache()->tags($tags)->get($key)]); @@ -606,7 +618,7 @@ public function testScopeClauseParsing() $authors = (new Author) ->startsWithA() ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-name_A%'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-name_A%'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = cache()->tags($tags)->get($key); @@ -625,7 +637,7 @@ public function testRelationshipQueriesAreCached() ->first() ->books() ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesbook' ]; @@ -646,7 +658,7 @@ public function testRawOrderByWithoutColumnReference() ->orderByRaw('DATE()') ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = cache() @@ -669,7 +681,7 @@ public function testDelete() ->first(); $authorId = $author->id; $liveResultId = $liveResult->id; - $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $author->delete(); diff --git a/tests/Unit/CachedModelTest.php b/tests/Unit/CachedModelTest.php index eb48379..7db5bbe 100644 --- a/tests/Unit/CachedModelTest.php +++ b/tests/Unit/CachedModelTest.php @@ -47,7 +47,7 @@ public function setUp() public function testAllModelResultsCreatesCache() { $authors = (new Author)->all(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', ]; @@ -64,7 +64,7 @@ public function testAllModelResultsCreatesCache() public function testScopeDisablesCaching() { - $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $authors = (new Author) ->where("name", "Bruno") diff --git a/tests/Unit/Console/Commands/FlushTest.php b/tests/Unit/Console/Commands/FlushTest.php index af21653..8e37cff 100644 --- a/tests/Unit/Console/Commands/FlushTest.php +++ b/tests/Unit/Console/Commands/FlushTest.php @@ -43,7 +43,7 @@ public function setUp() public function testGivenModelIsFlushed() { $authors = (new Author)->all(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = cache() @@ -62,7 +62,7 @@ public function testGivenModelIsFlushed() public function testGivenModelWithRelationshipIsFlushed() { $authors = (new Author)->with('books')->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', diff --git a/tests/Unit/DisabledCachedBuilderTest.php b/tests/Unit/DisabledCachedBuilderTest.php index f6eccd8..52e11be 100644 --- a/tests/Unit/DisabledCachedBuilderTest.php +++ b/tests/Unit/DisabledCachedBuilderTest.php @@ -55,7 +55,7 @@ public function testAvgModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->avg('id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-avg_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-avg_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -98,7 +98,10 @@ public function testChunkModelResultsIsNotCached() } $cachedChunks['authors']->push($chunk); - $cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile_orderBy_authors.id_asc{$offset}-limit_3"); + $cachedChunks['keys']->push(sha1( + "genealabslaravelmodelcachingtestsfixturesauthor-books-pr" . + "ofile_orderBy_authors.id_asc{$offset}-limit_3" + )); }); $liveResults = (new UncachedAuthor)->with('books', 'profile') @@ -122,7 +125,7 @@ public function testCountModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->count(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-count'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-count'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -146,7 +149,7 @@ public function testCursorModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->cursor(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-cursor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-cursor'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -172,7 +175,7 @@ public function testFindModelResultsIsNotCached() ->with('books') ->disableCache() ->find(1); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_1'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_1'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', ]; @@ -193,7 +196,7 @@ public function testGetModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -217,7 +220,7 @@ public function testMaxModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->max('id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-max_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-max_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -241,7 +244,7 @@ public function testMinModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->min('id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-min_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-min_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -265,7 +268,7 @@ public function testPluckModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->pluck('name', 'id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-pluck_name_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-pluck_name_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -289,7 +292,7 @@ public function testSumModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->sum('id'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-sum_id'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-sum_id'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -313,7 +316,7 @@ public function testValueModelResultsIsNotCached() ->with('books', 'profile') ->disableCache() ->value('name'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', diff --git a/tests/Unit/DisabledCachedModelTest.php b/tests/Unit/DisabledCachedModelTest.php index cae8e60..8bbf550 100644 --- a/tests/Unit/DisabledCachedModelTest.php +++ b/tests/Unit/DisabledCachedModelTest.php @@ -46,7 +46,7 @@ public function setUp() public function testAllModelResultsIsNotCached() { - $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $authors = (new Author) ->disableCache() diff --git a/tests/Unit/Traits/CachableTest.php b/tests/Unit/Traits/CachableTest.php index f1218b7..5d2cc3d 100644 --- a/tests/Unit/Traits/CachableTest.php +++ b/tests/Unit/Traits/CachableTest.php @@ -50,7 +50,7 @@ public function testSpecifyingAlternateCacheDriver() $configCacheStores['customCache'] = ['driver' => 'array']; config(['cache.stores' => $configCacheStores]); config(['laravel-model-caching.store' => 'customCache']); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $authors = (new Author) From 1fcb9642e06b64af49d1cc53792659ee14d64a09 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Thu, 11 Jan 2018 13:47:17 +0100 Subject: [PATCH 2/9] Implemented passing a key suffix to CacheKey. --- src/CachedBuilder.php | 92 +++++++++++++++++++++++++++-------------- src/Traits/Cachable.php | 14 +++---- 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index c7b4e00..c5da9d1 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -17,9 +17,12 @@ public function avg($column) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) { - return parent::avg($column); - }); + ->rememberForever( + $this->makeCacheKey(['*'], null, "-avg_{$column}"), + function () use ($column) { + return parent::avg($column); + } + ); } public function count($columns = ['*']) @@ -29,9 +32,12 @@ public function count($columns = ['*']) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) { - return parent::count($columns); - }); + ->rememberForever( + $this->makeCacheKey(['*'], null, "-count"), + function () use ($columns) { + return parent::count($columns); + } + ); } public function cursor() @@ -41,9 +47,12 @@ public function cursor() } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey() . "-cursor", function () { - return collect(parent::cursor()); - }); + ->rememberForever( + $this->makeCacheKey(['*'], null, "-cursor"), + function () { + return collect(parent::cursor()); + } + ); } public function delete() @@ -64,9 +73,12 @@ public function find($id, $columns = ['*']) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) { - return parent::find($id, $columns); - }); + ->rememberForever( + $this->makeCacheKey($columns, $id), + function () use ($id, $columns) { + return parent::find($id, $columns); + } + ); } public function first($columns = ['*']) @@ -76,9 +88,12 @@ public function first($columns = ['*']) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) { - return parent::first($columns); - }); + ->rememberForever( + $this->makeCacheKey($columns, null, '-first'), + function () use ($columns) { + return parent::first($columns); + } + ); } public function get($columns = ['*']) @@ -88,9 +103,12 @@ public function get($columns = ['*']) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey($columns), function () use ($columns) { - return parent::get($columns); - }); + ->rememberForever( + $this->makeCacheKey($columns), + function () use ($columns) { + return parent::get($columns); + } + ); } public function max($column) @@ -100,9 +118,12 @@ public function max($column) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) { - return parent::max($column); - }); + ->rememberForever( + $this->makeCacheKey(['*'], null, "-max_{$column}"), + function () use ($column) { + return parent::max($column); + } + ); } public function min($column) @@ -112,9 +133,12 @@ public function min($column) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) { - return parent::min($column); - }); + ->rememberForever( + $this->makeCacheKey(['*'], null, "-min_{$column}"), + function () use ($column) { + return parent::min($column); + } + ); } public function pluck($column, $key = null) @@ -123,7 +147,7 @@ public function pluck($column, $key = null) return parent::pluck($column, $key); } - $cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}"; + $cacheKey = $this->makeCacheKey([$column], null, "-pluck_{$column}"); if ($key) { $cacheKey .= "_{$key}"; @@ -142,9 +166,12 @@ public function sum($column) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) { - return parent::sum($column); - }); + ->rememberForever( + $this->makeCacheKey(['*'], null, "-sum_{$column}"), + function () use ($column) { + return parent::sum($column); + } + ); } public function value($column) @@ -154,8 +181,11 @@ public function value($column) } return $this->cache($this->makeCacheTags()) - ->rememberForever($this->makeCacheKey() . "-value_{$column}", function () use ($column) { - return parent::value($column); - }); + ->rememberForever( + $this->makeCacheKey(['*'], null, "-value_{$column}"), + function () use ($column) { + return parent::value($column); + } + ); } } diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index 132cd33..fbd9c36 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -41,13 +41,13 @@ public function flushCache(array $tags = []) $this->cache($tags)->flush(); } - protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string - { - $cacheKey = (new CacheKey($this->eagerLoad, $this->model, $this->query)); - if ($suffix) { - $cacheKey->setSuffix($suffix); - } - return $cacheKey->make($columns, $idColumn, $suffix); + protected function makeCacheKey( + array $columns = ['*'], + $idColumn = null, + string $suffix = '' + ) : string { + return (new CacheKey($this->eagerLoad, $this->model, $this->query)) + ->make($columns, $idColumn, $suffix); } protected function makeCacheTags() : array From bf5b2da40d7741a13dc83b46f77d8cbdb6c6b854 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Thu, 11 Jan 2018 13:47:52 +0100 Subject: [PATCH 3/9] Excluded vim's swap files from versioning. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8b7ef35..790574f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor composer.lock +.*.sw? From cec395fa25f2bcc33e4391a4e06de23d789575c0 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Thu, 11 Jan 2018 21:29:11 +0100 Subject: [PATCH 4/9] Renamed $status to $keyDifferentiator. --- src/CacheKey.php | 4 ++-- src/Traits/Cachable.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index b7227b2..71ad90e 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -20,7 +20,7 @@ public function __construct(array $eagerLoad, Model $model, Builder $query) public function make( array $columns = ['*'], $idColumn = null, - string $suffix = '' + string $keyDifferentiator = '' ) : string { $key = $this->getModelSlug(); $key .= $this->getIdColumn($idColumn ?: ''); @@ -30,7 +30,7 @@ public function make( $key .= $this->getOrderByClauses(); $key .= $this->getOffsetClause(); $key .= $this->getLimitClause(); - $key .= $suffix; + $key .= $keyDifferentiator; $key = sha1($key); return $key; diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index fbd9c36..fdc9f4a 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -44,10 +44,10 @@ public function flushCache(array $tags = []) protected function makeCacheKey( array $columns = ['*'], $idColumn = null, - string $suffix = '' + string $keyDifferentiator = '' ) : string { return (new CacheKey($this->eagerLoad, $this->model, $this->query)) - ->make($columns, $idColumn, $suffix); + ->make($columns, $idColumn, $keyDifferentiator); } protected function makeCacheTags() : array From d3ecb408817cbef466f9747a939de81a0deb554c Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Thu, 11 Jan 2018 21:43:07 +0100 Subject: [PATCH 5/9] Remove editor-specific excludes from .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 790574f..8b7ef35 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ /vendor composer.lock -.*.sw? From 4cfba48b6d25305d5bdf14a2f0d3f24bd26d8c57 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Thu, 11 Jan 2018 22:34:44 +0100 Subject: [PATCH 6/9] Fix CachedBuilder::pluck key generation --- src/CachedBuilder.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index c5da9d1..ec84894 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -147,11 +147,8 @@ public function pluck($column, $key = null) return parent::pluck($column, $key); } - $cacheKey = $this->makeCacheKey([$column], null, "-pluck_{$column}"); - - if ($key) { - $cacheKey .= "_{$key}"; - } + $keyDifferentiator = "-pluck_{$column}" . ($key ? "_{$key}" : ""); + $cacheKey = $this->makeCacheKey([$column], null, $keyDifferentiator); return $this->cache($this->makeCacheTags()) ->rememberForever($cacheKey, function () use ($column, $key) { From 31e30c251f4a1c58feb8b2f1069af2d54c1b74e1 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Fri, 12 Jan 2018 00:11:25 +0100 Subject: [PATCH 7/9] Add a unit test --- tests/Unit/CacheKeyTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/Unit/CacheKeyTest.php diff --git a/tests/Unit/CacheKeyTest.php b/tests/Unit/CacheKeyTest.php new file mode 100644 index 0000000..1613f4d --- /dev/null +++ b/tests/Unit/CacheKeyTest.php @@ -0,0 +1,23 @@ +setAccessible(true); + + $builder = (new Author)->startsWithA(); + $key = $makeCacheKey->invoke($builder); + + $this->assertTrue(strlen($key) === 40 && ctype_xdigit($key)); + } +} From dbdaca59fb6c630f93e4b2ccab583158a5f7ab1c Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Mon, 15 Jan 2018 17:20:50 +0100 Subject: [PATCH 8/9] Replaced a manually entered key with a call to makeCacheKey --- src/CachedModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachedModel.php b/src/CachedModel.php index edd9648..629642e 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -55,7 +55,7 @@ public static function all($columns = ['*']) $class = get_called_class(); $instance = new $class; $tags = [str_slug(get_called_class())]; - $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor'); + $key = makeCacheKey(); return $instance->cache($tags) ->rememberForever($key, function () use ($columns) { From d8bd67909553a0a7ef74148e5f300c460b482db1 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Bronner" Date: Mon, 15 Jan 2018 17:50:30 +0100 Subject: [PATCH 9/9] Fix the call to makeCacheKey from the previous commit and add sane defaults to makeCacheKey --- src/CachedModel.php | 2 +- src/Traits/Cachable.php | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/CachedModel.php b/src/CachedModel.php index 629642e..11b32f4 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -55,7 +55,7 @@ public static function all($columns = ['*']) $class = get_called_class(); $instance = new $class; $tags = [str_slug(get_called_class())]; - $key = makeCacheKey(); + $key = $instance->makeCacheKey(); return $instance->cache($tags) ->rememberForever($key, function () use ($columns) { diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index fdc9f4a..3e16c37 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -4,6 +4,7 @@ use GeneaLabs\LaravelModelCaching\CacheTags; use GeneaLabs\LaravelModelCaching\CachedModel; use Illuminate\Cache\TaggableStore; +use Illuminate\Database\Query\Builder; trait Cachable { @@ -46,7 +47,11 @@ protected function makeCacheKey( $idColumn = null, string $keyDifferentiator = '' ) : string { - return (new CacheKey($this->eagerLoad, $this->model, $this->query)) + $eagerLoad = $this->eagerLoad ?? []; + $model = $this->model ?? $this; + $query = $this->query ?? app(Builder::class); + + return (new CacheKey($eagerLoad, $model, $query)) ->make($columns, $idColumn, $keyDifferentiator); }