Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/cache/cache.php

Documentation is available at cache.php

  1. <?php
  2. /**
  3.  * @version        $Id: cache.php 6719 2007-02-25 00:31:45Z Jinx $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Cache
  6.  * @copyright    Copyright (C) 2005 - 2007 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. /**
  19.  * Joomla! Cache base object
  20.  *
  21.  * @abstract
  22.  * @author        Louis Landry <[email protected]>
  23.  * @package        Joomla.Framework
  24.  * @subpackage    Cache
  25.  * @since        1.5
  26.  */
  27. class JCache extends JObject
  28. {
  29.     /**
  30.      * Storage Handler
  31.      * @access    private
  32.      * @var        object 
  33.      */
  34.     var $_handler;
  35.  
  36.     /**
  37.      * Cache Options
  38.      * @access    private
  39.      * @var        array 
  40.      */
  41.     var $_options;
  42.  
  43.     /**
  44.      * Constructor
  45.      *
  46.      * @access    protected
  47.      * @param    array    $options    options
  48.      */
  49.     function __construct($options)
  50.     {
  51.         $this->_options =$options;
  52.  
  53.         // Get the default group and caching
  54.         $this->_options['cachebase']    = isset($options['cachebase']$options['cachebase''cache';
  55.         $this->_options['defaultgroup'= isset($options['defaultgroup']$options['defaultgroup''default';
  56.         $this->_options['caching']      = isset($options['caching']$options['caching'true;
  57.     }
  58.  
  59.     /**
  60.      * Returns a reference to a cache adapter object, only creating it
  61.      * if it doesn't already exist.
  62.      *
  63.      * @static
  64.      * @param    string    $type    The cache object type to instantiate
  65.      * @return    object    JCache object
  66.      * @since    1.5
  67.      */
  68.     function &getInstance($type 'output'$options array())
  69.     {
  70.  
  71.         $type strtolower($type);
  72.         jimport('joomla.cache.handlers.'.$type);
  73.         $class 'JCache'.ucfirst($type);
  74.         $instance new $class($options);
  75.  
  76.         return $instance;
  77.     }
  78.  
  79.     /**
  80.      * Set caching enabled state
  81.      *
  82.      * @access    public
  83.      * @param    boolean    $enabled    True to enable caching
  84.      * @return    void 
  85.      * @since    1.5
  86.      */
  87.     function setCaching($enabled)
  88.     {
  89.         $this->_options['caching'$enabled;
  90.     }
  91.  
  92.     /**
  93.      * Set cache lifetime
  94.      *
  95.      * @access    public
  96.      * @param    int    $lt    Cache lifetime
  97.      * @return    void 
  98.      * @since    1.5
  99.      */
  100.     function setLifeTime($lt)
  101.     {
  102.         $this->_options['lifetime'$lt;
  103.     }
  104.  
  105.     /**
  106.      * Set cache validation
  107.      *
  108.      * @access    public
  109.      * @return    void 
  110.      * @since    1.5
  111.      */
  112.     function setCacheValidation()
  113.     {
  114.         // Deprecated
  115.     }
  116.  
  117.     /**
  118.      * Get cached data by id and group
  119.      *
  120.      * @abstract
  121.      * @access    public
  122.      * @param    string    $id        The cache data id
  123.      * @param    string    $group    The cache data group
  124.      * @return    mixed    Boolean false on failure or a cached data string
  125.      * @since    1.5
  126.      */
  127.     function get($id$group=null)
  128.     {
  129.         // Get the default group
  130.         $group ($group$group $this->_options['defaultgroup'];
  131.  
  132.         // Get the storage handler
  133.         $handler =$this->_getStorageHandler();
  134.         if (!JError::isError($handler&& $this->_options['caching']{
  135.             return $handler->get($id$group(isset($this->_options['checkTime']))$this->_options['checkTime'true);
  136.         }
  137.         return false;
  138.     }
  139.  
  140.     /**
  141.      * Store the cached data by id and group
  142.      *
  143.      * @access    public
  144.      * @param    string    $id        The cache data id
  145.      * @param    string    $group    The cache data group
  146.      * @param    mixed    $data    The data to store
  147.      * @return    boolean    True if cache stored
  148.      * @since    1.5
  149.      */
  150.     function store($data$id$group=null)
  151.     {
  152.         // Get the default group
  153.         $group ($group$group $this->_options['defaultgroup'];
  154.  
  155.         // Get the storage handler and store the cached data
  156.         $handler =$this->_getStorageHandler();
  157.         if (!JError::isError($handler&& $this->_options['caching']{
  158.             return $handler->store($id$group$data);
  159.         }
  160.         return false;
  161.     }
  162.  
  163.     /**
  164.      * Remove a cached data entry by id and group
  165.      *
  166.      * @abstract
  167.      * @access    public
  168.      * @param    string    $id        The cache data id
  169.      * @param    string    $group    The cache data group
  170.      * @return    boolean    True on success, false otherwise
  171.      * @since    1.5
  172.      */
  173.     function remove($id$group=null)
  174.     {
  175.         // Get the default group
  176.         $group ($group$group $this->_options['defaultgroup'];
  177.  
  178.         // Get the storage handler
  179.         $handler =$this->_getStorageHandler();
  180.         if (!JError::isError($handler)) {
  181.             return $handler->remove($id$group);
  182.         }
  183.         return false;
  184.     }
  185.  
  186.     /**
  187.      * Clean cache for a group given a mode.
  188.      *
  189.      * group mode        : cleans all cache in the group
  190.      * notgroup mode    : cleans all cache not in the group
  191.      *
  192.      * @access    public
  193.      * @param    string    $group    The cache data group
  194.      * @param    string    $mode    The mode for cleaning cache [group|notgroup]
  195.      * @return    boolean    True on success, false otherwise
  196.      * @since    1.5
  197.      */
  198.     function clean($group=null$mode='group')
  199.     {
  200.         // Get the default group
  201.         $group ($group$group $this->_options['defaultgroup'];
  202.  
  203.         // Get the storage handler
  204.         $handler =$this->_getStorageHandler();
  205.         if (!JError::isError($handler)) {
  206.             return $handler->clean($group$mode);
  207.         }
  208.         return false;
  209.     }
  210.  
  211.     /**
  212.      * Garbage collect expired cache data
  213.      *
  214.      * @access public
  215.      * @return boolean  True on success, false otherwise.
  216.      */
  217.     function gc()
  218.     {
  219.         // Get the storage handler
  220.         $handler =$this->_getStorageHandler();
  221.         if (!JError::isError($handler)) {
  222.             return $handler->gc();
  223.         }
  224.         return false;
  225.     }
  226.  
  227.     function &_getStorageHandler()
  228.     {
  229.         if (is_a($this->_handler'JCacheStorage')) {
  230.             return $this->_handler;
  231.         }
  232.  
  233.         $config =JFactory::getConfig();
  234.         $handler $config->getValue('config.cache_handler''file');
  235.         jimport('joomla.cache.storage');
  236.         $this->_handler =JCacheStorage::getInstance($handler$this->_options);
  237.         return $this->_handler;
  238.     }
  239. }
  240. ?>

Documentation generated on Mon, 05 Mar 2007 20:53:41 +0000 by phpDocumentor 1.3.1