Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. 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 9764 2007-12-30 07:48:11Z ircmaxell $
  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.  * @author        Louis Landry <louis.landry@joomla.org>
  26.  * @package        Joomla.Framework
  27.  * @subpackage    Cache
  28.  * @since        1.5
  29.  */
  30. class JCache extends JObject
  31. {
  32.     /**
  33.      * Storage Handler
  34.      * @access    private
  35.      * @var        object 
  36.      */
  37.     var $_handler;
  38.  
  39.     /**
  40.      * Cache Options
  41.      * @access    private
  42.      * @var        array 
  43.      */
  44.     var $_options;
  45.  
  46.     /**
  47.      * Constructor
  48.      *
  49.      * @access    protected
  50.      * @param    array    $options    options
  51.      */
  52.     function __construct($options)
  53.     {
  54.         $this->_options =$options;
  55.  
  56.         // Get the default group and caching
  57.         if(isset($options['language'])) {
  58.             $this->_options['language'$options['language'];
  59.         else {
  60.             $options['language''en-GB';
  61.         }
  62.  
  63.         if(isset($options['cachebase'])) {
  64.             $this->_options['cachebase'$options['cachebase'];
  65.         else {
  66.             $this->_options['cachebase'JPATH_ROOT.DS.'cache';
  67.         }
  68.  
  69.         if(isset($options['defaultgroup'])) {
  70.             $this->_options['defaultgroup'$options['defaultgroup'];
  71.         else {
  72.             $this->_options['defaultgroup''default';
  73.         }
  74.  
  75.         if(isset($options['caching'])) {
  76.             $this->_options['caching'=  $options['caching'];
  77.         else {
  78.             $this->_options['caching'true;
  79.         }
  80.  
  81.         ifisset($options['storage'])) {
  82.             $this->_options['storage'$options['storage'];
  83.         else {
  84.             $this->_options['storage''file';
  85.         }
  86.     }
  87.  
  88.     /**
  89.      * Returns a reference to a cache adapter object, always creating it
  90.      *
  91.      * @static
  92.      * @param    string    $type    The cache object type to instantiate
  93.      * @return    object    JCache object
  94.      * @since    1.5
  95.      */
  96.     function &getInstance($type 'output'$options array())
  97.     {
  98.         $type strtolower(preg_replace('/[^A-Z0-9_\.-]/i'''$type));
  99.  
  100.         $class 'JCache'.ucfirst($type);
  101.  
  102.         if(!class_exists($class))
  103.         {
  104.             $path dirname(__FILE__).DS.'handler'.DS.$type.'.php';
  105.  
  106.             if (file_exists($path)) {
  107.                 require_once($path);
  108.             else {
  109.                 JError::raiseError(500'Unable to load Cache Handler: '.$type);
  110.             }
  111.         }
  112.  
  113.         $instance new $class($options);
  114.  
  115.         return $instance;
  116.     }
  117.  
  118.     /**
  119.      * Get the storage handlers
  120.      *
  121.      * @access public
  122.      * @return array An array of available storage handlers
  123.      */
  124.     function getStores()
  125.     {
  126.         jimport('joomla.filesystem.folder');
  127.         $handlers JFolder::files(dirname(__FILE__).DS.'storage''.php$');
  128.  
  129.         $names array();
  130.         foreach($handlers as $handler)
  131.         {
  132.             $name substr($handler0strrpos($handler'.'));
  133.             $class 'JCacheStorage'.$name;
  134.  
  135.             if(!class_exists($class)) {
  136.                 require_once(dirname(__FILE__).DS.'storage'.DS.$name.'.php');
  137.             }
  138.  
  139.             if(call_user_func_arrayarraytrim($class)'test' )null)) {
  140.                 $names[$name;
  141.             }
  142.         }
  143.  
  144.         return $names;
  145.     }
  146.  
  147.     /**
  148.      * Set caching enabled state
  149.      *
  150.      * @access    public
  151.      * @param    boolean    $enabled    True to enable caching
  152.      * @return    void 
  153.      * @since    1.5
  154.      */
  155.     function setCaching($enabled)
  156.     {
  157.         $this->_options['caching'$enabled;
  158.     }
  159.  
  160.     /**
  161.      * Set cache lifetime
  162.      *
  163.      * @access    public
  164.      * @param    int    $lt    Cache lifetime
  165.      * @return    void 
  166.      * @since    1.5
  167.      */
  168.     function setLifeTime($lt)
  169.     {
  170.         $this->_options['lifetime'$lt;
  171.     }
  172.  
  173.     /**
  174.      * Set cache validation
  175.      *
  176.      * @access    public
  177.      * @return    void 
  178.      * @since    1.5
  179.      */
  180.     function setCacheValidation()
  181.     {
  182.         // Deprecated
  183.     }
  184.  
  185.     /**
  186.      * Get cached data by id and group
  187.      *
  188.      * @abstract
  189.      * @access    public
  190.      * @param    string    $id        The cache data id
  191.      * @param    string    $group    The cache data group
  192.      * @return    mixed    Boolean false on failure or a cached data string
  193.      * @since    1.5
  194.      */
  195.     function get($id$group=null)
  196.     {
  197.         // Get the default group
  198.         $group ($group$group $this->_options['defaultgroup'];
  199.  
  200.         // Get the storage handler
  201.         $handler =$this->_getStorage();
  202.         if (!JError::isError($handler&& $this->_options['caching']{
  203.             return $handler->get($id$group(isset($this->_options['checkTime']))$this->_options['checkTime'true);
  204.         }
  205.         return false;
  206.     }
  207.  
  208.     /**
  209.      * Store the cached data by id and group
  210.      *
  211.      * @access    public
  212.      * @param    string    $id        The cache data id
  213.      * @param    string    $group    The cache data group
  214.      * @param    mixed    $data    The data to store
  215.      * @return    boolean    True if cache stored
  216.      * @since    1.5
  217.      */
  218.     function store($data$id$group=null)
  219.     {
  220.         // Get the default group
  221.         $group ($group$group $this->_options['defaultgroup'];
  222.  
  223.         // Get the storage handler and store the cached data
  224.         $handler =$this->_getStorage();
  225.         if (!JError::isError($handler&& $this->_options['caching']{
  226.             return $handler->store($id$group$data);
  227.         }
  228.         return false;
  229.     }
  230.  
  231.     /**
  232.      * Remove a cached data entry by id and group
  233.      *
  234.      * @abstract
  235.      * @access    public
  236.      * @param    string    $id        The cache data id
  237.      * @param    string    $group    The cache data group
  238.      * @return    boolean    True on success, false otherwise
  239.      * @since    1.5
  240.      */
  241.     function remove($id$group=null)
  242.     {
  243.         // Get the default group
  244.         $group ($group$group $this->_options['defaultgroup'];
  245.  
  246.         // Get the storage handler
  247.         $handler =$this->_getStorage();
  248.         if (!JError::isError($handler)) {
  249.             return $handler->remove($id$group);
  250.         }
  251.         return false;
  252.     }
  253.  
  254.     /**
  255.      * Clean cache for a group given a mode.
  256.      *
  257.      * group mode        : cleans all cache in the group
  258.      * notgroup mode    : cleans all cache not in the group
  259.      *
  260.      * @access    public
  261.      * @param    string    $group    The cache data group
  262.      * @param    string    $mode    The mode for cleaning cache [group|notgroup]
  263.      * @return    boolean    True on success, false otherwise
  264.      * @since    1.5
  265.      */
  266.     function clean($group=null$mode='group')
  267.     {
  268.         // Get the default group
  269.         $group ($group$group $this->_options['defaultgroup'];
  270.  
  271.         // Get the storage handler
  272.         $handler =$this->_getStorage();
  273.         if (!JError::isError($handler)) {
  274.             return $handler->clean($group$mode);
  275.         }
  276.         return false;
  277.     }
  278.  
  279.     /**
  280.      * Garbage collect expired cache data
  281.      *
  282.      * @access public
  283.      * @return boolean  True on success, false otherwise.
  284.      * @since    1.5
  285.      */
  286.     function gc()
  287.     {
  288.         // Get the storage handler
  289.         $handler =$this->_getStorage();
  290.         if (!JError::isError($handler)) {
  291.             return $handler->gc();
  292.         }
  293.         return false;
  294.     }
  295.  
  296.     /**
  297.      * Get the cache storage handler
  298.      *
  299.      * @access protected
  300.      * @return object JCacheStorage object
  301.      * @since    1.5
  302.      */
  303.     function &_getStorage()
  304.     {
  305.         if (is_a($this->_handler'JCacheStorage')) {
  306.             return $this->_handler;
  307.         }
  308.  
  309.         $this->_handler =JCacheStorage::getInstance($this->_options['storage']$this->_options);
  310.         return $this->_handler;
  311.     }
  312. }

Documentation generated on Tue, 29 Jan 2008 18:44:48 +0000 by phpDocumentor 1.3.1