Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Memcached.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Cache/Backend/ExtendedInterface.php';
28 
32 #require_once 'Zend/Cache/Backend.php';
33 
34 
42 {
46  const DEFAULT_HOST = '127.0.0.1';
47  const DEFAULT_PORT = 11211;
48  const DEFAULT_PERSISTENT = true;
49  const DEFAULT_WEIGHT = 1;
50  const DEFAULT_TIMEOUT = 1;
52  const DEFAULT_STATUS = true;
54 
58  const TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::clean() : tags are unsupported by the Memcached backend';
59  const TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::save() : tags are unsupported by the Memcached backend';
60 
91  protected $_options = array(
92  'servers' => array(array(
93  'host' => self::DEFAULT_HOST,
94  'port' => self::DEFAULT_PORT,
95  'persistent' => self::DEFAULT_PERSISTENT,
96  'weight' => self::DEFAULT_WEIGHT,
97  'timeout' => self::DEFAULT_TIMEOUT,
98  'retry_interval' => self::DEFAULT_RETRY_INTERVAL,
99  'status' => self::DEFAULT_STATUS,
100  'failure_callback' => self::DEFAULT_FAILURE_CALLBACK
101  )),
102  'compression' => false,
103  'compatibility' => false,
104  );
105 
111  protected $_memcache = null;
112 
120  public function __construct(array $options = array())
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  }
170 
178  public function load($id, $doNotTestCacheValidity = false)
179  {
180  $tmp = $this->_memcache->get($id);
181  if (is_array($tmp) && isset($tmp[0])) {
182  return $tmp[0];
183  }
184  return false;
185  }
186 
193  public function test($id)
194  {
195  $tmp = $this->_memcache->get($id);
196  if (is_array($tmp)) {
197  return $tmp[1];
198  }
199  return false;
200  }
201 
214  public function save($data, $id, $tags = array(), $specificLifetime = false)
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  }
232 
239  public function remove($id)
240  {
241  return $this->_memcache->delete($id, 0);
242  }
243 
259  public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
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  }
278 
285  {
286  return false;
287  }
288 
296  public function setDirectives($directives)
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  }
309 
315  public function getIds()
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  }
320 
326  public function getTags()
327  {
328  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
329  return array();
330  }
331 
340  public function getIdsMatchingTags($tags = array())
341  {
342  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
343  return array();
344  }
345 
354  public function getIdsNotMatchingTags($tags = array())
355  {
356  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
357  return array();
358  }
359 
368  public function getIdsMatchingAnyTags($tags = array())
369  {
370  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
371  return array();
372  }
373 
380  public function getFillingPercentage()
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  }
413 
425  public function getMetadatas($id)
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  }
445 
453  public function touch($id, $extraLifetime)
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  }
482 
497  public function getCapabilities()
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  }
508 
509 }
const TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND
Definition: Memcached.php:58
$id
Definition: fieldset.phtml:14
const TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND
Definition: Memcached.php:59
touch($id, $extraLifetime)
Definition: Memcached.php:453
getIdsMatchingAnyTags($tags=array())
Definition: Memcached.php:368
const CLEANING_MODE_OLD
Definition: Cache.php:73
_log($message, $priority=4)
Definition: Backend.php:273
setOption($name, $value)
Definition: Backend.php:101
save($data, $id, $tags=array(), $specificLifetime=false)
Definition: Memcached.php:214
$value
Definition: gender.phtml:16
const CLEANING_MODE_NOT_MATCHING_TAG
Definition: Cache.php:75
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
__construct(array $options=array())
Definition: Memcached.php:120
clean($mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
Definition: Memcached.php:259
const CLEANING_MODE_ALL
Definition: Cache.php:72
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
getIdsNotMatchingTags($tags=array())
Definition: Memcached.php:354
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
load($id, $doNotTestCacheValidity=false)
Definition: Memcached.php:178
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74
getLifetime($specificLifetime)
Definition: Backend.php:143
getIdsMatchingTags($tags=array())
Definition: Memcached.php:340