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

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_WEIGHT = 1
 
const TAGS_UNSUPPORTED_BY_CLEAN_OF_LIBMEMCACHED_BACKEND = 'Zend_Cache_Backend_Libmemcached::clean() : tags are unsupported by the Libmemcached backend'
 
const TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND = 'Zend_Cache_Backend_Libmemcached::save() : tags are unsupported by the Libmemcached 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 Libmemcached.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 97 of file Libmemcached.php.

98  {
99  if (!extension_loaded('memcached')) {
100  Zend_Cache::throwException('The memcached extension must be loaded for using this backend !');
101  }
102 
103  // override default client options
104  $this->_options['client'] = array(
105  Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
106  Memcached::OPT_HASH => Memcached::HASH_MD5,
107  Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
108  );
109 
110  parent::__construct($options);
111 
112  if (isset($this->_options['servers'])) {
113  $value = $this->_options['servers'];
114  if (isset($value['host'])) {
115  // in this case, $value seems to be a simple associative array (one server only)
116  $value = array(0 => $value); // let's transform it into a classical array of associative arrays
117  }
118  $this->setOption('servers', $value);
119  }
120  $this->_memcache = new Memcached;
121 
122  // setup memcached client options
123  foreach ($this->_options['client'] as $name => $value) {
124  $optId = null;
125  if (is_int($name)) {
126  $optId = $name;
127  } else {
128  $optConst = 'Memcached::OPT_' . strtoupper($name);
129  if (defined($optConst)) {
130  $optId = constant($optConst);
131  } else {
132  $this->_log("Unknown memcached client option '{$name}' ({$optConst})");
133  }
134  }
135  if (null !== $optId) {
136  if (!$this->_memcache->setOption($optId, $value)) {
137  $this->_log("Setting memcached client option '{$optId}' failed");
138  }
139  }
140  }
141 
142  // setup memcached servers
143  $servers = array();
144  foreach ($this->_options['servers'] as $server) {
145  if (!array_key_exists('port', $server)) {
146  $server['port'] = self::DEFAULT_PORT;
147  }
148  if (!array_key_exists('weight', $server)) {
149  $server['weight'] = self::DEFAULT_WEIGHT;
150  }
151 
152  $servers[] = array($server['host'], $server['port'], $server['weight']);
153  }
154  $this->_memcache->addServers($servers);
155  }
_log($message, $priority=4)
Definition: Backend.php:273
setOption($name, $value)
Definition: Backend.php:101
$value
Definition: gender.phtml:16
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
if(!isset($_GET['name'])) $name
Definition: log.php:14

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 245 of file Libmemcached.php.

246  {
247  switch ($mode) {
249  return $this->_memcache->flush();
250  break;
252  $this->_log("Zend_Cache_Backend_Libmemcached::clean() : CLEANING_MODE_OLD is unsupported by the Libmemcached backend");
253  break;
257  $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_LIBMEMCACHED_BACKEND);
258  break;
259  default:
260  Zend_Cache::throwException('Invalid mode for clean() method');
261  break;
262  }
263  }
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 472 of file Libmemcached.php.

473  {
474  return array(
475  'automatic_cleaning' => false,
476  'tags' => false,
477  'expired_read' => false,
478  'priority' => false,
479  'infinite_lifetime' => false,
480  'get_list' => false
481  );
482  }

◆ getFillingPercentage()

getFillingPercentage ( )

Return the filling percentage of the backend storage

Exceptions
Zend_Cache_Exception
Returns
int integer between 0 and 100

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 366 of file Libmemcached.php.

367  {
368  $mems = $this->_memcache->getStats();
369  if ($mems === false) {
370  return 0;
371  }
372 
373  $memSize = null;
374  $memUsed = null;
375  foreach ($mems as $key => $mem) {
376  if ($mem === false) {
377  $this->_log('can\'t get stat from ' . $key);
378  continue;
379  }
380 
381  $eachSize = $mem['limit_maxbytes'];
382  $eachUsed = $mem['bytes'];
383  if ($eachUsed > $eachSize) {
384  $eachUsed = $eachSize;
385  }
386 
387  $memSize += $eachSize;
388  $memUsed += $eachUsed;
389  }
390 
391  if ($memSize === null || $memUsed === null) {
392  Zend_Cache::throwException('Can\'t get filling percentage');
393  }
394 
395  return ((int) (100. * ($memUsed / $memSize)));
396  }
_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 301 of file Libmemcached.php.

302  {
303  $this->_log("Zend_Cache_Backend_Libmemcached::save() : getting the list of cache ids is unsupported by the Libmemcached backend");
304  return array();
305  }
_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 354 of file Libmemcached.php.

355  {
356  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND);
357  return array();
358  }
_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 326 of file Libmemcached.php.

327  {
328  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND);
329  return array();
330  }
_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 340 of file Libmemcached.php.

341  {
342  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND);
343  return array();
344  }
_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 409 of file Libmemcached.php.

410  {
411  $tmp = $this->_memcache->get($id);
412  if (isset($tmp[0], $tmp[1], $tmp[2])) {
413  $data = $tmp[0];
414  $mtime = $tmp[1];
415  $lifetime = $tmp[2];
416  return array(
417  'expire' => $mtime + $lifetime,
418  'tags' => array(),
419  'mtime' => $mtime
420  );
421  }
422 
423  return false;
424  }
$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 312 of file Libmemcached.php.

313  {
314  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND);
315  return array();
316  }
_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 270 of file Libmemcached.php.

271  {
272  return false;
273  }

◆ 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 164 of file Libmemcached.php.

165  {
166  $tmp = $this->_memcache->get($id);
167  if (isset($tmp[0])) {
168  return $tmp[0];
169  }
170  return false;
171  }
$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 225 of file Libmemcached.php.

226  {
227  return $this->_memcache->delete($id);
228  }
$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 200 of file Libmemcached.php.

201  {
202  $lifetime = $this->getLifetime($specificLifetime);
203 
204  // ZF-8856: using set because add needs a second request if item already exists
205  $result = @$this->_memcache->set($id, array($data, time(), $lifetime), $lifetime);
206  if ($result === false) {
207  $rsCode = $this->_memcache->getResultCode();
208  $rsMsg = $this->_memcache->getResultMessage();
209  $this->_log("Memcached::set() failed: [{$rsCode}] {$rsMsg}");
210  }
211 
212  if (count($tags) > 0) {
213  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND);
214  }
215 
216  return $result;
217  }
$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 282 of file Libmemcached.php.

283  {
284  parent::setDirectives($directives);
285  $lifetime = $this->getLifetime(false);
286  if ($lifetime > 2592000) {
287  // #ZF-3490 : For the memcached backend, there is a lifetime limit of 30 days (2592000 seconds)
288  $this->_log('memcached backend has a limit of 30 days (2592000 seconds) for the lifetime');
289  }
290  if ($lifetime === null) {
291  // #ZF-4614 : we tranform null to zero to get the maximal lifetime
292  parent::setDirectives(array('lifetime' => 0));
293  }
294  }
_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
int|false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Implements Zend_Cache_Backend_Interface.

Definition at line 179 of file Libmemcached.php.

180  {
181  $tmp = $this->_memcache->get($id);
182  if (isset($tmp[0], $tmp[1])) {
183  return (int)$tmp[1];
184  }
185  return false;
186  }
$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 433 of file Libmemcached.php.

434  {
435  $tmp = $this->_memcache->get($id);
436  if (isset($tmp[0], $tmp[1], $tmp[2])) {
437  $data = $tmp[0];
438  $mtime = $tmp[1];
439  $lifetime = $tmp[2];
440  $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
441  if ($newLifetime <=0) {
442  return false;
443  }
444  // #ZF-5702 : we try replace() first becase set() seems to be slower
445  if (!($result = $this->_memcache->replace($id, array($data, time(), $newLifetime), $newLifetime))) {
446  $result = $this->_memcache->set($id, array($data, time(), $newLifetime), $newLifetime);
447  if ($result === false) {
448  $rsCode = $this->_memcache->getResultCode();
449  $rsMsg = $this->_memcache->getResultMessage();
450  $this->_log("Memcached::set() failed: [{$rsCode}] {$rsMsg}");
451  }
452  }
453  return $result;
454  }
455  return false;
456  }
$id
Definition: fieldset.phtml:14
_log($message, $priority=4)
Definition: Backend.php:273

Field Documentation

◆ $_memcache

$_memcache = null
protected

Definition at line 88 of file Libmemcached.php.

◆ $_options

$_options
protected
Initial value:
= array(
'servers' => array(array(
'host' => self::DEFAULT_HOST,
'port' => self::DEFAULT_PORT,
'weight' => self::DEFAULT_WEIGHT,
)),
'client' => array()
)

Definition at line 74 of file Libmemcached.php.

◆ DEFAULT_HOST

const DEFAULT_HOST = '127.0.0.1'

Default Server Values

Definition at line 46 of file Libmemcached.php.

◆ DEFAULT_PORT

const DEFAULT_PORT = 11211

Definition at line 47 of file Libmemcached.php.

◆ DEFAULT_WEIGHT

const DEFAULT_WEIGHT = 1

Definition at line 48 of file Libmemcached.php.

◆ TAGS_UNSUPPORTED_BY_CLEAN_OF_LIBMEMCACHED_BACKEND

const TAGS_UNSUPPORTED_BY_CLEAN_OF_LIBMEMCACHED_BACKEND = 'Zend_Cache_Backend_Libmemcached::clean() : tags are unsupported by the Libmemcached backend'

Log message

Definition at line 53 of file Libmemcached.php.

◆ TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND

const TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND = 'Zend_Cache_Backend_Libmemcached::save() : tags are unsupported by the Libmemcached backend'

Definition at line 54 of file Libmemcached.php.


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