Source for file storage.php

Documentation is available at storage.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Cache
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Abstract cache storage handler
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Cache
  17.  * @since       11.1
  18.  */
  19. {
  20.     /**
  21.      * @var    string  Rawname
  22.      * @since  11.1
  23.      */
  24.     protected $rawname;
  25.  
  26.     /**
  27.      * @var    datetime  Now
  28.      * @since  11.1
  29.      */
  30.     public $_now;
  31.  
  32.     /**
  33.      * @var    integer  Cache lifetime
  34.      * @since  11.1
  35.      */
  36.     public $_lifetime;
  37.  
  38.     /**
  39.      * @var    boolean  Locking
  40.      * @since  11.1
  41.      */
  42.     public $_locking;
  43.  
  44.     /**
  45.      * @var    string  Language
  46.      * @since  11.1
  47.      */
  48.     public $_language;
  49.  
  50.     /**
  51.      * @var    string  Application name.
  52.      * @since  11.1
  53.      */
  54.     public $_application;
  55.  
  56.     /**
  57.      * @var    string  Hash
  58.      * @since  11.1
  59.      */
  60.     public $_hash;
  61.  
  62.     /**
  63.      * Constructor
  64.      *
  65.      * @param   array  $options  Optional parameters
  66.      *
  67.      * @since   11.1
  68.      */
  69.     public function __construct($options array())
  70.     {
  71.         $config JFactory::getConfig();
  72.         $this->_hash = md5($config->get('secret'));
  73.         $this->_application = (isset($options['application'])) $options['application'null;
  74.         $this->_language = (isset($options['language'])) $options['language''en-GB';
  75.         $this->_locking = (isset($options['locking'])) $options['locking'true;
  76.         $this->_lifetime = (isset($options['lifetime'])) $options['lifetime'60 $config->get('cachetime'60;
  77.         $this->_now = (isset($options['now'])) $options['now'time();
  78.  
  79.         // Set time threshold value.  If the lifetime is not set, default to 60 (0 is BAD)
  80.         // _threshold is now available ONLY as a legacy (it's deprecated).  It's no longer used in the core.
  81.         if (empty($this->_lifetime))
  82.         {
  83.             $this->_threshold $this->_now - 60;
  84.             $this->_lifetime = 60;
  85.         }
  86.         else
  87.         {
  88.             $this->_threshold $this->_now - $this->_lifetime;
  89.         }
  90.  
  91.     }
  92.  
  93.     /**
  94.      * Returns a cache storage handler object, only creating it
  95.      * if it doesn't already exist.
  96.      *
  97.      * @param   string  $handler  The cache storage handler to instantiate
  98.      * @param   array   $options  Array of handler options
  99.      *
  100.      * @return  JCacheStorage  A JCacheStorage instance
  101.      *
  102.      * @since   11.1
  103.      * @throws  UnexpectedValueException
  104.      * @throws  RuntimeException
  105.      */
  106.     public static function getInstance($handler null$options array())
  107.     {
  108.         static $now null;
  109.  
  110.         self::addIncludePath(JPATH_PLATFORM '/joomla/cache/storage');
  111.  
  112.         if (!isset($handler))
  113.         {
  114.             $conf JFactory::getConfig();
  115.             $handler $conf->get('cache_handler');
  116.             if (empty($handler))
  117.             {
  118.                 throw new UnexpectedValueException('Cache Storage Handler not set.');
  119.             }
  120.         }
  121.  
  122.         if (is_null($now))
  123.         {
  124.             $now time();
  125.         }
  126.  
  127.         $options['now'$now;
  128.  
  129.         // We can't cache this since options may change...
  130.         $handler strtolower(preg_replace('/[^A-Z0-9_\.-]/i'''$handler));
  131.  
  132.         $class 'JCacheStorage' ucfirst($handler);
  133.         if (!class_exists($class))
  134.         {
  135.             // Search for the class file in the JCacheStorage include paths.
  136.             jimport('joomla.filesystem.path');
  137.             if ($path JPath::find(self::addIncludePath()strtolower($handler'.php'))
  138.             {
  139.                 include_once $path;
  140.             }
  141.             else
  142.             {
  143.                 throw new RuntimeException(sprintf('Unable to load Cache Storage: %s'$handler));
  144.             }
  145.         }
  146.  
  147.         return new $class($options);
  148.     }
  149.  
  150.     /**
  151.      * Get cached data by id and group
  152.      *
  153.      * @param   string   $id         The cache data id
  154.      * @param   string   $group      The cache data group
  155.      * @param   boolean  $checkTime  True to verify cache time expiration threshold
  156.      *
  157.      * @return  mixed  Boolean  false on failure or a cached data object
  158.      *
  159.      * @since   11.1
  160.      */
  161.     public function get($id$group$checkTime true)
  162.     {
  163.         return false;
  164.     }
  165.  
  166.     /**
  167.      * Get all cached data
  168.      *
  169.      * @return  mixed    Boolean false on failure or a cached data object
  170.      *
  171.      * @since   11.1
  172.      * @todo    Review this method. The docblock doesn't fit what it actually does.
  173.      */
  174.     public function getAll()
  175.     {
  176.         if (!class_exists('JCacheStorageHelper'false))
  177.         {
  178.             include_once JPATH_PLATFORM '/joomla/cache/storage/helper.php';
  179.         }
  180.         return;
  181.     }
  182.  
  183.     /**
  184.      * Store the data to cache by id and group
  185.      *
  186.      * @param   string  $id     The cache data id
  187.      * @param   string  $group  The cache data group
  188.      * @param   string  $data   The data to store in cache
  189.      *
  190.      * @return  boolean  True on success, false otherwise
  191.      *
  192.      * @since   11.1
  193.      */
  194.     public function store($id$group$data)
  195.     {
  196.         return true;
  197.     }
  198.  
  199.     /**
  200.      * Remove a cached data entry by id and group
  201.      *
  202.      * @param   string  $id     The cache data id
  203.      * @param   string  $group  The cache data group
  204.      *
  205.      * @return  boolean  True on success, false otherwise
  206.      *
  207.      * @since   11.1
  208.      */
  209.     public function remove($id$group)
  210.     {
  211.         return true;
  212.     }
  213.  
  214.     /**
  215.      * Clean cache for a group given a mode.
  216.      *
  217.      * @param   string  $group  The cache data group
  218.      * @param   string  $mode   The mode for cleaning cache [group|notgroup]
  219.      *                           group mode     : cleans all cache in the group
  220.      *                           notgroup mode  : cleans all cache not in the group
  221.      *
  222.      * @return  boolean  True on success, false otherwise
  223.      *
  224.      * @since   11.1
  225.      */
  226.     public function clean($group$mode null)
  227.     {
  228.         return true;
  229.     }
  230.  
  231.     /**
  232.      * Garbage collect expired cache data
  233.      *
  234.      * @return boolean  True on success, false otherwise.
  235.      *
  236.      * @since   11.1
  237.      */
  238.     public function gc()
  239.     {
  240.         return true;
  241.     }
  242.  
  243.     /**
  244.      * Test to see if the storage handler is available.
  245.      *
  246.      * @return   boolean  True on success, false otherwise
  247.      *
  248.      * @since    12.1.
  249.      */
  250.     public static function isSupported()
  251.     {
  252.         return true;
  253.     }
  254.  
  255.     /**
  256.      * Test to see if the storage handler is available.
  257.      *
  258.      * @return  boolean  True on success, false otherwise.
  259.      *
  260.      * @since   11.1
  261.      * @deprecated  12.3 (Platform) & 4.0 (CMS)
  262.      */
  263.     public static function test()
  264.     {
  265.         JLog::add('JCacheStorage::test() is deprecated. Use JCacheStorage::isSupported() instead.'JLog::WARNING'deprecated');
  266.  
  267.         return static::isSupported();
  268.     }
  269.  
  270.     /**
  271.      * Lock cached item
  272.      *
  273.      * @param   string   $id        The cache data id
  274.      * @param   string   $group     The cache data group
  275.      * @param   integer  $locktime  Cached item max lock time
  276.      *
  277.      * @return  boolean  True on success, false otherwise.
  278.      *
  279.      * @since   11.1
  280.      */
  281.     public function lock($id$group$locktime)
  282.     {
  283.         return false;
  284.     }
  285.  
  286.     /**
  287.      * Unlock cached item
  288.      *
  289.      * @param   string  $id     The cache data id
  290.      * @param   string  $group  The cache data group
  291.      *
  292.      * @return  boolean  True on success, false otherwise.
  293.      *
  294.      * @since   11.1
  295.      */
  296.     public function unlock($id$group null)
  297.     {
  298.         return false;
  299.     }
  300.  
  301.     /**
  302.      * Get a cache_id string from an id/group pair
  303.      *
  304.      * @param   string  $id     The cache data id
  305.      * @param   string  $group  The cache data group
  306.      *
  307.      * @return  string   The cache_id string
  308.      *
  309.      * @since   11.1
  310.      */
  311.     protected function _getCacheId($id$group)
  312.     {
  313.         $name md5($this->_application . '-' $id '-' $this->_language);
  314.         $this->rawname = $this->_hash . '-' $name;
  315.         return $this->_hash . '-cache-' $group '-' $name;
  316.     }
  317.  
  318.     /**
  319.      * Add a directory where JCacheStorage should search for handlers. You may
  320.      * either pass a string or an array of directories.
  321.      *
  322.      * @param   string  $path  A path to search.
  323.      *
  324.      * @return  array  An array with directory elements
  325.      *
  326.      * @since   11.1
  327.      */
  328.     public static function addIncludePath($path '')
  329.     {
  330.         static $paths;
  331.  
  332.         if (!isset($paths))
  333.         {
  334.             $paths array();
  335.         }
  336.  
  337.         if (!empty($path&& !in_array($path$paths))
  338.         {
  339.             jimport('joomla.filesystem.path');
  340.             array_unshift($pathsJPath::clean($path));
  341.         }
  342.  
  343.         return $paths;
  344.     }
  345. }

Documentation generated on Tue, 19 Nov 2013 15:14:21 +0100 by phpDocumentor 1.4.3