diff --git a/src/CacheKey.php b/src/CacheKey.php index 9e3cd50..71ad90e 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 $keyDifferentiator = '' + ) : 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 .= $keyDifferentiator; + $key = sha1($key); return $key; } diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index c7b4e00..ec84894 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,11 +147,8 @@ public function pluck($column, $key = null) return parent::pluck($column, $key); } - $cacheKey = $this->makeCacheKey([$column]) . "-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) { @@ -142,9 +163,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 +178,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/CachedModel.php b/src/CachedModel.php index a2620f6..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 = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $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 08d4119..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 { @@ -41,10 +42,17 @@ public function flushCache(array $tags = []) $this->cache($tags)->flush(); } - protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string - { - return (new CacheKey($this->eagerLoad, $this->model, $this->query)) - ->make($columns, $idColumn); + protected function makeCacheKey( + array $columns = ['*'], + $idColumn = null, + string $keyDifferentiator = '' + ) : string { + $eagerLoad = $this->eagerLoad ?? []; + $model = $this->model ?? $this; + $query = $this->query ?? app(Builder::class); + + return (new CacheKey($eagerLoad, $model, $query)) + ->make($columns, $idColumn, $keyDifferentiator); } protected function makeCacheTags() : array 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)); + } +} diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 01486e7..db3bee0 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(sha1( + '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(sha1( + '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(sha1( + '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() { $authorName = (new Author)->with('books', 'profile') ->value('name'); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-value_name'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-value_name'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook', @@ -435,7 +447,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', @@ -454,7 +466,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', ]; @@ -469,7 +481,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', ]; @@ -485,7 +497,7 @@ public function testOrderByClauseParsing() { $authors = (new Author)->orderBy('name')->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc'); $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', ]; @@ -502,7 +514,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', @@ -524,7 +536,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); @@ -541,7 +553,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() @@ -563,8 +575,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() @@ -584,7 +596,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)]); @@ -604,7 +616,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); @@ -623,7 +635,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' ]; @@ -644,7 +656,7 @@ public function testRawOrderByWithoutColumnReference() ->orderByRaw('DATE()') ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date'; + $key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date'); $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = cache() @@ -667,7 +679,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 bb7da18..6a563ab 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 7c3c49a..9b8bfae 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 3098a10..e541936 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 fbca170..44674b7 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 a3399b6..f6e8d8c 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)