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/storage/file.php

Documentation is available at file.php

  1. <?php
  2. /**
  3.  * @version        $Id: file.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
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and 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.  * File cache storage handler
  20.  *
  21.  * @author        Louis Landry <[email protected]>
  22.  * @package        Joomla.Framework
  23.  * @subpackage    Cache
  24.  * @since        1.5
  25.  */
  26. {
  27.     /**
  28.     * Constructor
  29.     *
  30.     * @access protected
  31.     * @param array $options optional parameters
  32.     */
  33.     function __construct$options array() )
  34.     {
  35.         parent::__construct($options);
  36.  
  37.         $config            =JFactory::getConfig();
  38.         $this->_root    $options['cachebase'];
  39.         if ($this->_application{
  40.             $this->_root $this->_root.DS.$this->_application;
  41.         }
  42.         $this->_hash    $config->getValue('config.secret');
  43.     }
  44.  
  45.     /**
  46.      * Get cached data from a file by id and group
  47.      *
  48.      * @access    public
  49.      * @param    string    $id            The cache data id
  50.      * @param    string    $group        The cache data group
  51.      * @param    boolean    $checkTime    True to verify cache time expiration threshold
  52.      * @return    mixed    Boolean false on failure or a cached data string
  53.      * @since    1.5
  54.      */
  55.     function get($id$group$checkTime)
  56.     {
  57.         $data false;
  58.  
  59.         $path $this->_getFilePath($id$group);
  60.         clearstatcache();
  61.         if (is_file($path)) {
  62.             if ($checkTime{
  63.                 if (filemtime($path$this->_threshold{
  64.                     $data file_get_contents($path);
  65.                 }
  66.             else {
  67.                 $data file_get_contents($path);
  68.             }
  69.         }
  70.         return $data;
  71.     }
  72.  
  73.     /**
  74.      * Store the data to a file by id and group
  75.      *
  76.      * @access    public
  77.      * @param    string    $id        The cache data id
  78.      * @param    string    $group    The cache data group
  79.      * @param    string    $data    The data to store in cache
  80.      * @return    boolean    True on success, false otherwise
  81.      * @since    1.5
  82.      */
  83.     function store($id$group$data)
  84.     {
  85.         $written    false;
  86.         $path        $this->_getFilePath($id$group);
  87.  
  88.         $fp @fopen($path"wb");
  89.         if ($fp{
  90.             if ($this->_locking{
  91.                 @flock($fpLOCK_EX);
  92.             }
  93.             $len strlen($data);
  94.             @fwrite($fp$data$len);
  95.             if ($this->_locking{
  96.                 @flock($fpLOCK_UN);
  97.             }
  98.             @fclose($fp);
  99.             $written true;
  100.         }
  101.         // Data integrity check
  102.         if ($written && ($data == file_get_contents($path))) {
  103.             return true;
  104.         else {
  105.             return false;
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * Remove a cached data file by id and group
  111.      *
  112.      * @access    public
  113.      * @param    string    $id        The cache data id
  114.      * @param    string    $group    The cache data group
  115.      * @return    boolean    True on success, false otherwise
  116.      * @since    1.5
  117.      */
  118.     function remove($id$group)
  119.     {
  120.         $path $this->_getFilePath($id$group);
  121.         if (!@unlink($path)) {
  122.             return false;
  123.         }
  124.         return true;
  125.     }
  126.  
  127.     /**
  128.      * Clean cache for a group given a mode.
  129.      *
  130.      * group mode        : cleans all cache in the group
  131.      * notgroup mode    : cleans all cache not in the group
  132.      *
  133.      * @access    public
  134.      * @param    string    $group    The cache data group
  135.      * @param    string    $mode    The mode for cleaning cache [group|notgroup]
  136.      * @return    boolean    True on success, false otherwise
  137.      * @since    1.5
  138.      */
  139.     function clean($group$mode)
  140.     {
  141.         jimport('joomla.filesystem.folder');
  142.  
  143.         $return true;
  144.         $folder    md5($group.'-'.$this->_hash);
  145.         switch ($mode)
  146.         {
  147.             case 'notgroup':
  148.                 $folders JFolder::folders($this->_root);
  149.                 for ($i=0,$n=count($folders);$i<$n;$i++)
  150.                 {
  151.                     if ($folders[$i!= $folder{
  152.                         $return |= JFolder::delete($this->_root.DS.$folders[$i]);
  153.                     }
  154.                 }
  155.                 break;
  156.             case 'group':
  157.             default:
  158.                 if (is_dir($this->_root.DS.$folder)) {
  159.                     $return JFolder::delete($this->_root.DS.$folder);
  160.                 }
  161.                 break;
  162.         }
  163.         return $return;
  164.     }
  165.  
  166.     /**
  167.      * Garbage collect expired cache data
  168.      *
  169.      * @access public
  170.      * @return boolean  True on success, false otherwise.
  171.      */
  172.     function gc()
  173.     {
  174.         $result true;
  175.         // files older than lifeTime get deleted from cache
  176.         if (!is_null($this->_lifeTime)) {
  177.             $files JFolder::files($this->_root'.'truetrue);
  178.             for ($i=0,$n=count($files);$i<$n;$i++)
  179.             {
  180.                 if (filemtime($files[$i]$this->_threshold{
  181.                     $result |= JFile::delete($files[$i]);
  182.                 }
  183.             }
  184.         }
  185.         return $result;
  186.     }
  187.  
  188.     /**
  189.      * Test to see if the cache storage is available.
  190.      *
  191.      * @static
  192.      * @access public
  193.      * @return boolean  True on success, false otherwise.
  194.      */
  195.     function test()
  196.     {
  197.         $config    =JFactory::getConfig();
  198.         $root    $config->getValue('config.cache_path'JPATH_ROOT.DS.'cache');
  199.         return is_writable($root);
  200.     }
  201.  
  202.     /**
  203.      * Get a cache file path from an id/group pair
  204.      *
  205.      * @access    private
  206.      * @param    string    $id        The cache data id
  207.      * @param    string    $group    The cache data group
  208.      * @return    string    The cache file path
  209.      * @since    1.5
  210.      */
  211.     function _getFilePath($id$group)
  212.     {
  213.         $folder    md5($group.'-'.$this->_hash);
  214.         $name    md5($id.'-'.$this->_hash).'.cache';
  215.  
  216.         // If the folder doesn't exist try to create it
  217.         if (!is_dir($this->_root.DS.$folder)) {
  218.             @mkdir($this->_root.DS.$folder);
  219.         }
  220.  
  221.         // Make sure the folder exists
  222.         if (!is_dir($this->_root.DS.$folder)) {
  223.             return false;
  224.         }
  225.         return $this->_root.DS.$folder.DS.$name;
  226.     }
  227. }
  228. ?>

Documentation generated on Mon, 05 Mar 2007 20:58:51 +0000 by phpDocumentor 1.3.1