Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes
Zend_Cache_Backend_Memcached Class Reference
Inheritance diagram for Zend_Cache_Backend_Memcached:
Zend_Cache_Backend Zend_Cache_Backend_ExtendedInterface Zend_Cache_Backend_Interface Memcached

Public Member Functions

 __construct (array $options=array())
 
 load ($id, $doNotTestCacheValidity=false)
 
 test ($id)
 
 save ($data, $id, $tags=array(), $specificLifetime=false)
 
 remove ($id)
 
 clean ($mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
 
 isAutomaticCleaningAvailable ()
 
 setDirectives ($directives)
 
 getIds ()
 
 getTags ()
 
 getIdsMatchingTags ($tags=array())
 
 getIdsNotMatchingTags ($tags=array())
 
 getIdsMatchingAnyTags ($tags=array())
 
 getFillingPercentage ()
 
 getMetadatas ($id)
 
 touch ($id, $extraLifetime)
 
 getCapabilities ()
 
- Public Member Functions inherited from Zend_Cache_Backend
 __construct (array $options=array())
 
 setDirectives ($directives)
 
 setOption ($name, $value)
 
 getOption ($name)
 
 getLifetime ($specificLifetime)
 
 isAutomaticCleaningAvailable ()
 
 getTmpDir ()
 

Data Fields

const DEFAULT_HOST = '127.0.0.1'
 
const DEFAULT_PORT = 11211
 
const DEFAULT_PERSISTENT = true
 
const DEFAULT_WEIGHT = 1
 
const DEFAULT_TIMEOUT = 1
 
const DEFAULT_RETRY_INTERVAL = 15
 
const DEFAULT_STATUS = true
 
const DEFAULT_FAILURE_CALLBACK = null
 
const TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::clean() : tags are unsupported by the Memcached backend'
 
const TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::save() : tags are unsupported by the Memcached backend'
 

Protected Attributes

 $_options
 
 $_memcache = null
 
- Protected Attributes inherited from Zend_Cache_Backend
 $_directives
 
 $_options = array()
 

Additional Inherited Members

- Protected Member Functions inherited from Zend_Cache_Backend
 _isGoodTmpDir ($dir)
 
 _loggerSanity ()
 
 _log ($message, $priority=4)
 

Detailed Description

Definition at line 41 of file Memcached.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( array  $options = array())

Constructor

Parameters
array$optionsassociative array of options
Exceptions
Zend_Cache_Exception
Returns
void

Definition at line 120 of file Memcached.php.

121  {
122  if (!extension_loaded('memcache')) {
123  Zend_Cache::throwException('The memcache extension must be loaded for using this backend !');
124  }
125  parent::__construct($options);
126  if (isset($this->_options['servers'])) {
127  $value= $this->_options['servers'];
128  if (isset($value['host'])) {
129  // in this case, $value seems to be a simple associative array (one server only)
130  $value = array(0 => $value); // let's transform it into a classical array of associative arrays
131  }
132  $this->setOption('servers', $value);
133  }
134  $this->_memcache = new Memcache;
135  foreach ($this->_options['servers'] as $server) {
136  if (!array_key_exists('port', $server)) {
137  $server['port'] = self::DEFAULT_PORT;
138  }
139  if (!array_key_exists('persistent', $server)) {
140  $server['persistent'] = self::DEFAULT_PERSISTENT;
141  }
142  if (!array_key_exists('weight', $server)) {
143  $server['weight'] = self::DEFAULT_WEIGHT;
144  }
145  if (!array_key_exists('timeout', $server)) {
146  $server['timeout'] = self::DEFAULT_TIMEOUT;
147  }
148  if (!array_key_exists('retry_interval', $server)) {
149  $server['retry_interval'] = self::DEFAULT_RETRY_INTERVAL;
150  }
151  if (!array_key_exists('status', $server)) {
152  $server['status'] = self::DEFAULT_STATUS;
153  }
154  if (!array_key_exists('failure_callback', $server)) {
155  $server['failure_callback'] = self::DEFAULT_FAILURE_CALLBACK;
156  }
157  if ($this->_options['compatibility']) {
158  // No status for compatibility mode (#ZF-5887)
159  $this->_memcache->addServer($server['host'], $server['port'], $server['persistent'],
160  $server['weight'], $server['timeout'],
161  $server['retry_interval']);
162  } else {
163  $this->_memcache->addServer($server['host'], $server['port'], $server['persistent'],
164  $server['weight'], $server['timeout'],
165  $server['retry_interval'],
166  $server['status'], $server['failure_callback']);
167  }
168  }
169  }
setOption($name, $value)
Definition: Backend.php:101
$value
Definition: gender.phtml:16
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

Member Function Documentation

◆ clean()

clean (   $mode = Zend_Cache::CLEANING_MODE_ALL,
  $tags = array() 
)

Clean some cache records

Available modes are : 'all' (default) => remove all cache entries ($tags is not used) 'old' => unsupported 'matchingTag' => unsupported 'notMatchingTag' => unsupported 'matchingAnyTag' => unsupported

Parameters
string$modeClean mode
array$tagsArray of tags
Exceptions
Zend_Cache_Exception
Returns
boolean True if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 259 of file Memcached.php.

260  {
261  switch ($mode) {
263  return $this->_memcache->flush();
264  break;
266  $this->_log("Zend_Cache_Backend_Memcached::clean() : CLEANING_MODE_OLD is unsupported by the Memcached backend");
267  break;
271  $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND);
272  break;
273  default:
274  Zend_Cache::throwException('Invalid mode for clean() method');
275  break;
276  }
277  }
const CLEANING_MODE_OLD
Definition: Cache.php:73
_log($message, $priority=4)
Definition: Backend.php:273
const CLEANING_MODE_NOT_MATCHING_TAG
Definition: Cache.php:75
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
const CLEANING_MODE_ALL
Definition: Cache.php:72
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74

◆ getCapabilities()

getCapabilities ( )

Return an associative array of capabilities (booleans) of the backend

The array must include these keys :

  • automatic_cleaning (is automating cleaning necessary)
  • tags (are tags supported)
  • expired_read (is it possible to read expired cache records (for doNotTestCacheValidity option for example))
  • priority does the backend deal with priority when saving
  • infinite_lifetime (is infinite lifetime can work with this backend)
  • get_list (is it possible to get the list of cache ids and the complete list of tags)
Returns
array associative of with capabilities

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 497 of file Memcached.php.

498  {
499  return array(
500  'automatic_cleaning' => false,
501  'tags' => false,
502  'expired_read' => false,
503  'priority' => false,
504  'infinite_lifetime' => false,
505  'get_list' => false
506  );
507  }

◆ getFillingPercentage()

getFillingPercentage ( )

Return the filling percentage of the backend storage

Exceptions
Zend_Cache_Exception
Returns
int integer between 0 and 100

Couchbase 1.x uses 'mem_used' instead of 'bytes'

See also
https://www.couchbase.com/issues/browse/MB-3466

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 380 of file Memcached.php.

381  {
382  $mems = $this->_memcache->getExtendedStats();
383 
384  $memSize = null;
385  $memUsed = null;
386  foreach ($mems as $key => $mem) {
387  if ($mem === false) {
388  $this->_log('can\'t get stat from ' . $key);
389  continue;
390  }
391 
392  $eachSize = $mem['limit_maxbytes'];
393 
398  $eachUsed = isset($mem['bytes']) ? $mem['bytes'] : $mem['mem_used'];
399  if ($eachUsed > $eachSize) {
400  $eachUsed = $eachSize;
401  }
402 
403  $memSize += $eachSize;
404  $memUsed += $eachUsed;
405  }
406 
407  if ($memSize === null || $memUsed === null) {
408  Zend_Cache::throwException('Can\'t get filling percentage');
409  }
410 
411  return ((int) (100. * ($memUsed / $memSize)));
412  }
_log($message, $priority=4)
Definition: Backend.php:273
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

◆ getIds()

getIds ( )

Return an array of stored cache ids

Returns
array array of stored cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 315 of file Memcached.php.

316  {
317  $this->_log("Zend_Cache_Backend_Memcached::save() : getting the list of cache ids is unsupported by the Memcache backend");
318  return array();
319  }
_log($message, $priority=4)
Definition: Backend.php:273

◆ getIdsMatchingAnyTags()

getIdsMatchingAnyTags (   $tags = array())

Return an array of stored cache ids which match any given tags

In case of multiple tags, a logical AND is made between tags

Parameters
array$tagsarray of tags
Returns
array array of any matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 368 of file Memcached.php.

369  {
370  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
371  return array();
372  }
_log($message, $priority=4)
Definition: Backend.php:273

◆ getIdsMatchingTags()

getIdsMatchingTags (   $tags = array())

Return an array of stored cache ids which match given tags

In case of multiple tags, a logical AND is made between tags

Parameters
array$tagsarray of tags
Returns
array array of matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 340 of file Memcached.php.

341  {
342  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
343  return array();
344  }
_log($message, $priority=4)
Definition: Backend.php:273

◆ getIdsNotMatchingTags()

getIdsNotMatchingTags (   $tags = array())

Return an array of stored cache ids which don't match given tags

In case of multiple tags, a logical OR is made between tags

Parameters
array$tagsarray of tags
Returns
array array of not matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 354 of file Memcached.php.

355  {
356  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
357  return array();
358  }
_log($message, $priority=4)
Definition: Backend.php:273

◆ getMetadatas()

getMetadatas (   $id)

Return an array of metadatas for the given cache id

The array must include these keys :

  • expire : the expire timestamp
  • tags : a string array of tags
  • mtime : timestamp of last modification time
Parameters
string$idcache id
Returns
array array of metadatas (false if the cache id is not found)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 425 of file Memcached.php.

426  {
427  $tmp = $this->_memcache->get($id);
428  if (is_array($tmp)) {
429  $data = $tmp[0];
430  $mtime = $tmp[1];
431  if (!isset($tmp[2])) {
432  // because this record is only with 1.7 release
433  // if old cache records are still there...
434  return false;
435  }
436  $lifetime = $tmp[2];
437  return array(
438  'expire' => $mtime + $lifetime,
439  'tags' => array(),
440  'mtime' => $mtime
441  );
442  }
443  return false;
444  }
$id
Definition: fieldset.phtml:14

◆ getTags()

getTags ( )

Return an array of stored tags

Returns
array array of stored tags (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 326 of file Memcached.php.

327  {
328  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
329  return array();
330  }
_log($message, $priority=4)
Definition: Backend.php:273

◆ isAutomaticCleaningAvailable()

isAutomaticCleaningAvailable ( )

Return true if the automatic cleaning is available for the backend

Returns
boolean

Definition at line 284 of file Memcached.php.

285  {
286  return false;
287  }

◆ load()

load (   $id,
  $doNotTestCacheValidity = false 
)

Test if a cache is available for the given id and (if yes) return it (false else)

Parameters
string$idCache id
boolean$doNotTestCacheValidityIf set to true, the cache validity won't be tested
Returns
string|false cached datas

Implements Zend_Cache_Backend_Interface.

Definition at line 178 of file Memcached.php.

179  {
180  $tmp = $this->_memcache->get($id);
181  if (is_array($tmp) && isset($tmp[0])) {
182  return $tmp[0];
183  }
184  return false;
185  }
$id
Definition: fieldset.phtml:14

◆ remove()

remove (   $id)

Remove a cache record

Parameters
string$idCache id
Returns
boolean True if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 239 of file Memcached.php.

240  {
241  return $this->_memcache->delete($id, 0);
242  }
$id
Definition: fieldset.phtml:14

◆ save()

save (   $data,
  $id,
  $tags = array(),
  $specificLifetime = false 
)

Save some string datas into a cache record

Note : $data is always "string" (serialization is done by the core not by the backend)

Parameters
string$dataDatas to cache
string$idCache id
array$tagsArray of strings, the cache record will be tagged by each string entry
int$specificLifetimeIf != false, set a specific lifetime for this cache record (null => infinite lifetime)
Returns
boolean True if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 214 of file Memcached.php.

215  {
216  $lifetime = $this->getLifetime($specificLifetime);
217  if ($this->_options['compression']) {
218  $flag = MEMCACHE_COMPRESSED;
219  } else {
220  $flag = 0;
221  }
222 
223  // ZF-8856: using set because add needs a second request if item already exists
224  $result = @$this->_memcache->set($id, array($data, time(), $lifetime), $flag, $lifetime);
225 
226  if (count($tags) > 0) {
227  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
228  }
229 
230  return $result;
231  }
$id
Definition: fieldset.phtml:14
_log($message, $priority=4)
Definition: Backend.php:273
getLifetime($specificLifetime)
Definition: Backend.php:143

◆ setDirectives()

setDirectives (   $directives)

Set the frontend directives

Parameters
array$directivesAssoc of directives
Exceptions
Zend_Cache_Exception
Returns
void

Implements Zend_Cache_Backend_Interface.

Definition at line 296 of file Memcached.php.

297  {
298  parent::setDirectives($directives);
299  $lifetime = $this->getLifetime(false);
300  if ($lifetime > 2592000) {
301  // #ZF-3490 : For the memcached backend, there is a lifetime limit of 30 days (2592000 seconds)
302  $this->_log('memcached backend has a limit of 30 days (2592000 seconds) for the lifetime');
303  }
304  if ($lifetime === null) {
305  // #ZF-4614 : we tranform null to zero to get the maximal lifetime
306  parent::setDirectives(array('lifetime' => 0));
307  }
308  }
_log($message, $priority=4)
Definition: Backend.php:273
getLifetime($specificLifetime)
Definition: Backend.php:143

◆ test()

test (   $id)

Test if a cache is available or not (for the given id)

Parameters
string$idCache id
Returns
mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Implements Zend_Cache_Backend_Interface.

Definition at line 193 of file Memcached.php.

194  {
195  $tmp = $this->_memcache->get($id);
196  if (is_array($tmp)) {
197  return $tmp[1];
198  }
199  return false;
200  }
$id
Definition: fieldset.phtml:14

◆ touch()

touch (   $id,
  $extraLifetime 
)

Give (if possible) an extra lifetime to the given cache id

Parameters
string$idcache id
int$extraLifetime
Returns
boolean true if ok

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 453 of file Memcached.php.

454  {
455  if ($this->_options['compression']) {
456  $flag = MEMCACHE_COMPRESSED;
457  } else {
458  $flag = 0;
459  }
460  $tmp = $this->_memcache->get($id);
461  if (is_array($tmp)) {
462  $data = $tmp[0];
463  $mtime = $tmp[1];
464  if (!isset($tmp[2])) {
465  // because this record is only with 1.7 release
466  // if old cache records are still there...
467  return false;
468  }
469  $lifetime = $tmp[2];
470  $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
471  if ($newLifetime <=0) {
472  return false;
473  }
474  // #ZF-5702 : we try replace() first becase set() seems to be slower
475  if (!($result = $this->_memcache->replace($id, array($data, time(), $newLifetime), $flag, $newLifetime))) {
476  $result = $this->_memcache->set($id, array($data, time(), $newLifetime), $flag, $newLifetime);
477  }
478  return $result;
479  }
480  return false;
481  }
$id
Definition: fieldset.phtml:14

Field Documentation

◆ $_memcache

$_memcache = null
protected

Definition at line 111 of file Memcached.php.

◆ $_options

$_options
protected
Initial value:
= array(
'servers' => array(array(
'host' => self::DEFAULT_HOST,
'port' => self::DEFAULT_PORT,
'persistent' => self::DEFAULT_PERSISTENT,
'weight' => self::DEFAULT_WEIGHT,
'timeout' => self::DEFAULT_TIMEOUT,
'retry_interval' => self::DEFAULT_RETRY_INTERVAL,
'status' => self::DEFAULT_STATUS,
'failure_callback' => self::DEFAULT_FAILURE_CALLBACK
)),
'compression' => false,
'compatibility' => false,
)

Definition at line 91 of file Memcached.php.

◆ DEFAULT_FAILURE_CALLBACK

const DEFAULT_FAILURE_CALLBACK = null

Definition at line 53 of file Memcached.php.

◆ DEFAULT_HOST

const DEFAULT_HOST = '127.0.0.1'

Default Values

Definition at line 46 of file Memcached.php.

◆ DEFAULT_PERSISTENT

const DEFAULT_PERSISTENT = true

Definition at line 48 of file Memcached.php.

◆ DEFAULT_PORT

const DEFAULT_PORT = 11211

Definition at line 47 of file Memcached.php.

◆ DEFAULT_RETRY_INTERVAL

const DEFAULT_RETRY_INTERVAL = 15

Definition at line 51 of file Memcached.php.

◆ DEFAULT_STATUS

const DEFAULT_STATUS = true

Definition at line 52 of file Memcached.php.

◆ DEFAULT_TIMEOUT

const DEFAULT_TIMEOUT = 1

Definition at line 50 of file Memcached.php.

◆ DEFAULT_WEIGHT

const DEFAULT_WEIGHT = 1

Definition at line 49 of file Memcached.php.

◆ TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND

const TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::clean() : tags are unsupported by the Memcached backend'

Log message

Definition at line 58 of file Memcached.php.

◆ TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND

const TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::save() : tags are unsupported by the Memcached backend'

Definition at line 59 of file Memcached.php.


The documentation for this class was generated from the following file: