[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Cache/ -> CacheStorage.php (source)

   1  <?php
   2  /**
   3   * Joomla! Content Management System
   4   *
   5   * @copyright  (C) 2007 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;
  10  
  11  \defined('JPATH_PLATFORM') or die;
  12  
  13  use Joomla\CMS\Cache\Exception\UnsupportedCacheException;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Filesystem\Path;
  16  
  17  /**
  18   * Abstract cache storage handler
  19   *
  20   * @since  1.7.0
  21   * @note   As of 4.0 this class will be abstract
  22   */
  23  class CacheStorage
  24  {
  25      /**
  26       * The raw object name
  27       *
  28       * @var    string
  29       * @since  1.7.0
  30       */
  31      protected $rawname;
  32  
  33      /**
  34       * Time that the cache storage handler was instantiated
  35       *
  36       * @var    integer
  37       * @since  1.7.0
  38       */
  39      public $_now;
  40  
  41      /**
  42       * Cache lifetime
  43       *
  44       * @var    integer
  45       * @since  1.7.0
  46       */
  47      public $_lifetime;
  48  
  49      /**
  50       * Flag if locking is enabled
  51       *
  52       * @var    boolean
  53       * @since  1.7.0
  54       */
  55      public $_locking;
  56  
  57      /**
  58       * Language code
  59       *
  60       * @var    string
  61       * @since  1.7.0
  62       */
  63      public $_language;
  64  
  65      /**
  66       * Application name
  67       *
  68       * @var    string
  69       * @since  1.7.0
  70       */
  71      public $_application;
  72  
  73      /**
  74       * Object hash
  75       *
  76       * @var    string
  77       * @since  1.7.0
  78       */
  79      public $_hash;
  80  
  81      /**
  82       * Constructor
  83       *
  84       * @param   array  $options  Optional parameters
  85       *
  86       * @since   1.7.0
  87       */
  88  	public function __construct($options = array())
  89      {
  90          $app = Factory::getApplication();
  91  
  92          $this->_hash        = md5($app->get('secret'));
  93          $this->_application = $options['application'] ?? md5(JPATH_CONFIGURATION);
  94          $this->_language    = $options['language'] ?? 'en-GB';
  95          $this->_locking     = $options['locking'] ?? true;
  96          $this->_lifetime    = ($options['lifetime'] ?? $app->get('cachetime')) * 60;
  97          $this->_now         = $options['now'] ?? time();
  98  
  99          // Set time threshold value.  If the lifetime is not set, default to 60 (0 is BAD)
 100          // _threshold is now available ONLY as a legacy (it's deprecated).  It's no longer used in the core.
 101          if (empty($this->_lifetime))
 102          {
 103              $this->_threshold = $this->_now - 60;
 104              $this->_lifetime = 60;
 105          }
 106          else
 107          {
 108              $this->_threshold = $this->_now - $this->_lifetime;
 109          }
 110      }
 111  
 112      /**
 113       * Returns a cache storage handler object.
 114       *
 115       * @param   string  $handler  The cache storage handler to instantiate
 116       * @param   array   $options  Array of handler options
 117       *
 118       * @return  CacheStorage
 119       *
 120       * @since   1.7.0
 121       * @throws  \UnexpectedValueException
 122       * @throws  UnsupportedCacheException
 123       */
 124  	public static function getInstance($handler = null, $options = array())
 125      {
 126          static $now = null;
 127  
 128          if (!isset($handler))
 129          {
 130              $handler = Factory::getApplication()->get('cache_handler');
 131  
 132              if (empty($handler))
 133              {
 134                  throw new \UnexpectedValueException('Cache Storage Handler not set.');
 135              }
 136          }
 137  
 138          if (\is_null($now))
 139          {
 140              $now = time();
 141          }
 142  
 143          $options['now'] = $now;
 144  
 145          // We can't cache this since options may change...
 146          $handler = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $handler));
 147  
 148          /** @var CacheStorage $class */
 149          $class = __NAMESPACE__ . '\\Storage\\' . ucfirst($handler) . 'Storage';
 150  
 151          if (!class_exists($class))
 152          {
 153              $class = 'JCacheStorage' . ucfirst($handler);
 154          }
 155  
 156          if (!class_exists($class))
 157          {
 158              // Search for the class file in the JCacheStorage include paths.
 159              $path = Path::find(self::addIncludePath(), strtolower($handler) . '.php');
 160  
 161              if ($path === false)
 162              {
 163                  throw new UnsupportedCacheException(sprintf('Unable to load Cache Storage: %s', $handler));
 164              }
 165  
 166              \JLoader::register($class, $path);
 167  
 168              // The class should now be loaded
 169              if (!class_exists($class))
 170              {
 171                  throw new UnsupportedCacheException(sprintf('Unable to load Cache Storage: %s', $handler));
 172              }
 173          }
 174  
 175          // Validate the cache storage is supported on this platform
 176          if (!$class::isSupported())
 177          {
 178              throw new UnsupportedCacheException(sprintf('The %s Cache Storage is not supported on this platform.', $handler));
 179          }
 180  
 181          return new $class($options);
 182      }
 183  
 184      /**
 185       * Check if the cache contains data stored by ID and group
 186       *
 187       * @param   string  $id     The cache data ID
 188       * @param   string  $group  The cache data group
 189       *
 190       * @return  boolean
 191       *
 192       * @since   3.7.0
 193       */
 194  	public function contains($id, $group)
 195      {
 196          return false;
 197      }
 198  
 199      /**
 200       * Get cached data by ID and group
 201       *
 202       * @param   string   $id         The cache data ID
 203       * @param   string   $group      The cache data group
 204       * @param   boolean  $checkTime  True to verify cache time expiration threshold
 205       *
 206       * @return  mixed  Boolean false on failure or a cached data object
 207       *
 208       * @since   1.7.0
 209       */
 210  	public function get($id, $group, $checkTime = true)
 211      {
 212          return false;
 213      }
 214  
 215      /**
 216       * Get all cached data
 217       *
 218       * @return  mixed  Boolean false on failure or a cached data object
 219       *
 220       * @since   1.7.0
 221       */
 222  	public function getAll()
 223      {
 224          return false;
 225      }
 226  
 227      /**
 228       * Store the data to cache by ID and group
 229       *
 230       * @param   string  $id     The cache data ID
 231       * @param   string  $group  The cache data group
 232       * @param   string  $data   The data to store in cache
 233       *
 234       * @return  boolean
 235       *
 236       * @since   1.7.0
 237       */
 238  	public function store($id, $group, $data)
 239      {
 240          return true;
 241      }
 242  
 243      /**
 244       * Remove a cached data entry by ID and group
 245       *
 246       * @param   string  $id     The cache data ID
 247       * @param   string  $group  The cache data group
 248       *
 249       * @return  boolean
 250       *
 251       * @since   1.7.0
 252       */
 253  	public function remove($id, $group)
 254      {
 255          return true;
 256      }
 257  
 258      /**
 259       * Clean cache for a group given a mode.
 260       *
 261       * group mode    : cleans all cache in the group
 262       * notgroup mode : cleans all cache not in the group
 263       *
 264       * @param   string  $group  The cache data group
 265       * @param   string  $mode   The mode for cleaning cache [group|notgroup]
 266       *
 267       * @return  boolean
 268       *
 269       * @since   1.7.0
 270       */
 271  	public function clean($group, $mode = null)
 272      {
 273          return true;
 274      }
 275  
 276      /**
 277       * Flush all existing items in storage.
 278       *
 279       * @return  boolean
 280       *
 281       * @since   3.6.3
 282       */
 283  	public function flush()
 284      {
 285          return true;
 286      }
 287  
 288      /**
 289       * Garbage collect expired cache data
 290       *
 291       * @return  boolean
 292       *
 293       * @since   1.7.0
 294       */
 295      public function gc()
 296      {
 297          return true;
 298      }
 299  
 300      /**
 301       * Test to see if the storage handler is available.
 302       *
 303       * @return  boolean
 304       *
 305       * @since   3.0.0
 306       */
 307  	public static function isSupported()
 308      {
 309          return true;
 310      }
 311  
 312      /**
 313       * Lock cached item
 314       *
 315       * @param   string   $id        The cache data ID
 316       * @param   string   $group     The cache data group
 317       * @param   integer  $locktime  Cached item max lock time
 318       *
 319       * @return  mixed  Boolean false if locking failed or an object containing properties lock and locklooped
 320       *
 321       * @since   1.7.0
 322       */
 323  	public function lock($id, $group, $locktime)
 324      {
 325          return false;
 326      }
 327  
 328      /**
 329       * Unlock cached item
 330       *
 331       * @param   string  $id     The cache data ID
 332       * @param   string  $group  The cache data group
 333       *
 334       * @return  boolean
 335       *
 336       * @since   1.7.0
 337       */
 338  	public function unlock($id, $group = null)
 339      {
 340          return false;
 341      }
 342  
 343      /**
 344       * Get a cache ID string from an ID/group pair
 345       *
 346       * @param   string  $id     The cache data ID
 347       * @param   string  $group  The cache data group
 348       *
 349       * @return  string
 350       *
 351       * @since   1.7.0
 352       */
 353  	protected function _getCacheId($id, $group)
 354      {
 355          $name          = md5($this->_application . '-' . $id . '-' . $this->_language);
 356          $this->rawname = $this->_hash . '-' . $name;
 357  
 358          return Cache::getPlatformPrefix() . $this->_hash . '-cache-' . $group . '-' . $name;
 359      }
 360  
 361      /**
 362       * Add a directory where CacheStorage should search for handlers. You may either pass a string or an array of directories.
 363       *
 364       * @param   array|string  $path  A path to search.
 365       *
 366       * @return  array  An array with directory elements
 367       *
 368       * @since   1.7.0
 369       */
 370  	public static function addIncludePath($path = '')
 371      {
 372          static $paths;
 373  
 374          if (!isset($paths))
 375          {
 376              $paths = array();
 377          }
 378  
 379          if (!empty($path) && !\in_array($path, $paths))
 380          {
 381              array_unshift($paths, Path::clean($path));
 382          }
 383  
 384          return $paths;
 385      }
 386  }


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