Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Xcache.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Cache/Backend/Interface.php';
28 
32 #require_once 'Zend/Cache/Backend.php';
33 
34 
42 {
43 
47  const TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND = 'Zend_Cache_Backend_Xcache::clean() : tags are unsupported by the Xcache backend';
48  const TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND = 'Zend_Cache_Backend_Xcache::save() : tags are unsupported by the Xcache backend';
49 
61  protected $_options = array(
62  'user' => null,
63  'password' => null
64  );
65 
73  public function __construct(array $options = array())
74  {
75  if (!extension_loaded('xcache')) {
76  Zend_Cache::throwException('The xcache extension must be loaded for using this backend !');
77  }
78  parent::__construct($options);
79  }
80 
90  public function load($id, $doNotTestCacheValidity = false)
91  {
92  if ($doNotTestCacheValidity) {
93  $this->_log("Zend_Cache_Backend_Xcache::load() : \$doNotTestCacheValidity=true is unsupported by the Xcache backend");
94  }
95  $tmp = xcache_get($id);
96  if (is_array($tmp)) {
97  return $tmp[0];
98  }
99  return false;
100  }
101 
108  public function test($id)
109  {
110  if (xcache_isset($id)) {
111  $tmp = xcache_get($id);
112  if (is_array($tmp)) {
113  return $tmp[1];
114  }
115  }
116  return false;
117  }
118 
131  public function save($data, $id, $tags = array(), $specificLifetime = false)
132  {
133  $lifetime = $this->getLifetime($specificLifetime);
134  $result = xcache_set($id, array($data, time()), $lifetime);
135  if (count($tags) > 0) {
136  $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND);
137  }
138  return $result;
139  }
140 
147  public function remove($id)
148  {
149  return xcache_unset($id);
150  }
151 
167  public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
168  {
169  switch ($mode) {
171  // Necessary because xcache_clear_cache() need basic authentification
172  $backup = array();
173  if (isset($_SERVER['PHP_AUTH_USER'])) {
174  $backup['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER'];
175  }
176  if (isset($_SERVER['PHP_AUTH_PW'])) {
177  $backup['PHP_AUTH_PW'] = $_SERVER['PHP_AUTH_PW'];
178  }
179  if ($this->_options['user']) {
180  $_SERVER['PHP_AUTH_USER'] = $this->_options['user'];
181  }
182  if ($this->_options['password']) {
183  $_SERVER['PHP_AUTH_PW'] = $this->_options['password'];
184  }
185 
186  $cnt = xcache_count(XC_TYPE_VAR);
187  for ($i=0; $i < $cnt; $i++) {
188  xcache_clear_cache(XC_TYPE_VAR, $i);
189  }
190 
191  if (isset($backup['PHP_AUTH_USER'])) {
192  $_SERVER['PHP_AUTH_USER'] = $backup['PHP_AUTH_USER'];
193  $_SERVER['PHP_AUTH_PW'] = $backup['PHP_AUTH_PW'];
194  }
195  return true;
196  break;
198  $this->_log("Zend_Cache_Backend_Xcache::clean() : CLEANING_MODE_OLD is unsupported by the Xcache backend");
199  break;
203  $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND);
204  break;
205  default:
206  Zend_Cache::throwException('Invalid mode for clean() method');
207  break;
208  }
209  }
210 
217  {
218  return false;
219  }
220 
221 }
$id
Definition: fieldset.phtml:14
load($id, $doNotTestCacheValidity=false)
Definition: Xcache.php:90
const TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND
Definition: Xcache.php:47
const TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND
Definition: Xcache.php:48
save($data, $id, $tags=array(), $specificLifetime=false)
Definition: Xcache.php:131
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
__construct(array $options=array())
Definition: Xcache.php:73
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
clean($mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
Definition: Xcache.php:167
getLifetime($specificLifetime)
Definition: Backend.php:143
$i
Definition: gallery.phtml:31