From 9af08849221d2a154308e5da6c1c4b387f2b0ee9 Mon Sep 17 00:00:00 2001 From: Jack Sleight Date: Thu, 6 Mar 2025 12:05:39 +0000 Subject: [PATCH] Add remember when/unless methods to cache repository --- src/Illuminate/Cache/Repository.php | 66 +++++++++++++++++++++++++++ tests/Cache/CacheRepositoryTest.php | 70 +++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index a5f1df9db137..11327cf95591 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -431,6 +431,40 @@ public function remember($key, $ttl, Closure $callback) return $value; } + /** + * Get an item from the cache, or execute the given Closure and store the result, if the given "value" is (or resolves to) truthy. + * + * @template TCacheValue + * + * @param (\Closure($this): TWhenParameter)|TWhenParameter|null $value + * @param \Closure|\DateTimeInterface|\DateInterval|int|null $ttl + * @param \Closure(): TCacheValue $callback + * @return TCacheValue + */ + public function rememberWhen($value, $key, $ttl, Closure $callback) + { + $value = $value instanceof Closure ? $value($this) : $value; + + return $value ? $this->remember($key, $ttl, $callback) : $callback(); + } + + /** + * Get an item from the cache, or execute the given Closure and store the result, if the given "value" is (or resolves to) falsy. + * + * @template TCacheValue + * + * @param (\Closure($this): TWhenParameter)|TWhenParameter|null $value + * @param \Closure|\DateTimeInterface|\DateInterval|int|null $ttl + * @param \Closure(): TCacheValue $callback + * @return TCacheValue + */ + public function rememberUnless($value, $key, $ttl, Closure $callback) + { + $value = $value instanceof Closure ? $value($this) : $value; + + return ! $value ? $this->remember($key, $ttl, $callback) : $callback(); + } + /** * Get an item from the cache, or execute the given Closure and store the result forever. * @@ -470,6 +504,38 @@ public function rememberForever($key, Closure $callback) return $value; } + /** + * Get an item from the cache, or execute the given Closure and store the result forever, if the given "value" is (or resolves to) truthy. + * + * @template TCacheValue + * + * @param (\Closure($this): TWhenParameter)|TWhenParameter|null $value + * @param \Closure(): TCacheValue $callback + * @return TCacheValue + */ + public function rememberForeverWhen($value, $key, Closure $callback) + { + $value = $value instanceof Closure ? $value($this) : $value; + + return $value ? $this->rememberForever($key, $callback) : $callback(); + } + + /** + * Get an item from the cache, or execute the given Closure and store the result forever, if the given "value" is (or resolves to) falsy. + * + * @template TCacheValue + * + * @param (\Closure($this): TWhenParameter)|TWhenParameter|null $value + * @param \Closure(): TCacheValue $callback + * @return TCacheValue + */ + public function rememberForeverUnless($value, $key, Closure $callback) + { + $value = $value instanceof Closure ? $value($this) : $value; + + return ! $value ? $this->rememberForever($key, $callback) : $callback(); + } + /** * Retrieve an item from the cache by key, refreshing it in the background if it is stale. * diff --git a/tests/Cache/CacheRepositoryTest.php b/tests/Cache/CacheRepositoryTest.php index 5c4b77ce070f..5d5db92cd64c 100755 --- a/tests/Cache/CacheRepositoryTest.php +++ b/tests/Cache/CacheRepositoryTest.php @@ -142,6 +142,41 @@ public function testRememberMethodCallsPutAndReturnsDefault() $this->assertSame('bar', $result); } + public function testRememberWhenUnless() + { + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('get')->once()->andReturn(null); + $repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', 10); + $result = $repo->rememberWhen(true, 'foo', 10, function () { + return 'bar'; + }); + $this->assertSame('bar', $result); + + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('get')->never(); + $repo->getStore()->shouldReceive('put')->never(); + $result = $repo->rememberWhen(false, 'foo', 10, function () { + return 'bar'; + }); + $this->assertSame('bar', $result); + + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('get')->once()->andReturn(null); + $repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', 10); + $result = $repo->rememberUnless(false, 'foo', 10, function () { + return 'bar'; + }); + $this->assertSame('bar', $result); + + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('get')->never(); + $repo->getStore()->shouldReceive('put')->never(); + $result = $repo->rememberUnless(true, 'foo', 10, function () { + return 'bar'; + }); + $this->assertSame('bar', $result); + } + public function testRememberForeverMethodCallsForeverAndReturnsDefault() { $repo = $this->getRepository(); @@ -153,6 +188,41 @@ public function testRememberForeverMethodCallsForeverAndReturnsDefault() $this->assertSame('bar', $result); } + public function testRememberForeverWhenUnless() + { + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('get')->once()->andReturn(null); + $repo->getStore()->shouldReceive('forever')->once()->with('foo', 'bar'); + $result = $repo->rememberForeverWhen(true, 'foo', function () { + return 'bar'; + }); + $this->assertSame('bar', $result); + + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('get')->never(); + $repo->getStore()->shouldReceive('forever')->never(); + $result = $repo->rememberForeverWhen(false, 'foo', function () { + return 'bar'; + }); + $this->assertSame('bar', $result); + + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('get')->once()->andReturn(null); + $repo->getStore()->shouldReceive('forever')->once()->with('foo', 'bar'); + $result = $repo->rememberForeverUnless(false, 'foo', function () { + return 'bar'; + }); + $this->assertSame('bar', $result); + + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('get')->never(); + $repo->getStore()->shouldReceive('forever')->never(); + $result = $repo->rememberForeverUnless(true, 'foo', function () { + return 'bar'; + }); + $this->assertSame('bar', $result); + } + public function testPuttingMultipleItemsInCache() { $repo = $this->getRepository();