1 | <?php |
||
65 | class MemCached extends SimpleCache |
||
66 | { |
||
67 | /** |
||
68 | * @var string an ID that identifies a Memcached instance. |
||
69 | * By default the Memcached instances are destroyed at the end of the request. To create an instance that |
||
70 | * persists between requests, you may specify a unique ID for the instance. All instances created with the |
||
71 | * same ID will share the same connection. |
||
72 | * @see http://ca2.php.net/manual/en/memcached.construct.php |
||
73 | */ |
||
74 | public $persistentId; |
||
75 | /** |
||
76 | * @var array options for Memcached. |
||
77 | * @see http://ca2.php.net/manual/en/memcached.setoptions.php |
||
78 | */ |
||
79 | public $options; |
||
80 | /** |
||
81 | * @var string memcached sasl username. |
||
82 | * @see http://php.net/manual/en/memcached.setsaslauthdata.php |
||
83 | */ |
||
84 | public $username; |
||
85 | /** |
||
86 | * @var string memcached sasl password. |
||
87 | * @see http://php.net/manual/en/memcached.setsaslauthdata.php |
||
88 | */ |
||
89 | public $password; |
||
90 | |||
91 | /** |
||
92 | * @var \Memcached the Memcached instance |
||
93 | */ |
||
94 | private $_cache; |
||
95 | /** |
||
96 | * @var array list of memcached server configurations |
||
97 | */ |
||
98 | private $_servers = []; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Initializes this application component. |
||
103 | * It creates the memcached instance and adds memcached servers. |
||
104 | */ |
||
105 | 16 | public function init() |
|
106 | { |
||
107 | 16 | parent::init(); |
|
108 | 16 | $this->addServers($this->getMemcached(), $this->getServers()); |
|
109 | 16 | } |
|
110 | |||
111 | /** |
||
112 | * Add servers to the server pool of the cache specified |
||
113 | * |
||
114 | * @param \Memcached $cache |
||
115 | * @param MemCachedServer[] $servers |
||
116 | * @throws InvalidConfigException |
||
117 | */ |
||
118 | 16 | protected function addServers($cache, $servers) |
|
119 | { |
||
120 | 16 | if (empty($servers)) { |
|
121 | 16 | $servers = [new MemCachedServer([ |
|
122 | 16 | 'host' => '127.0.0.1', |
|
123 | 'port' => 11211, |
||
124 | ])]; |
||
125 | } else { |
||
126 | foreach ($servers as $server) { |
||
127 | if ($server->host === null) { |
||
128 | throw new InvalidConfigException("The 'host' property must be specified for every memcached server."); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | 16 | $existingServers = []; |
|
134 | 16 | if ($this->persistentId !== null) { |
|
135 | foreach ($cache->getServerList() as $s) { |
||
136 | $existingServers[$s['host'] . ':' . $s['port']] = true; |
||
137 | } |
||
138 | } |
||
139 | 16 | foreach ($servers as $server) { |
|
140 | 16 | if (empty($existingServers) || !isset($existingServers[$server->host . ':' . $server->port])) { |
|
141 | 16 | $cache->addServer($server->host, $server->port, $server->weight); |
|
142 | } |
||
143 | } |
||
144 | 16 | } |
|
145 | |||
146 | /** |
||
147 | * Returns the underlying memcached object. |
||
148 | * @return \Memcached the memcached object used by this cache component. |
||
149 | * @throws InvalidConfigException if memcached extension is not loaded |
||
150 | */ |
||
151 | 16 | public function getMemcached() |
|
152 | { |
||
153 | 16 | if ($this->_cache === null) { |
|
154 | 16 | if (!extension_loaded('memcached')) { |
|
155 | throw new InvalidConfigException('MemCached requires PHP memcached extension to be loaded.'); |
||
156 | } |
||
157 | |||
158 | 16 | $this->_cache = $this->persistentId !== null ? new \Memcached($this->persistentId) : new \Memcached; |
|
159 | 16 | if ($this->username !== null || $this->password !== null) { |
|
160 | $this->_cache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); |
||
161 | $this->_cache->setSaslAuthData($this->username, $this->password); |
||
162 | } |
||
163 | 16 | if (!empty($this->options)) { |
|
164 | $this->_cache->setOptions($this->options); |
||
|
|||
165 | } |
||
166 | } |
||
167 | |||
168 | 16 | return $this->_cache; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Returns the memcached server configurations. |
||
173 | * @return MemCachedServer[] list of memcached server configurations. |
||
174 | */ |
||
175 | 16 | public function getServers() |
|
176 | { |
||
177 | 16 | return $this->_servers; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param array $config list of memcached server configurations. Each element must be an array |
||
182 | * with the following keys: host, port, persistent, weight, timeout, retryInterval, status. |
||
183 | * @see http://php.net/manual/en/memcached.addserver.php |
||
184 | */ |
||
185 | public function setServers($config) |
||
186 | { |
||
187 | foreach ($config as $c) { |
||
188 | $this->_servers[] = new MemCachedServer($c); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | 13 | protected function getValue($key) |
|
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | 3 | protected function getValues($keys) |
|
204 | { |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | 12 | protected function setValue($key, $value, $ttl) |
|
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | 4 | protected function setValues($values, $ttl) |
|
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | 1 | protected function deleteValue($key) |
|
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | 11 | public function clear() |
|
249 | } |
||
250 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.