Skip to content

Commit

Permalink
Properly cache in seconds to follow PSR Simplecache requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Jun 23, 2021
1 parent 28e55ac commit f56cda1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
26 changes: 25 additions & 1 deletion src/Query/ArrayCacheStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,36 @@ public function set($key, $value, $ttl = null)
{
$this->storage[$key] = [
'value' => $value,
'expiresAt' => $this->parseDateInterval($ttl),
'expiresAt' => $this->calculateExpiration($ttl)
];

return true;
}

/**
* Get the expiration time of the key.
*
* @param int $seconds
*
* @return int
*/
protected function calculateExpiration($seconds)
{
return $this->toTimestamp($seconds);
}

/**
* Get the UNIX timestamp for the given number of seconds.
*
* @param int $seconds
*
* @return int
*/
protected function toTimestamp($seconds)
{
return $seconds > 0 ? $this->availableAt($seconds) : 0;
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 7 additions & 1 deletion src/Query/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public function get($key)
*/
public function put($key, $value, $ttl = null)
{
return $this->store->set($key, $value, $this->expiresAt($ttl));
$seconds = $this->secondsUntil($ttl);

if ($seconds <= 0) {
return $this->delete($key);
}

return $this->store->set($key, $value, $seconds);
}

/**
Expand Down
20 changes: 18 additions & 2 deletions src/Query/InteractsWithTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,29 @@
trait InteractsWithTime
{
/**
* Get the "expires at" UNIX timestamp.
* Get the number of seconds until the given DateTime.
*
* @param DateTimeInterface|DateInterval|int $delay
*
* @return int
*/
protected function expiresAt($delay = 0)
protected function secondsUntil($delay)
{
$delay = $this->parseDateInterval($delay);

return $delay instanceof DateTimeInterface
? max(0, $delay->getTimestamp() - $this->currentTime())
: (int) $delay;
}

/**
* Get the "available at" UNIX timestamp.
*
* @param DateTimeInterface|DateInterval|int $delay
*
* @return int
*/
protected function availableAt($delay = 0)
{
$delay = $this->parseDateInterval($delay);

Expand Down
12 changes: 7 additions & 5 deletions tests/Query/ArrayCacheStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ public function test_set_stores_values_without_ttl_indefinitely()
$this->assertEquals('value', $store->get('key'));
}

public function test_set_keys_with_expiry_return_default_value()
public function test_set_keys_with_expiry_return_default_value_when_expired()
{
Carbon::setTestNow(Carbon::now());

$store = new ArrayCacheStore();

$store->set('key', 'value', Carbon::now()->subDay()->getTimestamp());
$store->set('key', 'value', 10);
$this->assertEquals('value', $store->get('key', 'foo'));

$this->assertNull($store->get('key'));
Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond());
$this->assertEquals('foo', $store->get('key', 'foo'));

$store->set('key', 'value', Carbon::now()->addDay()->getTimestamp());
$this->assertEquals('value', $store->get('key', 'foo'));
Carbon::setTestNow(null);
}

public function test_set_multiple__stores_values()
Expand Down
16 changes: 5 additions & 11 deletions tests/Query/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,19 @@ public function test_items_can_be_put()
$cache = new Cache(new ArrayCacheStore());

$this->assertTrue($cache->put('foo', 'bar'));
$this->assertEquals('bar', $cache->get('foo'));
}

public function test_items_can_be_stored_with_expiry()
{
$cache = new Cache(new ArrayCacheStore());

$this->assertTrue($cache->put('foo', 'bar', Carbon::now()->subDay()));
$this->assertNull($cache->get('foo'));

$this->assertTrue($cache->put('foo', 'bar', Carbon::now()->addDay()));
$this->assertTrue($cache->put('foo', 'bar', Carbon::now()->addSeconds(10)));
$this->assertEquals('bar', $cache->get('foo'));
}

public function test_items_can_be_deleted()
{
$cache = new Cache(new ArrayCacheStore());

$this->assertTrue($cache->put('foo', 'bar'));
$cache->put('test', 'test');

$this->assertTrue($cache->put('foo', 'bar', Carbon::now()->addMinute()));
$this->assertEquals('bar', $cache->get('foo'));

$cache->delete('foo');
Expand All @@ -57,7 +51,7 @@ public function test_remember_executes_closure_and_stores_value()
{
$cache = new Cache(new ArrayCacheStore());

$cache->remember('foo', 0, function () {
$cache->remember('foo', Carbon::now()->addMinute(), function () {
return 'bar';
});

Expand Down

0 comments on commit f56cda1

Please sign in to comment.