|
2 | 2 |
|
3 | 3 | namespace ApolloPY\Memcached;
|
4 | 4 |
|
| 5 | +use Memcached; |
| 6 | +use ReflectionMethod; |
5 | 7 | use Illuminate\Cache\MemcachedStore as AbstractMemcachedStore;
|
6 | 8 |
|
7 | 9 | /**
|
|
12 | 14 | class MemcachedStore extends AbstractMemcachedStore
|
13 | 15 | {
|
14 | 16 | /**
|
| 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 |
15 | 68 | * @param array $keys
|
16 | 69 | * @return array|bool
|
17 | 70 | */
|
|
0 commit comments