8000 Add workaround to use Memcached 3.0.0 with Many of Laravel 5.2 · apollopy/laravel-memcached@ffb8b60 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Oct 9, 2019. It is now read-only.

Commit ffb8b60

Browse files
committed
Add workaround to use Memcached 3.0.0 with Many of Laravel 5.2
laravel/framework#15739
1 parent dc6ab09 commit ffb8b60

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/MemcachedStore.php

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

33
namespace ApolloPY\Memcached;
44

5+
use Memcached;
6+
use ReflectionMethod;
57
use Illuminate\Cache\MemcachedStore as AbstractMemcachedStore;
68

79
/**
@@ -12,6 +14,57 @@
1214
class MemcachedStore extends AbstractMemcachedStore
1315
{
1416
/**
17+
* Indicates whether we are using Memcached version >= 3.0.0.
18+
*
19+
* @var bool
20+
*/
21+
protected $onVersionThree;
22+
23+
/**
24+
* Create a new Memcached store.
25+
*
26+
* @param \Memcached $memcached
27+
* @param string $prefix
28+
*/
29+
public function __construct($memcached, $prefix = '')
30+
{
31+
parent::__construct($memcached, $prefix);
32+
33+
$this->onVersionThree = (new ReflectionMethod('Memcached', 'getMulti'))
34+
->getNumberOfParameters() == 2;
35+
}
36+
37+
/**
38+
* Retrieve multiple items from the cache by key.
39+
*
40+
* Items not found in the cache will have a null value.
41+
*
42+
* @param array $keys
43+
* @return array
44+
*/
45+
public function many(array $keys)
46+
{
47+
$prefixedKeys = array_map(function ($key) {
48+
return $this->prefix.$key;
49+
}, $keys);
50+
51+
if ($this->onVersionThree) {
52+
$values = $this->memcached->getMulti($prefixedKeys, Memcached::GET_PRESERVE_ORDER);
53+
} else {
54+
$null = null;
55+
56+
$values = $this->memcached->getMulti($prefixedKeys, $null, Memcached::GET_PRESERVE_ORDER);
57+
}
58+
59+
if ($this->memcached->getResultCode() != 0) {
60+
return array_fill_keys($keys, null);
61+
}
62+
63+
return array_combine($keys, $values);
64+
}
65+
66+
/**
67+
* @deprecated use function many
1568
* @param array $keys
1669
* @return array|bool
1770
*/

0 commit comments

Comments
 (0)
0