Skip to content

Commit f9c76f2

Browse files
committed
WIP - centralizing key generation to enable key hashing
1 parent 0b0d067 commit f9c76f2

9 files changed

+78
-55
lines changed

src/CacheKey.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ public function __construct(array $eagerLoad, Model $model, Builder $query)
1717
$this->query = $query;
1818
}
1919

20-
public function make(array $columns = ['*'], $idColumn = null) : string
21-
{
20+
public function make(
21+
array $columns = ['*'],
22+
$idColumn = null,
23+
string $suffix = ''
24+
) : string {
2225
$key = $this->getModelSlug();
2326
$key .= $this->getIdColumn($idColumn ?: '');
2427
$key .= $this->getQueryColumns($columns);
@@ -27,6 +30,8 @@ public function make(array $columns = ['*'], $idColumn = null) : string
2730
$key .= $this->getOrderByClauses();
2831
$key .= $this->getOffsetClause();
2932
$key .= $this->getLimitClause();
33+
$key .= $suffix;
34+
$key = sha1($key);
3035

3136
return $key;
3237
}

src/CachedModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static function all($columns = ['*'])
5555
$class = get_called_class();
5656
$instance = new $class;
5757
$tags = [str_slug(get_called_class())];
58-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
58+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
5959

6060
return $instance->cache($tags)
6161
->rememberForever($key, function () use ($columns) {

src/Traits/Cachable.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ public function flushCache(array $tags = [])
4343

4444
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
4545
{
46-
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
47-
->make($columns, $idColumn);
46+
$cacheKey = (new CacheKey($this->eagerLoad, $this->model, $this->query));
47+
if ($suffix) {
48+
$cacheKey->setSuffix($suffix);
49+
}
50+
return $cacheKey->make($columns, $idColumn, $suffix);
4851
}
4952

5053
protected function makeCacheTags() : array

tests/Unit/CachedBuilderTest.php

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testCacheIsNotEmptyAfterLoadingModels()
6868
'genealabslaravelmodelcachingtestsfixturesauthor',
6969
'genealabslaravelmodelcachingtestsfixturesbook'
7070
])
71-
->get('genealabslaravelmodelcachingtestsfixturesauthor-books');
71+
->get(sha1('genealabslaravelmodelcachingtestsfixturesauthor-books'));
7272

7373
$this->assertNotNull($results);
7474
}
@@ -83,7 +83,10 @@ public function testCreatingModelClearsCache()
8383
'genealabslaravelmodelcachingtestsfixturesauthor',
8484
'genealabslaravelmodelcachingtestsfixturesbook'
8585
])
86-
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
86+
->get(
87+
'genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_' .
88+
'7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'
89+
);
8790

8891
$this->assertNull($results);
8992
}
@@ -98,7 +101,10 @@ public function testUpdatingModelClearsCache()
98101
'genealabslaravelmodelcachingtestsfixturesauthor',
99102
'genealabslaravelmodelcachingtestsfixturesbook'
100103
])
101-
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
104+
->get(
105+
'genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_' .
106+
'7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'
107+
);
102108

103109
$this->assertNull($results);
104110
}
@@ -112,7 +118,10 @@ public function testDeletingModelClearsCache()
112118
'genealabslaravelmodelcachingtestsfixturesauthor',
113119
'genealabslaravelmodelcachingtestsfixturesbook'
114120
])
115-
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
121+
->get(
122+
'genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_' .
123+
'7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'
124+
);
116125

117126
$this->assertNull($results);
118127
}
@@ -125,7 +134,7 @@ public function testHasManyRelationshipIsCached()
125134
'genealabslaravelmodelcachingtestsfixturesauthor',
126135
'genealabslaravelmodelcachingtestsfixturesbook'
127136
])
128-
->get("genealabslaravelmodelcachingtestsfixturesauthor-books"));
137+
->get(sha1("genealabslaravelmodelcachingtestsfixturesauthor-books")));
129138

130139
$this->assertNotNull($results);
131140
$this->assertEmpty($authors->diffAssoc($results));
@@ -142,7 +151,7 @@ public function testBelongsToRelationshipIsCached()
142151
'genealabslaravelmodelcachingtestsfixturesbook',
143152
'genealabslaravelmodelcachingtestsfixturesauthor'
144153
])
145-
->get("genealabslaravelmodelcachingtestsfixturesbook-author"));
154+
->get(sha1("genealabslaravelmodelcachingtestsfixturesbook-author")));
146155

147156
$this->assertNotNull($results);
148157
$this->assertEmpty($books->diffAssoc($results));
@@ -159,7 +168,7 @@ public function testBelongsToManyRelationshipIsCached()
159168
'genealabslaravelmodelcachingtestsfixturesbook',
160169
'genealabslaravelmodelcachingtestsfixturesstore'
161170
])
162-
->get("genealabslaravelmodelcachingtestsfixturesbook-stores"));
171+
->get(sha1("genealabslaravelmodelcachingtestsfixturesbook-stores")));
163172

164173
$this->assertNotNull($results);
165174
$this->assertEmpty($books->diffAssoc($results));
@@ -177,7 +186,7 @@ public function testHasOneRelationshipIsCached()
177186
'genealabslaravelmodelcachingtestsfixturesauthor',
178187
'genealabslaravelmodelcachingtestsfixturesprofile'
179188
])
180-
->get("genealabslaravelmodelcachingtestsfixturesauthor-profile"));
189+
->get(sha1("genealabslaravelmodelcachingtestsfixturesauthor-profile")));
181190

182191
$this->assertNotNull($results);
183192
$this->assertEmpty($authors->diffAssoc($results));
@@ -190,7 +199,7 @@ public function testAvgModelResultsCreatesCache()
190199
{
191200
$authorId = (new Author)->with('books', 'profile')
192201
->avg('id');
193-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-avg_id';
202+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-avg_id');
194203
$tags = [
195204
'genealabslaravelmodelcachingtestsfixturesauthor',
196205
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -230,7 +239,10 @@ public function testChunkModelResultsCreatesCache()
230239
}
231240

232241
$cachedChunks['authors']->push($chunk);
233-
$cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile_orderBy_authors.id_asc{$offset}-limit_3");
242+
$cachedChunks['keys']->push(sha1(
243+
"genealabslaravelmodelcachingtestsfixturesauthor-books-pr" .
244+
"ofile_orderBy_authors.id_asc{$offset}-limit_3"
245+
));
234246
});
235247

236248
(new UncachedAuthor)->with('books', 'profile')
@@ -253,7 +265,7 @@ public function testCountModelResultsCreatesCache()
253265
$authors = (new Author)
254266
->with('books', 'profile')
255267
->count();
256-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-count';
268+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-count');
257269
$tags = [
258270
'genealabslaravelmodelcachingtestsfixturesauthor',
259271
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -274,7 +286,7 @@ public function testCursorModelResultsCreatesCache()
274286
$authors = (new Author)
275287
->with('books', 'profile')
276288
->cursor();
277-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-cursor';
289+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-cursor');
278290
$tags = [
279291
'genealabslaravelmodelcachingtestsfixturesauthor',
280292
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -297,7 +309,7 @@ public function testCursorModelResultsCreatesCache()
297309
public function testFindModelResultsCreatesCache()
298310
{
299311
$author = collect()->push((new Author)->find(1));
300-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_1';
312+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_1');
301313
$tags = [
302314
'genealabslaravelmodelcachingtestsfixturesauthor',
303315
];
@@ -314,7 +326,7 @@ public function testGetModelResultsCreatesCache()
314326
{
315327
$authors = (new Author)->with('books', 'profile')
316328
->get();
317-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile';
329+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile');
318330
$tags = [
319331
'genealabslaravelmodelcachingtestsfixturesauthor',
320332
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -334,7 +346,7 @@ public function testMaxModelResultsCreatesCache()
334346
{
335347
$authorId = (new Author)->with('books', 'profile')
336348
->max('id');
337-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-max_id';
349+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-max_id');
338350
$tags = [
339351
'genealabslaravelmodelcachingtestsfixturesauthor',
340352
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -354,7 +366,7 @@ public function testMinModelResultsCreatesCache()
354366
{
355367
$authorId = (new Author)->with('books', 'profile')
356368
->min('id');
357-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-min_id';
369+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-min_id');
358370
$tags = [
359371
'genealabslaravelmodelcachingtestsfixturesauthor',
360372
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -374,7 +386,7 @@ public function testPluckModelResultsCreatesCache()
374386
{
375387
$authors = (new Author)->with('books', 'profile')
376388
->pluck('name', 'id');
377-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-pluck_name_id';
389+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-pluck_name_id');
378390
$tags = [
379391
'genealabslaravelmodelcachingtestsfixturesauthor',
380392
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -394,7 +406,7 @@ public function testSumModelResultsCreatesCache()
394406
{
395407
$authorId = (new Author)->with('books', 'profile')
396408
->sum('id');
397-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-sum_id';
409+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-profile-sum_id');
398410
$tags = [
399411
'genealabslaravelmodelcachingtestsfixturesauthor',
400412
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -414,7 +426,7 @@ public function testValueModelResultsCreatesCache()
414426
{
415427
$authors = (new Author)->with('books', 'profile')
416428
->value('name');
417-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first';
429+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first');
418430
$tags = [
419431
'genealabslaravelmodelcachingtestsfixturesauthor',
420432
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -437,7 +449,7 @@ public function testNestedRelationshipEagerLoading()
437449
$authors = collect([(new Author)->with('books.publisher')
438450
->first()]);
439451

440-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher-first';
452+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher-first');
441453
$tags = [
442454
'genealabslaravelmodelcachingtestsfixturesauthor',
443455
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -456,7 +468,7 @@ public function testNestedRelationshipEagerLoading()
456468
public function testLazyLoadedRelationshipResolvesThroughCachedBuilder()
457469
{
458470
$books = (new Author)->first()->books;
459-
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull';
471+
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull');
460472
$tags = [
461473
'genealabslaravelmodelcachingtestsfixturesbook',
462474
];
@@ -471,7 +483,7 @@ public function testLazyLoadedRelationshipResolvesThroughCachedBuilder()
471483
public function testLazyLoadingOnResourceIsCached()
472484
{
473485
$books = (new AuthorResource((new Author)->first()))->books;
474-
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull';
486+
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull');
475487
$tags = [
476488
'genealabslaravelmodelcachingtestsfixturesbook',
477489
];
@@ -487,7 +499,7 @@ public function testOrderByClauseParsing()
487499
{
488500
$authors = (new Author)->orderBy('name')->get();
489501

490-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc';
502+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_orderBy_name_asc');
491503
$tags = [
492504
'genealabslaravelmodelcachingtestsfixturesauthor',
493505
];
@@ -504,7 +516,7 @@ public function testNestedRelationshipWhereClauseParsing()
504516
$authors = (new Author)->with('books.publisher')
505517
->get();
506518

507-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher';
519+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher');
508520
$tags = [
509521
'genealabslaravelmodelcachingtestsfixturesauthor',
510522
'genealabslaravelmodelcachingtestsfixturesbook',
@@ -526,7 +538,7 @@ public function testExistsRelationshipWhereClauseParsing()
526538
$authors = (new Author)->whereHas('books')
527539
->get();
528540

529-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_exists_and_authors.id_=_books.author_id';
541+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_exists_and_authors.id_=_books.author_id');
530542
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
531543

532544
$cachedResults = cache()->tags($tags)->get($key);
@@ -543,7 +555,7 @@ public function testDoesntHaveWhereClauseParsing()
543555
->doesntHave('books')
544556
->get();
545557

546-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_notexists_and_authors.id_=_books.author_id';
558+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_notexists_and_authors.id_=_books.author_id');
547559
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
548560

549561
$cachedResults = cache()
@@ -565,8 +577,8 @@ public function testColumnsRelationshipWhereClauseParsing()
565577
$authors = (new Author)
566578
->where('name', '=', $author->name)
567579
->get();
568-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-name_' .
569-
$author->name;
580+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-name_' .
581+
$author->name);
570582
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
571583

572584
$cachedResults = cache()
@@ -586,7 +598,7 @@ public function testRawWhereClauseParsing()
586598
->whereRaw('name <> \'\'')
587599
->first()]);
588600

589-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_name-first';
601+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_and_name-first');
590602
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
591603

592604
$cachedResults = collect([cache()->tags($tags)->get($key)]);
@@ -606,7 +618,7 @@ public function testScopeClauseParsing()
606618
$authors = (new Author)
607619
->startsWithA()
608620
->get();
609-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-name_A%';
621+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-name_A%');
610622
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
611623

612624
$cachedResults = cache()->tags($tags)->get($key);
@@ -625,7 +637,7 @@ public function testRelationshipQueriesAreCached()
625637
->first()
626638
->books()
627639
->get();
628-
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull';
640+
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull');
629641
$tags = [
630642
'genealabslaravelmodelcachingtestsfixturesbook'
631643
];
@@ -646,7 +658,7 @@ public function testRawOrderByWithoutColumnReference()
646658
->orderByRaw('DATE()')
647659
->get();
648660

649-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date';
661+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date');
650662
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
651663

652664
$cachedResults = cache()
@@ -669,7 +681,7 @@ public function testDelete()
669681
->first();
670682
$authorId = $author->id;
671683
$liveResultId = $liveResult->id;
672-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
684+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
673685
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
674686

675687
$author->delete();

tests/Unit/CachedModelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function setUp()
4747
public function testAllModelResultsCreatesCache()
4848
{
4949
$authors = (new Author)->all();
50-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
50+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
5151
$tags = [
5252
'genealabslaravelmodelcachingtestsfixturesauthor',
5353
];
@@ -64,7 +64,7 @@ public function testAllModelResultsCreatesCache()
6464

6565
public function testScopeDisablesCaching()
6666
{
67-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
67+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
6868
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
6969
$authors = (new Author)
7070
->where("name", "Bruno")

tests/Unit/Console/Commands/FlushTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function setUp()
4343
public function testGivenModelIsFlushed()
4444
{
4545
$authors = (new Author)->all();
46-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
46+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
4747
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
4848

4949
$cachedResults = cache()
@@ -62,7 +62,7 @@ public function testGivenModelIsFlushed()
6262
public function testGivenModelWithRelationshipIsFlushed()
6363
{
6464
$authors = (new Author)->with('books')->get();
65-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books';
65+
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor-books');
6666
$tags = [
6767
'genealabslaravelmodelcachingtestsfixturesauthor',
6868
'genealabslaravelmodelcachingtestsfixturesbook',

0 commit comments

Comments
 (0)