Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/cache/cache.php

Documentation is available at cache.php

  1. <?php
  2. /**
  3.  * @version        $Id: cache.php 12694 2009-09-11 21:03:02Z ian $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Cache
  6.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant to the
  9.  *  GNU General Public License, and as distributed it includes or is derivative
  10.  *  of works licensed under the GNU General Public License or other free or open
  11.  *  source software licenses. See COPYRIGHT.php for copyright notices and
  12.  *  details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. //Register the session storage class with the loader
  19. JLoader::register('JCacheStorage'dirname(__FILE__).DS.'storage.php');
  20.  
  21. /**
  22.  * Joomla! Cache base object
  23.  *
  24.  * @abstract
  25.  * @package        Joomla.Framework
  26.  * @subpackage    Cache
  27.  * @since        1.5
  28.  */
  29. class JCache extends JObject
  30. {
  31.     /**
  32.      * Storage Handler
  33.      * @access    private
  34.      * @var        object 
  35.      */
  36.     var $_handler;
  37.  
  38.     /**
  39.      * Cache Options
  40.      * @access    private
  41.      * @var        array 
  42.      */
  43.     var $_options;
  44.  
  45.     /**
  46.      * Constructor
  47.      *
  48.      * @access    protected
  49.      * @param    array    $options    options
  50.      */
  51.     function __construct($options)
  52.     {
  53.         $this->_options =$options;
  54.  
  55.         // Get the default group and caching
  56.         if(isset($options['language'])) {
  57.             $this->_options['language'$options['language'];
  58.         else {
  59.             $options['language''en-GB';
  60.         }
  61.  
  62.         if(isset($options['cachebase'])) {
  63.             $this->_options['cachebase'$options['cachebase'];
  64.         else {
  65.             $this->_options['cachebase'JPATH_ROOT.DS.'cache';
  66.         }
  67.  
  68.         if(isset($options['defaultgroup'])) {
  69.             $this->_options['defaultgroup'$options['defaultgroup'];
  70.         else {
  71.             $this->_options['defaultgroup''default';
  72.         }
  73.  
  74.         if(isset($options['caching'])) {
  75.             $this->_options['caching'=  $options['caching'];
  76.         else {
  77.             $this->_options['caching'true;
  78.         }
  79.  
  80.         ifisset($options['storage'])) {
  81.             $this->_options['storage'$options['storage'];
  82.         else {
  83.             $this->_options['storage''file';
  84.         }
  85.  
  86.         //Fix to detect if template positions are enabled...
  87.         if(JRequest::getCMD('tpl',0)) {
  88.             $this->_options['caching'false;
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * Returns a reference to a cache adapter object, always creating it
  94.      *
  95.      * @static
  96.      * @param    string    $type    The cache object type to instantiate
  97.      * @return    object    JCache object
  98.      * @since    1.5
  99.      */
  100.     function &getInstance($type 'output'$options array())
  101.     {
  102.         $type strtolower(preg_replace('/[^A-Z0-9_\.-]/i'''$type));
  103.  
  104.         $class 'JCache'.ucfirst($type);
  105.  
  106.         if(!class_exists($class))
  107.         {
  108.             $path dirname(__FILE__).DS.'handler'.DS.$type.'.php';
  109.  
  110.             if (file_exists($path)) {
  111.                 require_once($path);
  112.             else {
  113.                 JError::raiseError(500'Unable to load Cache Handler: '.$type);
  114.             }
  115.         }
  116.  
  117.         $instance new $class($options);
  118.  
  119.         return $instance;
  120.     }
  121.  
  122.     /**
  123.      * Get the storage handlers
  124.      *
  125.      * @access public
  126.      * @return array An array of available storage handlers
  127.      */
  128.     function getStores()
  129.     {
  130.         jimport('joomla.filesystem.folder');
  131.         $handlers JFolder::files(dirname(__FILE__).DS.'storage''.php$');
  132.  
  133.         $names array();
  134.         foreach($handlers as $handler)
  135.         {
  136.             $name substr($handler0strrpos($handler'.'));
  137.             $class 'JCacheStorage'.$name;
  138.  
  139.             if(!class_exists($class)) {
  140.                 require_once(dirname(__FILE__).DS.'storage'.DS.$name.'.php');
  141.             }
  142.  
  143.             if(call_user_func_arrayarraytrim($class)'test' )array())) {
  144.                 $names[$name;
  145.             }
  146.         }
  147.  
  148.         return $names;
  149.     }
  150.  
  151.     /**
  152.      * Set caching enabled state
  153.      *
  154.      * @access    public
  155.      * @param    boolean    $enabled    True to enable caching
  156.      * @return    void 
  157.      * @since    1.5
  158.      */
  159.     function setCaching($enabled)
  160.     {
  161.         $this->_options['caching'$enabled;
  162.     }
  163.  
  164.     /**
  165.      * Set cache lifetime
  166.      *
  167.      * @access    public
  168.      * @param    int    $lt    Cache lifetime
  169.      * @return    void 
  170.      * @since    1.5
  171.      */
  172.     function setLifeTime($lt)
  173.     {
  174.         $this->_options['lifetime'$lt;
  175.     }
  176.  
  177.     /**
  178.      * Set cache validation
  179.      *
  180.      * @access    public
  181.      * @return    void 
  182.      * @since    1.5
  183.      */
  184.     function setCacheValidation()
  185.     {
  186.         // Deprecated
  187.     }
  188.  
  189.     /**
  190.      * Get cached data by id and group
  191.      *
  192.      * @abstract
  193.      * @access    public
  194.      * @param    string    $id        The cache data id
  195.      * @param    string    $group    The cache data group
  196.      * @return    mixed    Boolean false on failure or a cached data string
  197.      * @since    1.5
  198.      */
  199.     function get($id$group=null)
  200.     {
  201.         // Get the default group
  202.         $group ($group$group $this->_options['defaultgroup'];
  203.  
  204.         // Get the storage handler
  205.         $handler =$this->_getStorage();
  206.         if (!JError::isError($handler&& $this->_options['caching']{
  207.             return $handler->get($id$group(isset($this->_options['checkTime']))$this->_options['checkTime'true);
  208.         }
  209.         return false;
  210.     }
  211.  
  212.     /**
  213.      * Store the cached data by id and group
  214.      *
  215.      * @access    public
  216.      * @param    string    $id        The cache data id
  217.      * @param    string    $group    The cache data group
  218.      * @param    mixed    $data    The data to store
  219.      * @return    boolean    True if cache stored
  220.      * @since    1.5
  221.      */
  222.     function store($data$id$group=null)
  223.     {
  224.         // Get the default group
  225.         $group ($group$group $this->_options['defaultgroup'];
  226.  
  227.         // Get the storage handler and store the cached data
  228.         $handler =$this->_getStorage();
  229.         if (!JError::isError($handler&& $this->_options['caching']{
  230.             return $handler->store($id$group$data);
  231.         }
  232.         return false;
  233.     }
  234.  
  235.     /**
  236.      * Remove a cached data entry by id and group
  237.      *
  238.      * @abstract
  239.      * @access    public
  240.      * @param    string    $id        The cache data id
  241.      * @param    string    $group    The cache data group
  242.      * @return    boolean    True on success, false otherwise
  243.      * @since    1.5
  244.      */
  245.     function remove($id$group=null)
  246.     {
  247.         // Get the default group
  248.         $group ($group$group $this->_options['defaultgroup'];
  249.  
  250.         // Get the storage handler
  251.         $handler =$this->_getStorage();
  252.         if (!JError::isError($handler)) {
  253.             return $handler->remove($id$group);
  254.         }
  255.         return false;
  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.      * @access    public
  265.      * @param    string    $group    The cache data group
  266.      * @param    string    $mode    The mode for cleaning cache [group|notgroup]
  267.      * @return    boolean    True on success, false otherwise
  268.      * @since    1.5
  269.      */
  270.     function clean($group=null$mode='group')
  271.     {
  272.         // Get the default group
  273.         $group ($group$group $this->_options['defaultgroup'];
  274.  
  275.         // Get the storage handler
  276.         $handler =$this->_getStorage();
  277.         if (!JError::isError($handler)) {
  278.             return $handler->clean($group$mode);
  279.         }
  280.         return false;
  281.     }
  282.  
  283.     /**
  284.      * Garbage collect expired cache data
  285.      *
  286.      * @access public
  287.      * @return boolean  True on success, false otherwise.
  288.      * @since    1.5
  289.      */
  290.     function gc()
  291.     {
  292.         // Get the storage handler
  293.         $handler =$this->_getStorage();
  294.         if (!JError::isError($handler)) {
  295.             return $handler->gc();
  296.         }
  297.         return false;
  298.     }
  299.  
  300.     /**
  301.      * Get the cache storage handler
  302.      *
  303.      * @access protected
  304.      * @return object JCacheStorage object
  305.      * @since    1.5
  306.      */
  307.     function &_getStorage()
  308.     {
  309.         if (is_a($this->_handler'JCacheStorage')) {
  310.             return $this->_handler;
  311.         }
  312.  
  313.         $this->_handler =JCacheStorage::getInstance($this->_options['storage']$this->_options);
  314.         return $this->_handler;
  315.     }
  316. }

Documentation generated on Sat, 14 Nov 2009 11:11:13 +0000 by phpDocumentor 1.3.1