Skip to content

Commit d301c44

Browse files
committed
add getMultiple and putMultiple to cache implementations
1 parent 54af638 commit d301c44

18 files changed

Lines changed: 303 additions & 1 deletion

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"iron-io/iron_mq": "~2.0",
7777
"mockery/mockery": "~0.9.2",
7878
"pda/pheanstalk": "~3.0",
79-
"phpunit/phpunit": "~4.0",
79+
"phpunit/phpunit": "~4.1",
8080
"predis/predis": "~1.0",
8181
"symfony/css-selector": "2.8.*|3.0.*",
8282
"symfony/dom-crawler": "2.8.*|3.0.*"

src/Illuminate/Cache/ApcStore.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class ApcStore extends TaggableStore implements Store
88
{
9+
use DefaultMultipleTrait;
10+
911
/**
1012
* The APC wrapper instance.
1113
*

src/Illuminate/Cache/ArrayStore.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class ArrayStore extends TaggableStore implements Store
88
{
9+
use DefaultMultipleTrait;
10+
911
/**
1012
* The array of stored values.
1113
*
@@ -39,6 +41,18 @@ public function put($key, $value, $minutes)
3941
$this->storage[$key] = $value;
4042
}
4143

44+
/**
45+
* Store multiple items in the cache for a set number of minutes.
46+
*
47+
* @param array $values
48+
* @param int $minutes
49+
* @return void
50+
*/
51+
public function putMultiple(array $values, $minutes)
52+
{
53+
$this->storage += $values;
54+
}
55+
4256
/**
4357
* Increment the value of an item in the cache.
4458
*

src/Illuminate/Cache/DatabaseStore.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class DatabaseStore implements Store
1212
{
13+
use DefaultMultipleTrait;
14+
1315
/**
1416
* The database connection instance.
1517
*
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Illuminate\Cache;
4+
5+
trait DefaultMultipleTrait
6+
{
7+
/**
8+
* Retrieve multiple items from the cache by key.
9+
*
10+
* Items not found in the cache will have a null value for the key.
11+
*
12+
* @param array $keys
13+
* @return array
14+
*/
15+
public function getMultiple(array $keys)
16+
{
17+
$returnValues = [];
18+
19+
foreach ($keys as $singleKey) {
20+
$returnValues[$singleKey] = $this->get($singleKey);
21+
}
22+
23+
return $returnValues;
24+
}
25+
26+
/**
27+
* Store multiple items in the cache for a set number of minutes.
28+
*
29+
* @param array $values
30+
* @param int $minutes
31+
* @return void
32+
*/
33+
public function putMultiple(array $values, $minutes)
34+
{
35+
foreach ($values as $key => $singleValue) {
36+
$this->put($key, $singleValue, $minutes);
37+
}
38+
}
39+
}

src/Illuminate/Cache/FileStore.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class FileStore implements Store
1111
{
12+
use DefaultMultipleTrait;
13+
1214
/**
1315
* The Illuminate Filesystem instance.
1416
*

src/Illuminate/Cache/MemcachedStore.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Cache;
44

5+
use Memcached;
56
use Illuminate\Contracts\Cache\Store;
67

78
class MemcachedStore extends TaggableStore implements Store
@@ -48,6 +49,34 @@ public function get($key)
4849
}
4950
}
5051

52+
/**
53+
* Retrieve multiple items from the cache by key.
54+
*
55+
* Items not found in the cache will have a null value for the key.
56+
*
57+
* @param array $keys
58+
* @return array
59+
*/
60+
public function getMultiple(array $keys)
61+
{
62+
$prefixedKeys = [];
63+
64+
foreach ($keys as $keyToPrefix) {
65+
$prefixedKeys[] = $this->prefix.$keyToPrefix;
66+
}
67+
68+
$cas = null;
69+
$cacheValues = $this->memcached->getMulti($prefixedKeys, $cas, Memcached::GET_PRESERVE_ORDER);
70+
71+
if ($this->memcached->getResultCode() != 0) {
72+
return array_fill_keys($keys, null);
73+
}
74+
75+
$returnValues = array_combine($keys, $cacheValues);
76+
77+
return $returnValues;
78+
}
79+
5180
/**
5281
* Store an item in the cache for a given number of minutes.
5382
*
@@ -61,6 +90,24 @@ public function put($key, $value, $minutes)
6190
$this->memcached->set($this->prefix.$key, $value, $minutes * 60);
6291
}
6392

93+
/**
94+
* Store multiple items in the cache for a set number of minutes.
95+
*
96+
* @param array $values
97+
* @param int $minutes
98+
* @return void
99+
*/
100+
public function putMultiple(array $values, $minutes)
101+
{
102+
$formattedKeyValues = [];
103+
104+
foreach ($values as $keyToPrefix => $singleValue) {
105+
$formattedKeyValues[$this->prefix.$keyToPrefix] = $singleValue;
106+
}
107+
108+
$this->memcached->setMulti($formattedKeyValues, $minutes * 60);
109+
}
110+
64111
/**
65112
* Store an item in the cache if the key doesn't exist.
66113
*

src/Illuminate/Cache/NullStore.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class NullStore extends TaggableStore implements Store
88
{
9+
use DefaultMultipleTrait;
10+
911
/**
1012
* The array of stored values.
1113
*

src/Illuminate/Cache/RedisStore.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ public function get($key)
5656
}
5757
}
5858

59+
/**
60+
* Retrieve multiple items from the cache by key.
61+
*
62+
* Items not found in the cache will have a null value for the key.
63+
*
64+
* @param array $keys
65+
* @return array
66+
*/
67+
public function getMultiple(array $keys)
68+
{
69+
$returnValues = [];
70+
$prefixedKeys = [];
71+
72+
foreach ($keys as $keyToPrefix) {
73+
$prefixedKeys[] = $this->prefix.$keyToPrefix;
74+
}
75+
76+
$cacheValues = $this->connection()->mget($prefixedKeys);
77+
78+
foreach ($cacheValues as $i => $value) {
79+
$key = $keys[$i];
80+
$returnValues[$key] = is_numeric($value) ? $value : unserialize($value);
81+
}
82+
83+
return $returnValues;
84+
}
85+
5986
/**
6087
* Store an item in the cache for a given number of minutes.
6188
*
@@ -73,6 +100,24 @@ public function put($key, $value, $minutes)
73100
$this->connection()->setex($this->prefix.$key, $minutes * 60, $value);
74101
}
75102

103+
/**
104+
* Store multiple items in the cache for a set number of minutes.
105+
*
106+
* @param array $values
107+
* @param int $minutes
108+
* @return void
109+
*/
110+
public function putMultiple(array $values, $minutes)
111+
{
112+
$this->connection()->multi();
113+
114+
foreach ($values as $key => $singleValue) {
115+
$this->put($key, $singleValue, $minutes);
116+
}
117+
118+
$this->connection()->exec();
119+
}
120+
76121
/**
77122
* Increment the value of an item in the cache.
78123
*

src/Illuminate/Cache/TaggedCache.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class TaggedCache implements Store
1111
{
12+
use DefaultMultipleTrait;
13+
1214
/**
1315
* The cache store implementation.
1416
*

0 commit comments

Comments
 (0)