Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Fix cache TTL compliance to PSR interface #27166

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Illuminate/Cache/ApcStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ public function get($key)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

return $this->apc->put($this->prefix.$key, $value, (int) ($minutes * 60));
}

Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Cache/ArrayStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ public function get($key)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

$this->storage[$key] = $value;

return true;
Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ public function get($key)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

$key = $this->prefix.$key;

$value = $this->serialize($value);
Expand Down
18 changes: 15 additions & 3 deletions src/Illuminate/Cache/DynamoDbStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,15 @@ protected function isExpired(array $item, $expiration = null)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

$this->dynamo->putItem([
'TableName' => $this->table,
'Item' => [
Expand All @@ -196,11 +200,15 @@ public function put($key, $value, $minutes)
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param float|int $minutes
* @param float|int|null $minutes
* @return void
*/
public function putMany(array $values, $minutes)
{
if (is_null($minutes)) {
return false;
}

$expiration = $this->toTimestamp($minutes);

$this->dynamo->batchWriteItem([
Expand Down Expand Up @@ -231,11 +239,15 @@ public function putMany(array $values, $minutes)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function add($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

try {
$response = $this->dynamo->putItem([
'TableName' => $this->table,
Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ public function get($key)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

$this->ensureCacheDirectoryExists($path = $this->path($key));

$result = $this->files->put(
Expand Down
18 changes: 15 additions & 3 deletions src/Illuminate/Cache/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ public function many(array $keys)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

return $this->memcached->set(
$this->prefix.$key, $value, $this->calculateExpiration($minutes)
);
Expand All @@ -111,11 +115,15 @@ public function put($key, $value, $minutes)
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function putMany(array $values, $minutes)
{
if (is_null($minutes)) {
return false;
}

$prefixedValues = [];

foreach ($values as $key => $value) {
Expand All @@ -132,11 +140,15 @@ public function putMany(array $values, $minutes)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function add($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

return $this->memcached->add(
$this->prefix.$key, $value, $this->calculateExpiration($minutes)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/NullStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function get($key)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes)
Expand Down
18 changes: 15 additions & 3 deletions src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,15 @@ public function many(array $keys)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

$result = $this->connection()->setex(
$this->prefix.$key, (int) max(1, $minutes * 60), $this->serialize($value)
);
Expand All @@ -100,11 +104,15 @@ public function put($key, $value, $minutes)
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function putMany(array $values, $minutes)
{
if (is_null($minutes)) {
return false;
}

$this->connection()->multi();

$manyResult = null;
Expand All @@ -125,11 +133,15 @@ public function putMany(array $values, $minutes)
*
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function add($key, $value, $minutes)
{
if (is_null($minutes)) {
return false;
}

$lua = "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])";

return (bool) $this->connection()->eval(
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/RedisTaggedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RedisTaggedCache extends TaggedCache
*
* @param string $key
* @param mixed $value
* @param \DateTime|float|int|null $minutes
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes = null)
Expand Down
64 changes: 35 additions & 29 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,22 @@ public function pull($key, $default = null)
public function put($key, $value, $minutes = null)
{
if (is_array($key)) {
$result = $this->putMany($key, $value);

return $result;
return $this->putMany($key, $value);
}

if (! is_null($minutes = $this->getMinutes($minutes))) {
$result = $this->store->put($this->itemKey($key), $value, $minutes);
$minutes = $this->getMinutes($minutes);

if ($result) {
$this->event(new KeyWritten($key, $value, $minutes));
}
if ($minutes !== null && $minutes <= 0) {
return false;
}

$result = $this->store->put($this->itemKey($key), $value, $minutes);

return $result;
if ($result) {
$this->event(new KeyWritten($key, $value, $minutes));
}

return false;
return $result;
}

/**
Expand All @@ -229,24 +229,26 @@ public function set($key, $value, $ttl = null)
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
* @return bool
*/
public function putMany(array $values, $minutes)
{
if (! is_null($minutes = $this->getMinutes($minutes))) {
$result = $this->store->putMany($values, $minutes);
$minutes = $this->getMinutes($minutes);

if ($result) {
foreach ($values as $key => $value) {
$this->event(new KeyWritten($key, $value, $minutes));
}
}
if ($minutes !== null && $minutes <= 0) {
return false;
}

return $result;
$result = $this->store->putMany($values, $minutes);

if ($result) {
foreach ($values as $key => $value) {
$this->event(new KeyWritten($key, $value, $minutes));
}
}

return false;
return $result;
}

/**
Expand All @@ -262,12 +264,14 @@ public function setMultiple($values, $ttl = null)
*
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
* @return bool
*/
public function add($key, $value, $minutes)
{
if (is_null($minutes = $this->getMinutes($minutes))) {
$minutes = $this->getMinutes($minutes);

if ($minutes !== null && $minutes <= 0) {
laurencei marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

Expand All @@ -284,9 +288,7 @@ public function add($key, $value, $minutes)
// so it exists for subsequent requests. Then, we will return true so it is
// easy to know if the value gets added. Otherwise, we will return false.
if (is_null($this->get($key))) {
$this->put($key, $value, $minutes);

return true;
return $this->put($key, $value, $minutes);
}

return false;
Expand Down Expand Up @@ -573,18 +575,22 @@ public function offsetUnset($key)
/**
* Calculate the number of minutes with the given duration.
*
* @param \DateTimeInterface|\DateInterval|float|int $duration
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
* @return float|int|null
*/
protected function getMinutes($duration)
protected function getMinutes($minutes)
{
$duration = $this->parseDateInterval($duration);
if ($minutes === null) {
driesvints marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

$duration = $this->parseDateInterval($minutes);

if ($duration instanceof DateTimeInterface) {
$duration = Carbon::now()->diffInRealSeconds($duration, false) / 60;
}

return (int) ($duration * 60) > 0 ? $duration : null;
return $duration;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Cache/RetrievesMultipleKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ public function many(array $keys)
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param float|int $minutes
* @param float|int|null $minutes
* @return bool
*/
public function putMany(array $values, $minutes)
{
if (is_null($minutes)) {
driesvints marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

$manyResult = null;

foreach ($values as $key => $value) {
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Contracts/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function pull($key, $default = null);
*
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
driesvints marked this conversation as resolved.
Show resolved Hide resolved
* @return bool
*/
public function put($key, $value, $minutes);
Expand All @@ -48,7 +48,7 @@ public function put($key, $value, $minutes);
*
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
driesvints marked this conversation as resolved.
Show resolved Hide resolved
* @return bool
*/
public function add($key, $value, $minutes);
Expand Down
Loading