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