|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Cache\Adapter; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Rob Frawley 2nd <[email protected]> |
| 16 | + */ |
| 17 | +class MemcachedAdapter extends AbstractAdapter |
| 18 | +{ |
| 19 | + private $client; |
| 20 | + |
| 21 | + /** |
| 22 | + * @param \Memcached $client |
| 23 | + */ |
| 24 | + public function __construct(\Memcached $client, $namespace = '', $defaultLifetime = 0) |
| 25 | + { |
| 26 | + parent::__construct($namespace, $defaultLifetime); |
| 27 | + $this->client = $client; |
| 28 | + } |
| 29 | + |
| 30 | + public static function isSupported() |
| 31 | + { |
| 32 | + return extension_loaded('memcached') && version_compare(phpversion('memcached'), '2.2.0', '>='); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritdoc} |
| 37 | + */ |
| 38 | + protected function doSave(array $values, $lifetime) |
| 39 | + { |
| 40 | + return $this->client->setMulti($values, $lifetime) |
| 41 | + && $this->client->getResultCode() === \Memcached::RES_SUCCESS; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritdoc} |
| 46 | + */ |
| 47 | + protected function doFetch(array $ids) |
| 48 | + { |
| 49 | + return $this->client->getMulti($ids); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + protected function doHave($id) |
| 56 | + { |
| 57 | + return $this->client->get($id) !== false |
| 58 | + || $this->client->getResultCode() !== \Memcached::RES_NOTFOUND; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritdoc} |
| 63 | + */ |
| 64 | + protected function doDelete(array $ids) |
| 65 | + { |
| 66 | + $toDelete = count($ids); |
| 67 | + foreach ((array) $this->client->deleteMulti($ids) as $result) { |
| 68 | + if (true === $result || \Memcached::RES_NOTFOUND === $result) { |
| 69 | + --$toDelete; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return 0 === $toDelete; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritdoc} |
| 78 | + */ |
| 79 | + protected function doClear($namespace) |
| 80 | + { |
| 81 | + if (!isset($namespace[0]) || false === $ids = $this->getIdsByPrefix($namespace)) { |
| 82 | + return $this->client->flush(); |
| 83 | + } |
| 84 | + |
| 85 | + $isCleared = true; |
| 86 | + do { |
| 87 | + $isCleared = $this->doDelete($ids) && $isCleared; |
| 88 | + } while ($ids = $this->getIdsByPrefix($namespace)); |
| 89 | + |
| 90 | + return $isCleared; |
| 91 | + } |
| 92 | + |
| 93 | + private function getIdsByPrefix($namespace) |
| 94 | + { |
| 95 | + if (false === $ids = $this->client->getAllKeys()) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + return array_filter((array) $ids, function ($id) use ($namespace) { |
| 100 | + return 0 === strpos($id, $namespace); |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
0 commit comments