Skip to content

Commit cc24760

Browse files
committed
Added caching for value() querybuilder method
1 parent 06cea55 commit cc24760

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/CachedBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,16 @@ public function sum($column)
146146
return parent::sum($column);
147147
});
148148
}
149+
150+
public function value($column)
151+
{
152+
if (! $this->isCachable) {
153+
return parent::value($column);
154+
}
155+
156+
return $this->cache($this->makeCacheTags())
157+
->rememberForever($this->makeCacheKey() . "-value_{$column}", function () use ($column) {
158+
return parent::value($column);
159+
});
160+
}
149161
}

tests/Unit/CachedBuilderTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,26 +410,27 @@ public function testSumModelResultsCreatesCache()
410410
$this->assertEquals($liveResult, $cachedResult);
411411
}
412412

413+
/**
414+
* @group test
415+
*/
413416
public function testValueModelResultsCreatesCache()
414417
{
415-
$authors = (new Author)->with('books', 'profile')
418+
$authorName = (new Author)->with('books', 'profile')
416419
->value('name');
417-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first';
420+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile-value_name';
418421
$tags = [
419422
'genealabslaravelmodelcachingtestsfixturesauthor',
420423
'genealabslaravelmodelcachingtestsfixturesbook',
421424
'genealabslaravelmodelcachingtestsfixturesprofile',
422425
];
423426

424-
$cachedResults = cache()->tags($tags)
425-
->get($key)
426-
->name;
427-
428-
$liveResults = (new UncachedAuthor)->with('books', 'profile')
427+
$cachedResult = cache()->tags($tags)
428+
->get($key);
429+
$liveResult = (new UncachedAuthor)->with('books', 'profile')
429430
->value('name');
430431

431-
$this->assertEquals($authors, $cachedResults);
432-
$this->assertEquals($liveResults, $cachedResults);
432+
$this->assertEquals($authorName, $cachedResult);
433+
$this->assertEquals($authorName, $liveResult);
433434
}
434435

435436
public function testNestedRelationshipEagerLoading()

0 commit comments

Comments
 (0)