[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Cache/Storage/ -> ApcuStorage.php (source)

   1  <?php
   2  /**
   3   * Joomla! Content Management System
   4   *
   5   * @copyright  (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
   6   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   7   */
   8  
   9  namespace Joomla\CMS\Cache\Storage;
  10  
  11  \defined('JPATH_PLATFORM') or die;
  12  
  13  use Joomla\CMS\Cache\CacheStorage;
  14  
  15  /**
  16   * APCu cache storage handler
  17   *
  18   * @link   https://www.php.net/manual/en/ref.apcu.php
  19   * @since  3.5
  20   */
  21  class ApcuStorage extends CacheStorage
  22  {
  23      /**
  24       * Check if the cache contains data stored by ID and group
  25       *
  26       * @param   string  $id     The cache data ID
  27       * @param   string  $group  The cache data group
  28       *
  29       * @return  boolean
  30       *
  31       * @since   3.7.0
  32       */
  33  	public function contains($id, $group)
  34      {
  35          return apcu_exists($this->_getCacheId($id, $group));
  36      }
  37  
  38      /**
  39       * Get cached data by ID and group
  40       *
  41       * @param   string   $id         The cache data ID
  42       * @param   string   $group      The cache data group
  43       * @param   boolean  $checkTime  True to verify cache time expiration threshold
  44       *
  45       * @return  mixed  Boolean false on failure or a cached data object
  46       *
  47       * @since   3.5
  48       */
  49  	public function get($id, $group, $checkTime = true)
  50      {
  51          return apcu_fetch($this->_getCacheId($id, $group));
  52      }
  53  
  54      /**
  55       * Get all cached data
  56       *
  57       * @return  mixed  Boolean false on failure or a cached data object
  58       *
  59       * @since   3.5
  60       */
  61  	public function getAll()
  62      {
  63          $allinfo = apcu_cache_info();
  64          $keys    = $allinfo['cache_list'];
  65          $secret  = $this->_hash;
  66  
  67          $data = array();
  68  
  69          foreach ($keys as $key)
  70          {
  71              if (isset($key['info']))
  72              {
  73                  // The internal key name changed with APCu 4.0.7 from key to info
  74                  $name = $key['info'];
  75              }
  76              elseif (isset($key['entry_name']))
  77              {
  78                  // Some APCu modules changed the internal key name from key to entry_name
  79                  $name = $key['entry_name'];
  80              }
  81              else
  82              {
  83                  // A fall back for the old internal key name
  84                  $name = $key['key'];
  85              }
  86  
  87              $namearr = explode('-', $name);
  88  
  89              if ($namearr !== false && $namearr[0] == $secret && $namearr[1] === 'cache')
  90              {
  91                  $group = $namearr[2];
  92  
  93                  if (!isset($data[$group]))
  94                  {
  95                      $item = new CacheStorageHelper($group);
  96                  }
  97                  else
  98                  {
  99                      $item = $data[$group];
 100                  }
 101  
 102                  $item->updateSize($key['mem_size']);
 103  
 104                  $data[$group] = $item;
 105              }
 106          }
 107  
 108          return $data;
 109      }
 110  
 111      /**
 112       * Store the data to cache by ID and group
 113       *
 114       * @param   string  $id     The cache data ID
 115       * @param   string  $group  The cache data group
 116       * @param   string  $data   The data to store in cache
 117       *
 118       * @return  boolean
 119       *
 120       * @since   3.5
 121       */
 122  	public function store($id, $group, $data)
 123      {
 124          return apcu_store($this->_getCacheId($id, $group), $data, $this->_lifetime);
 125      }
 126  
 127      /**
 128       * Remove a cached data entry by ID and group
 129       *
 130       * @param   string  $id     The cache data ID
 131       * @param   string  $group  The cache data group
 132       *
 133       * @return  boolean
 134       *
 135       * @since   3.5
 136       */
 137  	public function remove($id, $group)
 138      {
 139          $cache_id = $this->_getCacheId($id, $group);
 140  
 141          // The apcu_delete function returns false if the ID does not exist
 142          if (apcu_exists($cache_id))
 143          {
 144              return apcu_delete($cache_id);
 145          }
 146  
 147          return true;
 148      }
 149  
 150      /**
 151       * Clean cache for a group given a mode.
 152       *
 153       * group mode    : cleans all cache in the group
 154       * notgroup mode : cleans all cache not in the group
 155       *
 156       * @param   string  $group  The cache data group
 157       * @param   string  $mode   The mode for cleaning cache [group|notgroup]
 158       *
 159       * @return  boolean
 160       *
 161       * @since   3.5
 162       */
 163  	public function clean($group, $mode = null)
 164      {
 165          $allinfo = apcu_cache_info();
 166          $keys    = $allinfo['cache_list'];
 167          $secret  = $this->_hash;
 168  
 169          foreach ($keys as $key)
 170          {
 171              if (isset($key['info']))
 172              {
 173                  // The internal key name changed with APCu 4.0.7 from key to info
 174                  $internalKey = $key['info'];
 175              }
 176              elseif (isset($key['entry_name']))
 177              {
 178                  // Some APCu modules changed the internal key name from key to entry_name
 179                  $internalKey = $key['entry_name'];
 180              }
 181              else
 182              {
 183                  // A fall back for the old internal key name
 184                  $internalKey = $key['key'];
 185              }
 186  
 187              if (strpos($internalKey, $secret . '-cache-' . $group . '-') === 0 xor $mode !== 'group')
 188              {
 189                  apcu_delete($internalKey);
 190              }
 191          }
 192  
 193          return true;
 194      }
 195  
 196      /**
 197       * Garbage collect expired cache data
 198       *
 199       * @return  boolean
 200       *
 201       * @since   3.5
 202       */
 203      public function gc()
 204      {
 205          $allinfo = apcu_cache_info();
 206          $keys    = $allinfo['cache_list'];
 207          $secret  = $this->_hash;
 208  
 209          foreach ($keys as $key)
 210          {
 211              if (isset($key['info']))
 212              {
 213                  // The internal key name changed with APCu 4.0.7 from key to info
 214                  $internalKey = $key['info'];
 215              }
 216              elseif (isset($key['entry_name']))
 217              {
 218                  // Some APCu modules changed the internal key name from key to entry_name
 219                  $internalKey = $key['entry_name'];
 220              }
 221              else
 222              {
 223                  // A fall back for the old internal key name
 224                  $internalKey = $key['key'];
 225              }
 226  
 227              if (strpos($internalKey, $secret . '-cache-'))
 228              {
 229                  apcu_fetch($internalKey);
 230              }
 231          }
 232  
 233          return true;
 234      }
 235  
 236      /**
 237       * Test to see if the storage handler is available.
 238       *
 239       * @return  boolean
 240       *
 241       * @since   3.5
 242       */
 243  	public static function isSupported()
 244      {
 245          $supported = \extension_loaded('apcu') && ini_get('apc.enabled');
 246  
 247          // If on the CLI interface, the `apc.enable_cli` option must also be enabled
 248          if ($supported && PHP_SAPI === 'cli')
 249          {
 250              $supported = ini_get('apc.enable_cli');
 251          }
 252  
 253          return (bool) $supported;
 254      }
 255  
 256      /**
 257       * Lock cached item
 258       *
 259       * @param   string   $id        The cache data ID
 260       * @param   string   $group     The cache data group
 261       * @param   integer  $locktime  Cached item max lock time
 262       *
 263       * @return  mixed  Boolean false if locking failed or an object containing properties lock and locklooped
 264       *
 265       * @since   3.5
 266       */
 267  	public function lock($id, $group, $locktime)
 268      {
 269          $returning = new \stdClass;
 270          $returning->locklooped = false;
 271  
 272          $looptime = $locktime * 10;
 273  
 274          $cache_id = $this->_getCacheId($id, $group) . '_lock';
 275  
 276          $data_lock = apcu_add($cache_id, 1, $locktime);
 277  
 278          if ($data_lock === false)
 279          {
 280              $lock_counter = 0;
 281  
 282              // Loop until you find that the lock has been released.
 283              // That implies that data get from other thread has finished
 284              while ($data_lock === false)
 285              {
 286                  if ($lock_counter > $looptime)
 287                  {
 288                      $returning->locked = false;
 289                      $returning->locklooped = true;
 290                      break;
 291                  }
 292  
 293                  usleep(100);
 294                  $data_lock = apcu_add($cache_id, 1, $locktime);
 295                  $lock_counter++;
 296              }
 297          }
 298  
 299          $returning->locked = $data_lock;
 300  
 301          return $returning;
 302      }
 303  
 304      /**
 305       * Unlock cached item
 306       *
 307       * @param   string  $id     The cache data ID
 308       * @param   string  $group  The cache data group
 309       *
 310       * @return  boolean
 311       *
 312       * @since   3.5
 313       */
 314  	public function unlock($id, $group = null)
 315      {
 316          $cache_id = $this->_getCacheId($id, $group) . '_lock';
 317  
 318          // The apcu_delete function returns false if the ID does not exist
 319          if (apcu_exists($cache_id))
 320          {
 321              return apcu_delete($cache_id);
 322          }
 323  
 324          return true;
 325      }
 326  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer