3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Cache\Storage\Adapter
;
12 use Memcached
as MemcachedResource
;
13 use Zend\Cache\Exception
;
16 * These are options specific to the Memcached adapter
18 class MemcachedOptions
extends AdapterOptions
20 // @codingStandardsIgnoreStart
22 * Prioritized properties ordered by prio to be set first
23 * in case a bulk of options sets set at once
27 protected $__prioritizedProperties__ = ['resource_manager', 'resource_id'];
28 // @codingStandardsIgnoreEnd
31 * The namespace separator
34 protected $namespaceSeparator = ':';
37 * The memcached resource manager
39 * @var null|MemcachedResourceManager
41 protected $resourceManager;
44 * The resource id of the resource manager
48 protected $resourceId = 'default';
53 * The option Memcached::OPT_PREFIX_KEY will be used as the namespace.
54 * It can't be longer than 128 characters.
56 * @see AdapterOptions::setNamespace()
57 * @see MemcachedOptions::setPrefixKey()
59 public function setNamespace($namespace)
61 $namespace = (string) $namespace;
63 if (128 < strlen($namespace)) {
64 throw new Exception\
InvalidArgumentException(sprintf(
65 '%s expects a prefix key of no longer than 128 characters',
70 return parent
::setNamespace($namespace);
74 * Set namespace separator
76 * @param string $namespaceSeparator
77 * @return MemcachedOptions
79 public function setNamespaceSeparator($namespaceSeparator)
81 $namespaceSeparator = (string) $namespaceSeparator;
82 if ($this->namespaceSeparator
!== $namespaceSeparator) {
83 $this->triggerOptionEvent('namespace_separator', $namespaceSeparator);
84 $this->namespaceSeparator
= $namespaceSeparator;
90 * Get namespace separator
94 public function getNamespaceSeparator()
96 return $this->namespaceSeparator
;
100 * A memcached resource to share
102 * @param null|MemcachedResource $memcachedResource
103 * @return MemcachedOptions
104 * @deprecated Please use the resource manager instead
106 public function setMemcachedResource(MemcachedResource
$memcachedResource = null)
109 'This method is deprecated and will be removed in the feature'
110 . ', please use the resource manager instead',
114 if ($memcachedResource !== null) {
115 $this->triggerOptionEvent('memcached_resource', $memcachedResource);
116 $resourceManager = $this->getResourceManager();
117 $resourceId = $this->getResourceId();
118 $resourceManager->setResource($resourceId, $memcachedResource);
124 * Get memcached resource to share
126 * @return MemcachedResource
127 * @deprecated Please use the resource manager instead
129 public function getMemcachedResource()
132 'This method is deprecated and will be removed in the feature'
133 . ', please use the resource manager instead',
137 return $this->resourceManager
->getResource($this->getResourceId());
141 * Set the memcached resource manager to use
143 * @param null|MemcachedResourceManager $resourceManager
144 * @return MemcachedOptions
146 public function setResourceManager(MemcachedResourceManager
$resourceManager = null)
148 if ($this->resourceManager
!== $resourceManager) {
149 $this->triggerOptionEvent('resource_manager', $resourceManager);
150 $this->resourceManager
= $resourceManager;
156 * Get the memcached resource manager
158 * @return MemcachedResourceManager
160 public function getResourceManager()
162 if (! $this->resourceManager
) {
163 $this->resourceManager
= new MemcachedResourceManager();
165 return $this->resourceManager
;
169 * Get the memcached resource id
173 public function getResourceId()
175 return $this->resourceId
;
179 * Set the memcached resource id
181 * @param string $resourceId
182 * @return MemcachedOptions
184 public function setResourceId($resourceId)
186 $resourceId = (string) $resourceId;
187 if ($this->resourceId
!== $resourceId) {
188 $this->triggerOptionEvent('resource_id', $resourceId);
189 $this->resourceId
= $resourceId;
195 * Get the persistent id
199 public function getPersistentId()
201 return $this->getResourceManager()->getPersistentId($this->getResourceId());
205 * Set the persistent id
207 * @param string $persistentId
208 * @return MemcachedOptions
210 public function setPersistentId($persistentId)
212 $this->triggerOptionEvent('persistent_id', $persistentId);
213 $this->getResourceManager()->setPersistentId($this->getResourceId(), $persistentId);
218 * Add a server to the list
220 * @param string $host
223 * @return MemcachedOptions
224 * @deprecated Please use the resource manager instead
226 public function addServer($host, $port = 11211, $weight = 0)
229 'This method is deprecated and will be removed in the feature'
230 . ', please use the resource manager instead',
234 $this->getResourceManager()->addServer($this->getResourceId(), [
244 * Set a list of memcached servers to add on initialize
246 * @param string|array $servers list of servers
247 * @return MemcachedOptions
248 * @throws Exception\InvalidArgumentException
250 public function setServers($servers)
252 $this->getResourceManager()->setServers($this->getResourceId(), $servers);
261 public function getServers()
263 return $this->getResourceManager()->getServers($this->getResourceId());
267 * Set libmemcached options
269 * @param array $libOptions
270 * @return MemcachedOptions
271 * @link http://php.net/manual/memcached.constants.php
273 public function setLibOptions(array $libOptions)
275 $this->getResourceManager()->setLibOptions($this->getResourceId(), $libOptions);
280 * Set libmemcached option
282 * @param string|int $key
283 * @param mixed $value
284 * @return MemcachedOptions
285 * @link http://php.net/manual/memcached.constants.php
286 * @deprecated Please use lib_options or the resource manager instead
288 public function setLibOption($key, $value)
291 'This method is deprecated and will be removed in the feature'
292 . ', please use "lib_options" or the resource manager instead',
296 $this->getResourceManager()->setLibOption($this->getResourceId(), $key, $value);
301 * Get libmemcached options
304 * @link http://php.net/manual/memcached.constants.php
306 public function getLibOptions()
308 return $this->getResourceManager()->getLibOptions($this->getResourceId());
312 * Get libmemcached option
314 * @param string|int $key
316 * @link http://php.net/manual/memcached.constants.php
317 * @deprecated Please use lib_options or the resource manager instead
319 public function getLibOption($key)
322 'This method is deprecated and will be removed in the feature'
323 . ', please use "lib_options" or the resource manager instead',
327 return $this->getResourceManager()->getLibOption($this->getResourceId(), $key);