Skip to content

Commit

Permalink
Add remember when/unless methods to cache repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksleight committed Mar 6, 2025
1 parent 4284e61 commit 9af0884
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
70 changes: 70 additions & 0 deletions tests/Cache/CacheRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 9af0884

Please sign in to comment.