Permalink
Browse files

Allow caching in seconds

These changes will allow developers more finer granular caching. By allowing caching in seconds, developers can set even more specific caching times. This also makes the caching TTL conform with PSR-16 which states that:

> Implementing Libraries MUST support at minimum TTL functionality as described below with whole-second granularity.

https://www.php-fig.org/psr/psr-16/

This refactor also simplifies things by removing support for floats and only supporting integers since floats aren't necessary anymore. If you take a look at the refactored tests and internals you'll see that we now do much less calculations.

This will have quite an impact on any app implementing caching and should be indicated in the upgrade guide as a high impact change.
  • Loading branch information...
driesvints committed Jan 22, 2019
1 parent a091aec commit fd6eb89b62ec09df1ffbee164831a827e83fa61d
@@ -49,16 +49,16 @@ public function get($key)
}
/**
* Store an item in the cache for a given number of minutes.
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function put($key, $value, $minutes)
public function put($key, $value, $seconds)
{
return $this->apc->put($this->prefix.$key, $value, (int) ($minutes * 60));
return $this->apc->put($this->prefix.$key, $value, $seconds);
}
/**
@@ -41,18 +41,18 @@ public function get($key)
}
/**
* Store an item in the cache for a given number of minutes.
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function put($key, $value, $minutes)
public function put($key, $value, $seconds)
{
$this->storage[$key] = [
'value' => $value,
'expiresAt' => $this->calculateExpiration($minutes),
'expiresAt' => $this->calculateExpiration($seconds),
];
return true;
@@ -140,22 +140,22 @@ public function getPrefix()
/**
* Get the expiration time of the key.
*
* @param int $minutes
* @param int $seconds
* @return int
*/
protected function calculateExpiration($minutes)
protected function calculateExpiration($seconds)
{
return $this->toTimestamp($minutes);
return $this->toTimestamp($seconds);
}
/**
* Get the UNIX timestamp for the given number of minutes.
* Get the UNIX timestamp for the given number of seconds.
*
* @param int $minutes
* @param int $seconds
* @return int
*/
protected function toTimestamp($minutes)
protected function toTimestamp($seconds)
{
return $minutes > 0 ? $this->availableAt($minutes * 60) : 0;
return $seconds > 0 ? $this->availableAt($seconds) : 0;
}
}
@@ -84,20 +84,20 @@ public function get($key)
}
/**
* Store an item in the cache for a given number of minutes.
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function put($key, $value, $minutes)
public function put($key, $value, $seconds)
{
$key = $this->prefix.$key;
$value = $this->serialize($value);
$expiration = $this->getTime() + (int) ($minutes * 60);
$expiration = $this->getTime() + $seconds;
try {
return $this->table()->insert(compact('key', 'value', 'expiration'));
@@ -35,7 +35,7 @@ public function __construct(DynamoDbStore $dynamo, $name, $seconds, $owner = nul
public function acquire()
{
return $this->dynamo->add(
$this->name, $this->owner, $this->seconds / 60
$this->name, $this->owner, $this->seconds
);
}
@@ -182,14 +182,14 @@ protected function isExpired(array $item, $expiration = null)
}
/**
* Store an item in the cache for a given number of minutes.
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function put($key, $value, $minutes)
public function put($key, $value, $seconds)
{
$this->dynamo->putItem([
'TableName' => $this->table,
@@ -201,7 +201,7 @@ public function put($key, $value, $minutes)
$this->type($value) => $this->serialize($value),
],
$this->expirationAttribute => [
'N' => (string) $this->toTimestamp($minutes),
'N' => (string) $this->toTimestamp($seconds),
],
],
]);
@@ -210,15 +210,15 @@ public function put($key, $value, $minutes)
}
/**
* Store multiple items in the cache for a given number of minutes.
* Store multiple items in the cache for a given number of $seconds.
*
* @param array $values
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function putMany(array $values, $minutes)
public function putMany(array $values, $seconds)
{
$expiration = $this->toTimestamp($minutes);
$expiration = $this->toTimestamp($seconds);
$this->dynamo->batchWriteItem([
'RequestItems' => [
@@ -250,10 +250,10 @@ public function putMany(array $values, $minutes)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function add($key, $value, $minutes)
public function add($key, $value, $seconds)
{
try {
$this->dynamo->putItem([
@@ -266,7 +266,7 @@ public function add($key, $value, $minutes)
$this->type($value) => $this->serialize($value),
],
$this->expirationAttribute => [
'N' => (string) $this->toTimestamp($minutes),
'N' => (string) $this->toTimestamp($seconds),
],
],
'ConditionExpression' => 'attribute_not_exists(#key) OR #expires_at < :now',
@@ -449,15 +449,15 @@ public function flush()
}
/**
* Get the UNIX timestamp for the given number of minutes.
* Get the UNIX timestamp for the given number of seconds.
*
* @param int $minutes
* @param int $seconds
* @return int
*/
protected function toTimestamp($minutes)
protected function toTimestamp($seconds)
{
return $minutes > 0
? $this->availableAt($minutes * 60)
return $seconds > 0
? $this->availableAt($seconds)
: Carbon::now()->getTimestamp();
}
@@ -12,26 +12,26 @@ class KeyWritten extends CacheEvent
public $value;
/**
* The number of minutes the key should be valid.
* The number of seconds the key should be valid.
*
* @var int|null
*/
public $minutes;
public $seconds;
/**
* Create a new event instance.
*
* @param string $key
* @param mixed $value
* @param int|null $minutes
* @param int|null $seconds
* @param array $tags
* @return void
*/
public function __construct($key, $value, $minutes = null, $tags = [])
public function __construct($key, $value, $seconds = null, $tags = [])
{
parent::__construct($key, $tags);
$this->value = $value;
$this->minutes = $minutes;
$this->seconds = $seconds;
}
}
@@ -50,19 +50,19 @@ public function get($key)
}
/**
* Store an item in the cache for a given number of minutes.
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function put($key, $value, $minutes)
public function put($key, $value, $seconds)
{
$this->ensureCacheDirectoryExists($path = $this->path($key));
$result = $this->files->put(
$path, $this->expiration($minutes).serialize($value), true
$path, $this->expiration($seconds).serialize($value), true
);
return $result !== false && $result > 0;
@@ -188,10 +188,10 @@ protected function getPayload($key)
$data = unserialize(substr($contents, 10));
// Next, we'll extract the number of minutes that are remaining for a cache
// Next, we'll extract the number of seconds that are remaining for a cache
// so that we can properly retain the time for things like the increment
// operation that may be performed on this cache on a later operation.
$time = ($expire - $this->currentTime()) / 60;
$time = $expire - $this->currentTime();
return compact('data', 'time');
}
@@ -220,16 +220,16 @@ protected function path($key)
}
/**
* Get the expiration time based on the given minutes.
* Get the expiration time based on the given seconds.
*
* @param float|int $minutes
* @param int $seconds
* @return int
*/
protected function expiration($minutes)
protected function expiration($seconds)
{
$time = $this->availableAt((int) ($minutes * 60));
$time = $this->availableAt($seconds);
return $minutes === 0 || $time > 9999999999 ? 9999999999 : (int) $time;
return $seconds === 0 || $time > 9999999999 ? 9999999999 : $time;
}
/**
@@ -93,28 +93,28 @@ public function many(array $keys)
}
/**
* Store an item in the cache for a given number of minutes.
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function put($key, $value, $minutes)
public function put($key, $value, $seconds)
{
return $this->memcached->set(
$this->prefix.$key, $value, $this->calculateExpiration($minutes)
$this->prefix.$key, $value, $this->calculateExpiration($seconds)
);
}
/**
* Store multiple items in the cache for a given number of minutes.
* Store multiple items in the cache for a given number of seconds.
*
* @param array $values
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function putMany(array $values, $minutes)
public function putMany(array $values, $seconds)
{
$prefixedValues = [];
@@ -123,7 +123,7 @@ public function putMany(array $values, $minutes)
}
return $this->memcached->setMulti(
$prefixedValues, $this->calculateExpiration($minutes)
$prefixedValues, $this->calculateExpiration($seconds)
);
}
@@ -132,13 +132,13 @@ public function putMany(array $values, $minutes)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function add($key, $value, $minutes)
public function add($key, $value, $seconds)
{
return $this->memcached->add(
$this->prefix.$key, $value, $this->calculateExpiration($minutes)
$this->prefix.$key, $value, $this->calculateExpiration($seconds)
);
}
@@ -227,23 +227,23 @@ public function flush()
/**
* Get the expiration time of the key.
*
* @param int $minutes
* @param int $seconds
* @return int
*/
protected function calculateExpiration($minutes)
protected function calculateExpiration($seconds)
{
return $this->toTimestamp($minutes);
return $this->toTimestamp($seconds);
}
/**
* Get the UNIX timestamp for the given number of minutes.
* Get the UNIX timestamp for the given number of seconds.
*
* @param int $minutes
* @param int $seconds
* @return int
*/
protected function toTimestamp($minutes)
protected function toTimestamp($seconds)
{
return $minutes > 0 ? $this->availableAt($minutes * 60) : 0;
return $seconds > 0 ? $this->availableAt($seconds) : 0;
}
/**
@@ -25,14 +25,14 @@ public function get($key)
}
/**
* Store an item in the cache for a given number of minutes.
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param int $seconds
* @return bool
*/
public function put($key, $value, $minutes)
public function put($key, $value, $seconds)
{
return false;
}
Oops, something went wrong.

0 comments on commit fd6eb89

Please sign in to comment.