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

Documentation is available at file.php

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

Documentation generated on Tue, 29 Jan 2008 18:45:49 +0000 by phpDocumentor 1.3.1