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

Public Member Functions

 __construct (array $options=[])
 
 save ($data, $id, $tags=[], $specificLifetime=false)
 
 load ($id, $doNotTestCacheValidity=false)
 
- Public Member Functions inherited from Zend_Cache_Backend_Memcached
 __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_SLAB_SIZE = 1048576
 
const CODE_WORD = '{splitted}'
 
- Data Fields inherited from Zend_Cache_Backend_Memcached
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 Member Functions

 _getChunkId ($id, $index)
 
 _cleanTheMess ($id, $chunks)
 
- Protected Member Functions inherited from Zend_Cache_Backend
 _isGoodTmpDir ($dir)
 
 _loggerSanity ()
 
 _log ($message, $priority=4)
 

Additional Inherited Members

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

Detailed Description

Definition at line 8 of file Memcached.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( array  $options = [])

Constructor

Parameters
array$options
See also
\Zend_Cache_Backend_Memcached::__construct()
Exceptions

Definition at line 26 of file Memcached.php.

27  {
28  parent::__construct($options);
29 
30  if (!isset($options['slab_size']) || !is_numeric($options['slab_size'])) {
31  if (isset($options['slab_size'])) {
32  throw new \Magento\Framework\Exception\LocalizedException(
33  new \Magento\Framework\Phrase(
34  "Invalid value for the node <slab_size>. Expected to be positive integer."
35  )
36  );
37  }
38 
39  $this->_options['slab_size'] = self::DEFAULT_SLAB_SIZE;
40  } else {
41  $this->_options['slab_size'] = $options['slab_size'];
42  }
43  }

Member Function Documentation

◆ _cleanTheMess()

_cleanTheMess (   $id,
  $chunks 
)
protected

Remove saved chunks in case something gone wrong (e.g. some chunk from the chain can not be found)

Parameters
string$idID of data's info cell
int$chunksNumber of chunks to remove (basically, the number after '{splitted}|')
Returns
null

Definition at line 64 of file Memcached.php.

65  {
66  for ($i = 0; $i < $chunks; $i++) {
67  $this->remove($this->_getChunkId($id, $i));
68  }
69 
70  $this->remove($id);
71  }
$id
Definition: fieldset.phtml:14
$i
Definition: gallery.phtml:31

◆ _getChunkId()

_getChunkId (   $id,
  $index 
)
protected

Returns ID of a specific chunk on the basis of data's ID

Parameters
string$idMain data's ID
int$indexParticular chunk number to return ID for
Returns
string

Definition at line 52 of file Memcached.php.

53  {
54  return "{$id}[{$index}]";
55  }

◆ load()

load (   $id,
  $doNotTestCacheValidity = false 
)

Load data from memcached, glue from several chunks if it was splitted upon save.

Parameters
string$id
See also
\Zend_Cache_Backend_Memcached::load()
Parameters
bool$doNotTestCacheValidity
See also
\Zend_Cache_Backend_Memcached::load()
Returns
bool|false|string

Implements Zend_Cache_Backend_Interface.

Definition at line 109 of file Memcached.php.

110  {
111  $data = parent::load($id, $doNotTestCacheValidity);
112 
113  if (is_string($data) && substr($data, 0, strlen(self::CODE_WORD)) == self::CODE_WORD) {
114  // Seems we've got chunked data
115 
116  $arr = explode('|', $data);
117  $chunks = isset($arr[1]) ? $arr[1] : false;
118  $chunkData = [];
119 
120  if ($chunks && is_numeric($chunks)) {
121  for ($i = 0; $i < $chunks; $i++) {
122  $chunk = parent::load($this->_getChunkId($id, $i), $doNotTestCacheValidity);
123 
124  if (false === $chunk) {
125  // Some chunk in chain was not found, we can not glue-up the data:
126  // clean the mess and return nothing
127 
128  $this->_cleanTheMess($id, $chunks);
129  return false;
130  }
131 
132  $chunkData[] = $chunk;
133  }
134 
135  return implode('', $chunkData);
136  }
137  }
138 
139  // Data has not been splitted to chunks on save
140  return $data;
141  }
$id
Definition: fieldset.phtml:14
$i
Definition: gallery.phtml:31

◆ save()

save (   $data,
  $id,
  $tags = [],
  $specificLifetime = false 
)

Save data to memcached, split it into chunks if data size is bigger than memcached slab size.

Parameters
string$data
See also
\Zend_Cache_Backend_Memcached::save()
Parameters
string$id
See also
\Zend_Cache_Backend_Memcached::save()
Parameters
string[]$tags
See also
\Zend_Cache_Backend_Memcached::save()
Parameters
bool$specificLifetime
See also
\Zend_Cache_Backend_Memcached::save()
Returns
bool

Implements Zend_Cache_Backend_Interface.

Definition at line 82 of file Memcached.php.

83  {
84  if (is_string($data) && strlen($data) > $this->_options['slab_size']) {
85  $dataChunks = str_split($data, $this->_options['slab_size']);
86 
87  for ($i = 0, $cnt = count($dataChunks); $i < $cnt; $i++) {
88  $chunkId = $this->_getChunkId($id, $i);
89 
90  if (!parent::save($dataChunks[$i], $chunkId, $tags, $specificLifetime)) {
91  $this->_cleanTheMess($id, $i + 1);
92  return false;
93  }
94  }
95 
96  $data = self::CODE_WORD . '|' . $i;
97  }
98 
99  return parent::save($data, $id, $tags, $specificLifetime);
100  }
$id
Definition: fieldset.phtml:14
$i
Definition: gallery.phtml:31

Field Documentation

◆ CODE_WORD

const CODE_WORD = '{splitted}'

Used to tell chunked data from ordinary

Definition at line 18 of file Memcached.php.

◆ DEFAULT_SLAB_SIZE

const DEFAULT_SLAB_SIZE = 1048576

Maximum chunk of data that could be saved in one memcache cell (1 MiB)

Definition at line 13 of file Memcached.php.


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