Skip to content

Commit

Permalink
Normalize PhpRedis GET results
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkruss committed Jan 27, 2017
1 parent 88a907c commit f352597
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public function get($key)
{
$value = $this->connection()->get($this->prefix.$key);

if (! is_null($value) && $value !== false) {
return $this->unserialize($value);
}
return ! is_null($value) ? $this->unserialize($value) : null;
}

/**
Expand Down
16 changes: 6 additions & 10 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public function __construct(Store $store)
*/
public function has($key)
{
$value = $this->get($key);

return ! is_null($value) && $value !== false;
return ! is_null($this->get($key));
}

/**
Expand All @@ -85,7 +83,7 @@ public function get($key, $default = null)
// If we could not find the cache value, we will fire the missed event and get
// the default value for this cache value. This default could be a callback
// so we will execute the value function which will resolve it if needed.
if (is_null($value) || $value === false) {
if (is_null($value)) {
$this->event(new CacheMissed($key));

$value = value($default);
Expand Down Expand Up @@ -128,7 +126,7 @@ protected function handleManyResult($keys, $key, $value)
// If we could not find the cache value, we will fire the missed event and get
// the default value for this cache value. This default could be a callback
// so we will execute the value function which will resolve it if needed.
if (is_null($value) || $value === false) {
if (is_null($value)) {
$this->event(new CacheMissed($key));

return isset($keys[$key]) ? value($keys[$key]) : null;
Expand Down Expand Up @@ -218,12 +216,10 @@ public function add($key, $value, $minutes)
);
}

$exists = $this->get($key);

// If the value did not exist in the cache, we will put the value in the cache
// 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($exists) || $exists === false) {
if (is_null($this->get($key))) {
$this->put($key, $value, $minutes);

return true;
Expand Down Expand Up @@ -285,7 +281,7 @@ public function remember($key, $minutes, Closure $callback)
// If the item exists in the cache we will just return this immediately and if
// not we will execute the given Closure and cache the result of that for a
// given number of minutes so it's available for all subsequent requests.
if (! is_null($value) && $value !== false) {
if (! is_null($value)) {
return $value;
}

Expand Down Expand Up @@ -320,7 +316,7 @@ public function rememberForever($key, Closure $callback)
// If the item exists in the cache we will just return this immediately and if
// not we will execute the given Closure and cache the result of that for a
// given number of minutes so it's available for all subsequent requests.
if (! is_null($value) && $value !== false) {
if (! is_null($value)) {
return $value;
}

Expand Down
26 changes: 26 additions & 0 deletions src/Illuminate/Redis/Connections/PhpRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ public function __construct($client)
$this->client = $client;
}

/**
* Returns the value of the given key.
*
* @param string $key
* @return string|null
*/
public function get($key)
{
$result = $this->client->get($key);

return $result !== false ? $result : null;
}

/**
* Get the values of all the given keys.
*
* @param array $keys
* @return array
*/
public function mget(array $keys)
{
return array_map(function ($value) {
return $value !== false ? $value : null;
}, $this->client->mget($keys));
}

/**
* Set the string value in argument as value of the key.
*
Expand Down

0 comments on commit f352597

Please sign in to comment.