|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Cache\Adapter;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Cache\Exception\CacheException; |
| 15 | +use Symfony\Component\Cache\Exception\InvalidArgumentException; |
| 16 | + |
14 | 17 | /**
|
15 | 18 | * @author Rob Frawley 2nd <[email protected]>
|
| 19 | + * @author Nicolas Grekas <[email protected]> |
16 | 20 | */
|
17 | 21 | class MemcachedAdapter extends AbstractAdapter
|
18 | 22 | {
|
| 23 | + private static $defaultClientOptions = array( |
| 24 | + 'persistent_id' => null, |
| 25 | + 'username' => null, |
| 26 | + 'password' => null, |
| 27 | + 'binary_protocol' => true, |
| 28 | + 'libketama_compatible' => true, |
| 29 | + ); |
| 30 | + |
| 31 | + protected $maxIdLength = 250; |
| 32 | + |
19 | 33 | private $client;
|
20 | 34 |
|
| 35 | + public static function isSupported() |
| 36 | + { |
| 37 | + return extension_loaded('memcached') && version_compare(phpversion('memcached'), '2.2.0', '>='); |
| 38 | + } |
| 39 | + |
21 | 40 | public function __construct(\Memcached $client, $namespace = '', $defaultLifetime = 0)
|
22 | 41 | {
|
| 42 | + if (!static::isSupported()) { |
| 43 | + throw new CacheException('Memcached >= 2.2.0 is required'); |
| 44 | + } |
| 45 | + $opt = $client->getOption(\Memcached::OPT_SERIALIZER); |
| 46 | + if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) { |
| 47 | + throw new CacheException('MemcachedAdapter: serializer must be PHP or IGBINARY.'); |
| 48 | + } |
| 49 | + if (!$client->getOption(\Memcached::OPT_BINARY_PROTOCOL)) { |
| 50 | + throw new CacheException('MemcachedAdapter: OPT_BINARY_PROTOCOL must be used.'); |
| 51 | + } |
| 52 | + if ($client->getOption(\Memcached::OPT_NO_BLOCK)) { |
| 53 | + throw new CacheException('MemcachedAdapter: OPT_NO_BLOCK must be disabled.'); |
| 54 | + } |
| 55 | + $this->maxIdLength -= strlen($client->getOption(\Memcached::OPT_PREFIX_KEY)); |
| 56 | + |
23 | 57 | parent::__construct($namespace, $defaultLifetime);
|
24 | 58 | $this->client = $client;
|
25 | 59 | }
|
26 | 60 |
|
27 |
| - public static function isSupported() |
| 61 | + /** |
| 62 | + * @return \Memcached |
| 63 | + * |
| 64 | + * @throws \ErrorEception When invalid options or servers are provided. |
| 65 | + */ |
| 66 | + public static function createConnection($servers, array $options = array()) |
28 | 67 | {
|
29 |
| - return extension_loaded('memcached') && version_compare(phpversion('memcached'), '2.2.0', '>='); |
| 68 | + if (is_string($servers)) { |
| 69 | + $servers = array($servers); |
| 70 | + } elseif (!is_array($servers)) { |
| 71 | + throw new InvalidArgumentException(sprintf('MemcachedAdapter::createClient() expects array or string as first argument, %s given.', get_type($servers))); |
| 72 | + } |
| 73 | + set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); }); |
| 74 | + try { |
| 75 | + $options += static::$defaultClientOptions; |
| 76 | + $client = new \Memcached($options['persistent_id']); |
| 77 | + $username = $options['username']; |
| 78 | + $password = $options['password']; |
| 79 | + unset($options['persistent_id'], $options['username'], $options['password']); |
| 80 | + |
| 81 | + // set client's options |
| 82 | + foreach ($options as $name => $value) { |
| 83 | + if (is_int($name)) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + $ucName = strtoupper($name); |
| 87 | + $opt = constant('Memcached::OPT_'.$ucName); |
| 88 | + |
| 89 | + if ('HASH' === $ucName || 'SERIALIZER' === $ucName || 'DISTRIBUTION' === $ucName) { |
| 90 | + $value = constant('Memcached::'.$ucName.'_'.strtoupper($value)); |
| 91 | + } |
| 92 | + |
| 93 | + unset($options[$name]); |
| 94 | + $options[$opt] = $value; |
| 95 | + } |
| 96 | + $client->setOptions($options); |
| 97 | + |
| 98 | + // parse any DSN in $servers |
| 99 | + foreach ($servers as $i => $dsn) { |
| 100 | + if (is_array($dsn)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + if (0 !== strpos($dsn, 'memcached://')) { |
| 104 | + throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s does not start with "memcached://"', $dsn)); |
| 105 | + } |
| 106 | + $params = preg_replace_callback('#^memcached://(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) { |
| 107 | + if (!empty($m[1])) { |
| 108 | + list($username, $password) = explode(':', $m[1], 2) + array(1 => null); |
| 109 | + } |
| 110 | + |
| 111 | + return 'file://'; |
| 112 | + }, $dsn); |
| 113 | + if (false === $params = parse_url($params)) { |
| 114 | + throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s', $dsn)); |
| 115 | + } |
| 116 | + if (!isset($params['host']) && !isset($params['path'])) { |
| 117 | + throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s', $dsn)); |
| 118 | + } |
| 119 | + if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) { |
| 120 | + $params['weight'] = $m[1]; |
| 121 | + $params['path'] = substr($params['path'], 0, -strlen($m[0])); |
| 122 | + } |
| 123 | + $params += array( |
| 124 | + 'host' => isset($params['host']) ? $params['host'] : $params['path'], |
| 125 | + 'port' => isset($params['host']) ? 11211 : null, |
| 126 | + 'weight' => 0, |
| 127 | + ); |
| 128 | + if (isset($params['query'])) { |
| 129 | + parse_str($params['query'], $query); |
| 130 | + $params += $query; |
| 131 | + } |
| 132 | + |
| 133 | + $servers[$i] = array($params['host'], $params['port'], $params['weight']); |
| 134 | + } |
| 135 | + |
| 136 | + // set client's servers, taking care of persistent connections |
| 137 | + if (!$client->isPristine()) { |
| 138 | + $oldServers = array(); |
| 139 | + foreach ($client->getServerList() as $server) { |
| 140 | + $oldServers[] = array($server['host'], $server['port']); |
| 141 | + } |
| 142 | + |
| 143 | + $newServers = array(); |
| 144 | + foreach ($servers as $server) { |
| 145 | + if (1 < count($server)) { |
| 146 | + $server = array_values($server); |
| 147 | + unset($server[2]); |
| 148 | + $server[1] = (int) $server[1]; |
| 149 | + } |
| 150 | + $newServers[] = $server; |
| 151 | + } |
| 152 | + |
| 153 | + if ($oldServers !== $newServers) { |
| 154 | + // before resetting, ensure $servers is valid |
| 155 | + $client->addServers($servers); |
| 156 | + $client->resetServerList(); |
| 157 | + } |
| 158 | + } |
| 159 | + $client->addServers($servers); |
| 160 | + |
| 161 | + if (null !== $username || null !== $password) { |
| 162 | + if (!method_exists($client, 'setSaslAuthData')) { |
| 163 | + trigger_error('Missing SASL support: the memcached extension must be compiled with --enable-memcached-sasl.'); |
| 164 | + } |
| 165 | + $client->setSaslAuthData($username, $password); |
| 166 | + } |
| 167 | + |
| 168 | + return $client; |
| 169 | + } finally { |
| 170 | + restore_error_handler(); |
| 171 | + } |
30 | 172 | }
|
31 | 173 |
|
32 | 174 | /**
|
33 | 175 | * {@inheritdoc}
|
34 | 176 | */
|
35 | 177 | protected function doSave(array $values, $lifetime)
|
36 | 178 | {
|
37 |
| - return $this->client->setMulti($values, $lifetime) && $this->client->getResultCode() === \Memcached::RES_SUCCESS; |
| 179 | + return $this->checkResultCode($this->client->setMulti($values, $lifetime)); |
38 | 180 | }
|
39 | 181 |
|
40 | 182 | /**
|
41 | 183 | * {@inheritdoc}
|
42 | 184 | */
|
43 | 185 | protected function doFetch(array $ids)
|
44 | 186 | {
|
45 |
| - return $this->client->getMulti($ids); |
| 187 | + return $this->checkResultCode($this->client->getMulti($ids)); |
46 | 188 | }
|
47 | 189 |
|
48 | 190 | /**
|
49 | 191 | * {@inheritdoc}
|
50 | 192 | */
|
51 | 193 | protected function doHave($id)
|
52 | 194 | {
|
53 |
| - return $this->client->get($id) !== false || $this->client->getResultCode() === \Memcached::RES_SUCCESS; |
| 195 | + return false !== $this->client->get($id) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode()); |
54 | 196 | }
|
55 | 197 |
|
56 | 198 | /**
|
57 | 199 | * {@inheritdoc}
|
58 | 200 | */
|
59 | 201 | protected function doDelete(array $ids)
|
60 | 202 | {
|
61 |
| - $toDelete = count($ids); |
62 |
| - foreach ($this->client->deleteMulti($ids) as $result) { |
63 |
| - if (\Memcached::RES_SUCCESS === $result || \Memcached::RES_NOTFOUND === $result) { |
64 |
| - --$toDelete; |
| 203 | + $ok = true; |
| 204 | + foreach ($this->checkResultCode($this->client->deleteMulti($ids)) as $result) { |
| 205 | + if (\Memcached::RES_SUCCESS !== $result && \Memcached::RES_NOTFOUND !== $result) { |
| 206 | + $ok = false; |
65 | 207 | }
|
66 | 208 | }
|
67 | 209 |
|
68 |
| - return 0 === $toDelete; |
| 210 | + return $ok; |
69 | 211 | }
|
70 | 212 |
|
71 | 213 | /**
|
72 | 214 | * {@inheritdoc}
|
73 | 215 | */
|
74 | 216 | protected function doClear($namespace)
|
75 | 217 | {
|
76 |
| - return $this->client->flush(); |
| 218 | + return $this->checkResultCode($this->client->flush()); |
| 219 | + } |
| 220 | + |
| 221 | + private function checkResultCode($result) |
| 222 | + { |
| 223 | + $code = $this->client->getResultCode(); |
| 224 | + |
| 225 | + if (\Memcached::RES_SUCCESS === $code || \Memcached::RES_NOTFOUND === $code) { |
| 226 | + return $result; |
| 227 | + } |
| 228 | + |
| 229 | + throw new CacheException(sprintf('MemcachedAdapter client error: %s.', strtolower($this->client->getResultMessage()))); |
77 | 230 | }
|
78 | 231 | }
|
0 commit comments