Skip to content

Commit

Permalink
lots of refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 1, 2015
1 parent 5ebe13c commit bbd825b
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 347 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Cache/ApcStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ApcStore extends TaggableStore implements Store
{
use DefaultMultipleTrait;
use RetrievesMultipleKeys;

/**
* The APC wrapper instance.
Expand Down Expand Up @@ -38,7 +38,7 @@ public function __construct(ApcWrapper $apc, $prefix = '')
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param string|array $key
* @return mixed
*/
public function get($key)
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Cache/ArrayStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ArrayStore extends TaggableStore implements Store
{
use DefaultMultipleTrait;
use RetrievesMultipleKeys;

/**
* The array of stored values.
Expand All @@ -18,7 +18,7 @@ class ArrayStore extends TaggableStore implements Store
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param string|array $key
* @return mixed
*/
public function get($key)
Expand All @@ -42,7 +42,7 @@ public function put($key, $value, $minutes)
}

/**
* Store multiple items in the cache for a set number of minutes.
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param int $minutes
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DatabaseStore implements Store
{
use DefaultMultipleTrait;
use RetrievesMultipleKeys;

/**
* The database connection instance.
Expand Down Expand Up @@ -60,7 +60,7 @@ public function __construct(ConnectionInterface $connection, EncrypterContract $
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param string|array $key
* @return mixed
*/
public function get($key)
Expand Down
39 changes: 0 additions & 39 deletions src/Illuminate/Cache/DefaultMultipleTrait.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class FileStore implements Store
{
use DefaultMultipleTrait;
use RetrievesMultipleKeys;

/**
* The Illuminate Filesystem instance.
Expand Down Expand Up @@ -41,7 +41,7 @@ public function __construct(Filesystem $files, $directory)
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param string|array $key
* @return mixed
*/
public function get($key)
Expand Down
33 changes: 14 additions & 19 deletions src/Illuminate/Cache/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct($memcached, $prefix = '')
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param string|array $key
* @return mixed
*/
public function get($key)
Expand All @@ -52,29 +52,24 @@ public function get($key)
/**
* Retrieve multiple items from the cache by key.
*
* Items not found in the cache will have a null value for the key.
* Items not found in the cache will have a null value.
*
* @param array $keys
* @return array
*/
public function getMultiple(array $keys)
public function many(array $keys)
{
$prefixedKeys = [];

foreach ($keys as $keyToPrefix) {
$prefixedKeys[] = $this->prefix.$keyToPrefix;
}
$prefixedKeys = array_map(function ($key) {
return $this->prefix.$key;
}, $keys);

$cas = null;
$cacheValues = $this->memcached->getMulti($prefixedKeys, $cas, Memcached::GET_PRESERVE_ORDER);
$values = $this->memcached->getMulti($prefixedKeys, null, Memcached::GET_PRESERVE_ORDER);

if ($this->memcached->getResultCode() != 0) {
return array_fill_keys($keys, null);
}

$returnValues = array_combine($keys, $cacheValues);

return $returnValues;
return array_combine($keys, $values);
}

/**
Expand All @@ -91,21 +86,21 @@ public function put($key, $value, $minutes)
}

/**
* Store multiple items in the cache for a set number of minutes.
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param int $minutes
* @return void
*/
public function putMultiple(array $values, $minutes)
public function putMany(array $values, $minutes)
{
$formattedKeyValues = [];
$prefixedValues = [];

foreach ($values as $keyToPrefix => $singleValue) {
$formattedKeyValues[$this->prefix.$keyToPrefix] = $singleValue;
foreach ($values as $key => $value) {
$prefixedValues[$this->prefix.$key] = $value;
}

$this->memcached->setMulti($formattedKeyValues, $minutes * 60);
$this->memcached->setMulti($prefixedValues, $minutes * 60);
}

/**
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 @@ -6,7 +6,7 @@

class NullStore extends TaggableStore implements Store
{
use DefaultMultipleTrait;
use RetrievesMultipleKeys;

/**
* The array of stored values.
Expand Down
32 changes: 15 additions & 17 deletions src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(Redis $redis, $prefix = '', $connection = 'default')
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param string|array $key
* @return mixed
*/
public function get($key)
Expand All @@ -59,28 +59,26 @@ public function get($key)
/**
* Retrieve multiple items from the cache by key.
*
* Items not found in the cache will have a null value for the key.
* Items not found in the cache will have a null value.
*
* @param array $keys
* @return array
*/
public function getMultiple(array $keys)
public function many(array $keys)
{
$returnValues = [];
$prefixedKeys = [];
$return = [];

foreach ($keys as $keyToPrefix) {
$prefixedKeys[] = $this->prefix.$keyToPrefix;
}
$prefixedKeys = array_map(function ($key) {
return $this->prefix.$key;
}, $keys);

$cacheValues = $this->connection()->mget($prefixedKeys);
$values = $this->connection()->mget($prefixedKeys);

foreach ($cacheValues as $i => $value) {
$key = $keys[$i];
$returnValues[$key] = is_numeric($value) ? $value : unserialize($value);
foreach ($values as $index => $value) {
$return[$keys[$index]] = is_numeric($value) ? $value : unserialize($value);
}

return $returnValues;
return $return;
}

/**
Expand All @@ -101,18 +99,18 @@ public function put($key, $value, $minutes)
}

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

foreach ($values as $key => $singleValue) {
$this->put($key, $singleValue, $minutes);
foreach ($values as $key => $value) {
$this->put($key, $value, $minutes);
}

$this->connection()->exec();
Expand Down
62 changes: 61 additions & 1 deletion src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTime;
use ArrayAccess;
use Carbon\Carbon;
use InvalidArgumentException;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Events\Dispatcher;
Expand Down Expand Up @@ -105,6 +106,10 @@ public function has($key)
*/
public function get($key, $default = null)
{
if (is_array($key)) {
return $this->many($key);
}

$value = $this->store->get($key);

if (is_null($value)) {
Expand All @@ -118,6 +123,37 @@ public function get($key, $default = null)
return $value;
}

/**
* Retrieve multiple items from the cache by key.
*
* Items not found in the cache will have a null value.
*
* @param array $keys
* @return array
*/
public function many(array $keys)
{
$normalizedKeys = [];

foreach ($keys as $key => $value) {
$normalizedKeys[] = is_string($key) ? $key : $value;
}

$values = $this->store->many($normalizedKeys);

foreach ($values as $key => &$value) {
if (is_null($value)) {
$this->fireCacheEvent('missed', [$key]);

$value = isset($keys[$key]) ? value($keys[$key]) : null;
} else {
$this->fireCacheEvent('hit', [$key, $value]);
}
}

return $values;
}

/**
* Retrieve an item from the cache and delete it.
*
Expand All @@ -142,8 +178,12 @@ public function pull($key, $default = null)
* @param \DateTime|int $minutes
* @return void
*/
public function put($key, $value, $minutes)
public function put($key, $value, $minutes = null)
{
if (is_array($key) && filter_var($value, FILTER_VALIDATE_INT) !== false) {
return $this->putMany($key, $value);
}

$minutes = $this->getMinutes($minutes);

if (! is_null($minutes)) {
Expand All @@ -153,6 +193,26 @@ public function put($key, $value, $minutes)
}
}

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

if (! is_null($minutes)) {
$this->store->putMany($values, $minutes);

foreach ($values as $key => $value) {
$this->fireCacheEvent('write', [$key, $value, $minutes]);
}
}
}

/**
* Store an item in the cache if the key does not exist.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Cache/TaggedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TaggedCache implements Store
{
use DefaultMultipleTrait;
use RetrievesMultipleKeys;

/**
* The cache store implementation.
Expand Down Expand Up @@ -52,7 +52,7 @@ public function has($key)
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param string|array $key
* @param mixed $default
* @return mixed
*/
Expand Down
Loading

0 comments on commit bbd825b

Please sign in to comment.