|
24 | 24 | */ |
25 | 25 | class MemcachedStore implements StoreInterface |
26 | 26 | { |
| 27 | + private static $defaultClientOptions = array( |
| 28 | + 'persistent_id' => null, |
| 29 | + 'username' => null, |
| 30 | + 'password' => null, |
| 31 | + ); |
| 32 | + |
27 | 33 | private $memcached; |
28 | 34 | private $initialTtl; |
29 | 35 | /** @var bool */ |
@@ -52,6 +58,128 @@ public function __construct(\Memcached $memcached, $initialTtl = 300) |
52 | 58 | $this->initialTtl = $initialTtl; |
53 | 59 | } |
54 | 60 |
|
| 61 | + /** |
| 62 | + * Creates a Memcached instance. |
| 63 | + * |
| 64 | + * By default, the binary protocol, block, and libketama compatible options are enabled. |
| 65 | + * |
| 66 | + * Example DSN: |
| 67 | + * - 'memcached://user:pass@localhost?weight=33' |
| 68 | + * - array(array('localhost', 11211, 33)) |
| 69 | + * |
| 70 | + * @param string $dsn A server or A DSN |
| 71 | + * @param array $options An array of options |
| 72 | + * |
| 73 | + * @return \Memcached |
| 74 | + * |
| 75 | + * @throws \ErrorEception When invalid options or server are provided |
| 76 | + */ |
| 77 | + public static function createConnection($server, array $options = array()) |
| 78 | + { |
| 79 | + if (!static::isSupported()) { |
| 80 | + throw new InvalidArgumentException('Memcached extension is required'); |
| 81 | + } |
| 82 | + set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); }); |
| 83 | + try { |
| 84 | + $options += static::$defaultClientOptions; |
| 85 | + $client = new \Memcached($options['persistent_id']); |
| 86 | + $username = $options['username']; |
| 87 | + $password = $options['password']; |
| 88 | + |
| 89 | + // parse any DSN in $server |
| 90 | + if (is_string($server)) { |
| 91 | + if (0 !== strpos($server, 'memcached://')) { |
| 92 | + throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s does not start with "memcached://"', $server)); |
| 93 | + } |
| 94 | + $params = preg_replace_callback('#^memcached://(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) { |
| 95 | + if (!empty($m[1])) { |
| 96 | + list($username, $password) = explode(':', $m[1], 2) + array(1 => null); |
| 97 | + } |
| 98 | + |
| 99 | + return 'file://'; |
| 100 | + }, $server); |
| 101 | + if (false === $params = parse_url($params)) { |
| 102 | + throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s', $server)); |
| 103 | + } |
| 104 | + if (!isset($params['host']) && !isset($params['path'])) { |
| 105 | + throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s', $server)); |
| 106 | + } |
| 107 | + if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) { |
| 108 | + $params['weight'] = $m[1]; |
| 109 | + $params['path'] = substr($params['path'], 0, -strlen($m[0])); |
| 110 | + } |
| 111 | + $params += array( |
| 112 | + 'host' => isset($params['host']) ? $params['host'] : $params['path'], |
| 113 | + 'port' => isset($params['host']) ? 11211 : null, |
| 114 | + 'weight' => 0, |
| 115 | + ); |
| 116 | + if (isset($params['query'])) { |
| 117 | + parse_str($params['query'], $query); |
| 118 | + $params += $query; |
| 119 | + $options = $query + $options; |
| 120 | + } |
| 121 | + |
| 122 | + $server = array($params['host'], $params['port'], $params['weight']); |
| 123 | + } |
| 124 | + |
| 125 | + // set client's options |
| 126 | + unset($options['persistent_id'], $options['username'], $options['password'], $options['weight']); |
| 127 | + $options = array_change_key_case($options, CASE_UPPER); |
| 128 | + $client->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); |
| 129 | + $client->setOption(\Memcached::OPT_NO_BLOCK, false); |
| 130 | + if (!array_key_exists('LIBKETAMA_COMPATIBLE', $options) && !array_key_exists(\Memcached::OPT_LIBKETAMA_COMPATIBLE, $options)) { |
| 131 | + $client->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true); |
| 132 | + } |
| 133 | + foreach ($options as $name => $value) { |
| 134 | + if (is_int($name)) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + if ('HASH' === $name || 'SERIALIZER' === $name || 'DISTRIBUTION' === $name) { |
| 138 | + $value = constant('Memcached::'.$name.'_'.strtoupper($value)); |
| 139 | + } |
| 140 | + $opt = constant('Memcached::OPT_'.$name); |
| 141 | + |
| 142 | + unset($options[$name]); |
| 143 | + $options[$opt] = $value; |
| 144 | + } |
| 145 | + $client->setOptions($options); |
| 146 | + |
| 147 | + // set client's servers, taking care of persistent connections |
| 148 | + if (!$client->isPristine()) { |
| 149 | + $oldServers = array(); |
| 150 | + foreach ($client->getServerList() as $server) { |
| 151 | + $oldServers[] = array($server['host'], $server['port']); |
| 152 | + } |
| 153 | + |
| 154 | + $newServers = array(); |
| 155 | + if (1 < count($server)) { |
| 156 | + $server = array_values($server); |
| 157 | + unset($server[2]); |
| 158 | + $server[1] = (int) $server[1]; |
| 159 | + } |
| 160 | + $newServers[] = $server; |
| 161 | + |
| 162 | + if ($oldServers !== $newServers) { |
| 163 | + // before resetting, ensure $servers is valid |
| 164 | + $client->addServers(array($server)); |
| 165 | + $client->resetServerList(); |
| 166 | + } |
| 167 | + } |
| 168 | + $client->addServers(array($server)); |
| 169 | + |
| 170 | + if (null !== $username || null !== $password) { |
| 171 | + if (!method_exists($client, 'setSaslAuthData')) { |
| 172 | + trigger_error('Missing SASL support: the memcached extension must be compiled with --enable-memcached-sasl.'); |
| 173 | + } |
| 174 | + $client->setSaslAuthData($username, $password); |
| 175 | + } |
| 176 | + |
| 177 | + return $client; |
| 178 | + } finally { |
| 179 | + restore_error_handler(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
55 | 183 | /** |
56 | 184 | * {@inheritdoc} |
57 | 185 | */ |
|
0 commit comments