Skip to content

Commit

Permalink
Merge branch 'cache-multi-5.2' of https://github.com/nicholas-eden/fr…
Browse files Browse the repository at this point in the history
…amework into nicholas-eden-cache-multi-5.2
  • Loading branch information
taylorotwell committed Dec 1, 2015
2 parents 8fc1942 + d301c44 commit 5ebe13c
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"iron-io/iron_mq": "~2.0",
"mockery/mockery": "~0.9.2",
"pda/pheanstalk": "~3.0",
"phpunit/phpunit": "~4.0",
"phpunit/phpunit": "~4.1",
"predis/predis": "~1.0",
"symfony/css-selector": "2.8.*|3.0.*",
"symfony/dom-crawler": "2.8.*|3.0.*"
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Cache/ApcStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class ApcStore extends TaggableStore implements Store
{
use DefaultMultipleTrait;

/**
* The APC wrapper instance.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Cache/ArrayStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class ArrayStore extends TaggableStore implements Store
{
use DefaultMultipleTrait;

/**
* The array of stored values.
*
Expand Down Expand Up @@ -39,6 +41,18 @@ public function put($key, $value, $minutes)
$this->storage[$key] = $value;
}

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

/**
* Increment the value of an item in the cache.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class DatabaseStore implements Store
{
use DefaultMultipleTrait;

/**
* The database connection instance.
*
Expand Down
39 changes: 39 additions & 0 deletions src/Illuminate/Cache/DefaultMultipleTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Illuminate\Cache;

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

foreach ($keys as $singleKey) {
$returnValues[$singleKey] = $this->get($singleKey);
}

return $returnValues;
}

/**
* Store multiple items in the cache for a set number of minutes.
*
* @param array $values
* @param int $minutes
* @return void
*/
public function putMultiple(array $values, $minutes)
{
foreach ($values as $key => $singleValue) {
$this->put($key, $singleValue, $minutes);
}
}
}
2 changes: 2 additions & 0 deletions src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class FileStore implements Store
{
use DefaultMultipleTrait;

/**
* The Illuminate Filesystem instance.
*
Expand Down
47 changes: 47 additions & 0 deletions src/Illuminate/Cache/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Cache;

use Memcached;
use Illuminate\Contracts\Cache\Store;

class MemcachedStore extends TaggableStore implements Store
Expand Down Expand Up @@ -48,6 +49,34 @@ 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.
*
* @param array $keys
* @return array
*/
public function getMultiple(array $keys)
{
$prefixedKeys = [];

foreach ($keys as $keyToPrefix) {
$prefixedKeys[] = $this->prefix.$keyToPrefix;
}

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

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

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

return $returnValues;
}

/**
* Store an item in the cache for a given number of minutes.
*
Expand All @@ -61,6 +90,24 @@ public function put($key, $value, $minutes)
$this->memcached->set($this->prefix.$key, $value, $minutes * 60);
}

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

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

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

/**
* Store an item in the cache if the key doesn't exist.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Cache/NullStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class NullStore extends TaggableStore implements Store
{
use DefaultMultipleTrait;

/**
* The array of stored values.
*
Expand Down
45 changes: 45 additions & 0 deletions src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ 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.
*
* @param array $keys
* @return array
*/
public function getMultiple(array $keys)
{
$returnValues = [];
$prefixedKeys = [];

foreach ($keys as $keyToPrefix) {
$prefixedKeys[] = $this->prefix.$keyToPrefix;
}

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

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

return $returnValues;
}

/**
* Store an item in the cache for a given number of minutes.
*
Expand All @@ -73,6 +100,24 @@ public function put($key, $value, $minutes)
$this->connection()->setex($this->prefix.$key, $minutes * 60, $value);
}

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

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

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

/**
* Increment the value of an item in the cache.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Cache/TaggedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class TaggedCache implements Store
{
use DefaultMultipleTrait;

/**
* The cache store implementation.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Cache/WinCacheStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class WinCacheStore extends TaggableStore implements Store
{
use DefaultMultipleTrait;

/**
* A string that should be prepended to keys.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Cache/XCacheStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class XCacheStore extends TaggableStore implements Store
{
use DefaultMultipleTrait;

/**
* A string that should be prepended to keys.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Contracts/Cache/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ interface Store
*/
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.
*
* @param array $keys
* @return array
*/
public function getMultiple(array $keys);

/**
* Store an item in the cache for a given number of minutes.
*
Expand All @@ -22,6 +32,15 @@ public function get($key);
*/
public function put($key, $value, $minutes);

/**
* Store multiple items in the cache for a set number of minutes.
*
* @param array $values
* @param int $minutes
* @return void
*/
public function putMultiple(array $values, $minutes);

/**
* Increment the value of an item in the cache.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Cache/CacheApcStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ public function testAPCValueIsReturned()
$this->assertEquals('bar', $store->get('foo'));
}

public function testGetMultipleReturnsNullWhenNotFoundAndValueWhenFound()
{
$apc = $this->getMock('Illuminate\Cache\ApcWrapper', ['get']);
$apc->expects($this->exactly(3))->method('get')->willReturnMap([
['foo', 'qux'],
['bar', null],
['baz', 'norf'],
]);
$store = new Illuminate\Cache\ApcStore($apc);
$this->assertEquals([
'foo' => 'qux',
'bar' => null,
'baz' => 'norf',
], $store->getMultiple(['foo', 'bar', 'baz']));
}

public function testSetMethodProperlyCallsAPC()
{
$apc = $this->getMock('Illuminate\Cache\ApcWrapper', ['put']);
Expand All @@ -26,6 +42,24 @@ public function testSetMethodProperlyCallsAPC()
$store->put('foo', 'bar', 1);
}

public function testSetMultipleMethodProperlyCallsAPC()
{
$apc = $this->getMock('Illuminate\Cache\ApcWrapper', ['put']);
$apc->expects($this->exactly(3))->method('put')->withConsecutive([
$this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(60),
], [
$this->equalTo('baz'), $this->equalTo('qux'), $this->equalTo(60),
], [
$this->equalTo('bar'), $this->equalTo('norf'), $this->equalTo(60),
]);
$store = new Illuminate\Cache\ApcStore($apc);
$store->putMultiple([
'foo' => 'bar',
'baz' => 'qux',
'bar' => 'norf',
], 1);
}

public function testIncrementMethodProperlyCallsAPC()
{
$apc = $this->getMock('Illuminate\Cache\ApcWrapper', ['increment']);
Expand Down
16 changes: 16 additions & 0 deletions tests/Cache/CacheArrayStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ public function testItemsCanBeSetAndRetrieved()
$this->assertEquals('bar', $store->get('foo'));
}

public function testMultipleItemsCanBeSetAndRetrieved()
{
$store = new ArrayStore;
$store->put('foo', 'bar', 10);
$store->putMultiple([
'fizz' => 'buz',
'quz' => 'baz',
], 10);
$this->assertEquals([
'foo' => 'bar',
'fizz' => 'buz',
'quz' => 'baz',
'norf' => null,
], $store->getMultiple(['foo', 'fizz', 'quz', 'norf']));
}

public function testStoreItemForeverProperlyStoresInArray()
{
$mock = $this->getMock('Illuminate\Cache\ArrayStore', ['put']);
Expand Down
Loading

0 comments on commit 5ebe13c

Please sign in to comment.