Skip to content

Commit

Permalink
Honor PSR-16 SimpleCache interface
Browse files Browse the repository at this point in the history
Methods `put`, `set`, `putMany`, `setMultiple`, and `forever` now return boolean values.

Fixes issue #26674
  • Loading branch information
plehatron committed Dec 1, 2018
1 parent d0cf02d commit 90c9569
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 97 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Cache/ApcStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public function get($key)
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @return void
* @return bool
*/
public function put($key, $value, $minutes)
{
$this->apc->put($this->prefix.$key, $value, (int) ($minutes * 60));
return $this->apc->put($this->prefix.$key, $value, (int) ($minutes * 60));
}

/**
Expand Down Expand Up @@ -92,11 +92,11 @@ public function decrement($key, $value = 1)
*
* @param string $key
* @param mixed $value
* @return void
* @return bool
*/
public function forever($key, $value)
{
$this->put($key, $value, 0);
return $this->put($key, $value, 0);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Cache/ArrayStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public function get($key)
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @return void
* @return bool
*/
public function put($key, $value, $minutes)
{
$this->storage[$key] = $value;

return array_key_exists($key, $this->storage);
}

/**
Expand Down Expand Up @@ -71,11 +73,11 @@ public function decrement($key, $value = 1)
*
* @param string $key
* @param mixed $value
* @return void
* @return bool
*/
public function forever($key, $value)
{
$this->put($key, $value, 0);
return $this->put($key, $value, 0);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function get($key)
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @return void
* @return bool
*/
public function put($key, $value, $minutes)
{
Expand All @@ -100,9 +100,9 @@ public function put($key, $value, $minutes)
$expiration = $this->getTime() + (int) ($minutes * 60);

try {
$this->table()->insert(compact('key', 'value', 'expiration'));
return $this->table()->insert(compact('key', 'value', 'expiration'));
} catch (Exception $e) {
$this->table()->where('key', $key)->update(compact('value', 'expiration'));
return $this->table()->where('key', $key)->update(compact('value', 'expiration'));
}
}

Expand Down Expand Up @@ -196,11 +196,11 @@ protected function getTime()
*
* @param string $key
* @param mixed $value
* @return void
* @return bool
*/
public function forever($key, $value)
{
$this->put($key, $value, 5256000);
return $this->put($key, $value, 5256000);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ public function get($key)
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @return void
* @return bool
*/
public function put($key, $value, $minutes)
{
$this->ensureCacheDirectoryExists($path = $this->path($key));

$this->files->put(
$result = $this->files->put(
$path, $this->expiration($minutes).serialize($value), true
);

return $result !== false && $result > 0;
}

/**
Expand Down Expand Up @@ -112,11 +114,11 @@ public function decrement($key, $value = 1)
*
* @param string $key
* @param mixed $value
* @return void
* @return bool
*/
public function forever($key, $value)
{
$this->put($key, $value, 0);
return $this->put($key, $value, 0);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Cache/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public function many(array $keys)
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @return void
* @return bool
*/
public function put($key, $value, $minutes)
{
$this->memcached->set(
return $this->memcached->set(
$this->prefix.$key, $value, $this->calculateExpiration($minutes)
);
}
Expand All @@ -120,7 +120,7 @@ public function put($key, $value, $minutes)
*
* @param array $values
* @param float|int $minutes
* @return void
* @return bool
*/
public function putMany(array $values, $minutes)
{
Expand All @@ -130,7 +130,7 @@ public function putMany(array $values, $minutes)
$prefixedValues[$this->prefix.$key] = $value;
}

$this->memcached->setMulti(
return $this->memcached->setMulti(
$prefixedValues, $this->calculateExpiration($minutes)
);
}
Expand Down Expand Up @@ -179,11 +179,11 @@ public function decrement($key, $value = 1)
*
* @param string $key
* @param mixed $value
* @return void
* @return bool
*/
public function forever($key, $value)
{
$this->put($key, $value, 0);
return $this->put($key, $value, 0);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Illuminate/Cache/NullStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NullStore extends TaggableStore implements Store
*/
public function get($key)
{
//
return null;
}

/**
Expand All @@ -32,11 +32,11 @@ public function get($key)
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @return void
* @return bool
*/
public function put($key, $value, $minutes)
{
//
return false;
}

/**
Expand Down Expand Up @@ -68,22 +68,22 @@ public function decrement($key, $value = 1)
*
* @param string $key
* @param mixed $value
* @return void
* @return bool
*/
public function forever($key, $value)
{
//
return false;
}

/**
* Remove an item from the cache.
*
* @param string $key
* @return void
* @return bool
*/
public function forget($key)
{
//
return true;
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,37 @@ public function many(array $keys)
* @param string $key
* @param mixed $value
* @param float|int $minutes
* @return void
* @return bool
*/
public function put($key, $value, $minutes)
{
$this->connection()->setex(
$result = $this->connection()->setex(
$this->prefix.$key, (int) max(1, $minutes * 60), $this->serialize($value)
);

return $result ? true : false;
}

/**
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param float|int $minutes
* @return void
* @return bool
*/
public function putMany(array $values, $minutes)
{
$this->connection()->multi();

$resultMany = null;
foreach ($values as $key => $value) {
$this->put($key, $value, $minutes);
$result = $this->put($key, $value, $minutes);
$resultMany = is_null($resultMany) ? $result : $result && $resultMany;
}

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

return $resultMany ?: false;
}

/**
Expand Down Expand Up @@ -158,11 +164,13 @@ public function decrement($key, $value = 1)
*
* @param string $key
* @param mixed $value
* @return void
* @return bool
*/
public function forever($key, $value)
{
$this->connection()->set($this->prefix.$key, $this->serialize($value));
$result = $this->connection()->set($this->prefix.$key, $this->serialize($value));

return $result ? true : false;
}

/**
Expand Down
30 changes: 20 additions & 10 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,55 +194,63 @@ public function pull($key, $default = null)
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
* @return void
* @return bool
*/
public function put($key, $value, $minutes = null)
{
if (is_array($key)) {
$this->putMany($key, $value);
$result = $this->putMany($key, $value);

return;
return $result;
}

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

$this->event(new KeyWritten($key, $value, $minutes));

return $result;
}

return false;
}

/**
* {@inheritdoc}
*/
public function set($key, $value, $ttl = null)
{
$this->put($key, $value, $ttl);
return $this->put($key, $value, $ttl);
}

/**
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @return void
* @return bool
*/
public function putMany(array $values, $minutes)
{
if (! is_null($minutes = $this->getMinutes($minutes))) {
$this->store->putMany($values, $minutes);
$result = $this->store->putMany($values, $minutes);

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

return $result;
}

return false;
}

/**
* {@inheritdoc}
*/
public function setMultiple($values, $ttl = null)
{
$this->putMany($values, $ttl);
return $this->putMany($values, $ttl);
}

/**
Expand Down Expand Up @@ -309,13 +317,15 @@ public function decrement($key, $value = 1)
*
* @param string $key
* @param mixed $value
* @return void
* @return bool
*/
public function forever($key, $value)
{
$this->store->forever($this->itemKey($key), $value);
$result = $this->store->forever($this->itemKey($key), $value);

$this->event(new KeyWritten($key, $value, 0));

return $result;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Illuminate/Cache/RetrievesMultipleKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ public function many(array $keys)
*
* @param array $values
* @param float|int $minutes
* @return void
* @return bool
*/
public function putMany(array $values, $minutes)
{
$resultMany = null;
foreach ($values as $key => $value) {
$this->put($key, $value, $minutes);
$result = $this->put($key, $value, $minutes);
$resultMany = is_null($resultMany) ? $result : $result && $resultMany;
}

return $resultMany ?: false;
}
}
Loading

0 comments on commit 90c9569

Please sign in to comment.