Source for file cachelite.php

Documentation is available at cachelite.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Cache
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Cache lite storage handler
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Cache
  17.  * @see         http://pear.php.net/package/Cache_Lite/
  18.  * @since       11.1
  19.  */
  20. {
  21.     /**
  22.      * Static cache of the Cache_Lite instance
  23.      *
  24.      * @var    object 
  25.      * @since  11.1
  26.      */
  27.     protected static $CacheLiteInstance null;
  28.  
  29.     /**
  30.      * Root path
  31.      *
  32.      * @var    string 
  33.      * @since  11.1
  34.      */
  35.     protected $_root;
  36.  
  37.     /**
  38.      * Constructor
  39.      *
  40.      * @param   array  $options  Optional parameters.
  41.      *
  42.      * @since   11.1
  43.      */
  44.     public function __construct($options array())
  45.     {
  46.         parent::__construct($options);
  47.  
  48.         $this->_root = $options['cachebase'];
  49.  
  50.         $cloptions array(
  51.             'cacheDir' => $this->_root . '/',
  52.             'lifeTime' => $this->_lifetime,
  53.             'fileLocking' => $this->_locking,
  54.             'automaticCleaningFactor' => isset($options['autoclean']$options['autoclean'200,
  55.             'fileNameProtection' => false,
  56.             'hashedDirectoryLevel' => 0,
  57.             'caching' => $options['caching']);
  58.  
  59.         if (self::$CacheLiteInstance === null)
  60.         {
  61.             $this->initCache($cloptions);
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Instantiates the appropriate CacheLite object.
  67.      * Only initializes the engine if it does not already exist.
  68.      * Note this is a protected method
  69.      *
  70.      * @param   array  $cloptions  optional parameters
  71.      *
  72.      * @return  object 
  73.      *
  74.      * @since   11.1
  75.      */
  76.     protected function initCache($cloptions)
  77.     {
  78.         require_once 'Cache/Lite.php';
  79.  
  80.         self::$CacheLiteInstance new Cache_Lite($cloptions);
  81.  
  82.         return self::$CacheLiteInstance;
  83.     }
  84.  
  85.     /**
  86.      * Get cached data from a file by id and group
  87.      *
  88.      * @param   string   $id         The cache data id.
  89.      * @param   string   $group      The cache data group.
  90.      * @param   boolean  $checkTime  True to verify cache time expiration threshold.
  91.      *
  92.      * @return  mixed  Boolean false on failure or a cached data string.
  93.      *
  94.      * @since   11.1
  95.      */
  96.     public function get($id$group$checkTime true)
  97.     {
  98.         self::$CacheLiteInstance->setOption('cacheDir'$this->_root . '/' $group '/');
  99.         $this->_getCacheId($id$group);
  100.         $data self::$CacheLiteInstance->get($this->rawname$group);
  101.  
  102.         return $data;
  103.     }
  104.  
  105.     /**
  106.      * Get all cached data
  107.      *
  108.      * @return  array 
  109.      *
  110.      * @since   11.1
  111.      */
  112.     public function getAll()
  113.     {
  114.         parent::getAll();
  115.  
  116.         $path $this->_root;
  117.         $folders new DirectoryIterator($path);
  118.         $data array();
  119.  
  120.         foreach ($folders as $folder)
  121.         {
  122.             if (!$folder->isDir(|| $folder->isDot())
  123.             {
  124.                 continue;
  125.             }
  126.  
  127.             $foldername $folder->getFilename();
  128.  
  129.             $files new DirectoryIterator($path '/' $foldername);
  130.             $item  new JCacheStorageHelper($foldername);
  131.  
  132.             foreach ($files as $file)
  133.             {
  134.                 if (!$file->isFile())
  135.                 {
  136.                     continue;
  137.                 }
  138.  
  139.                 $filename $file->getFilename();
  140.  
  141.                 $item->updateSize(filesize($path '/' $foldername '/' $filename1024);
  142.             }
  143.  
  144.             $data[$foldername$item;
  145.         }
  146.  
  147.         return $data;
  148.     }
  149.  
  150.     /**
  151.      * Store the data to a file by id and group
  152.      *
  153.      * @param   string  $id     The cache data id.
  154.      * @param   string  $group  The cache data group.
  155.      * @param   string  $data   The data to store in cache.
  156.      *
  157.      * @return  boolean  True on success, false otherwise
  158.      *
  159.      * @since   11.1
  160.      */
  161.     public function store($id$group$data)
  162.     {
  163.         $dir $this->_root . '/' $group;
  164.  
  165.         // If the folder doesn't exist try to create it
  166.         if (!is_dir($dir))
  167.         {
  168.             // Make sure the index file is there
  169.             $indexFile $dir '/index.html';
  170.             @mkdir($dir&& file_put_contents($indexFile'<!DOCTYPE html><title></title>');
  171.         }
  172.  
  173.         // Make sure the folder exists
  174.         if (!is_dir($dir))
  175.         {
  176.             return false;
  177.         }
  178.  
  179.         self::$CacheLiteInstance->setOption('cacheDir'$this->_root . '/' $group '/');
  180.         $this->_getCacheId($id$group);
  181.         $success self::$CacheLiteInstance->save($data$this->rawname$group);
  182.  
  183.         if ($success == true)
  184.         {
  185.             return $success;
  186.         }
  187.         else
  188.         {
  189.             return false;
  190.         }
  191.     }
  192.  
  193.     /**
  194.      * Remove a cached data file by id and group
  195.      *
  196.      * @param   string  $id     The cache data id
  197.      * @param   string  $group  The cache data group
  198.      *
  199.      * @return  boolean  True on success, false otherwise
  200.      *
  201.      * @since   11.1
  202.      */
  203.     public function remove($id$group)
  204.     {
  205.         self::$CacheLiteInstance->setOption('cacheDir'$this->_root . '/' $group '/');
  206.         $this->_getCacheId($id$group);
  207.         $success self::$CacheLiteInstance->remove($this->rawname$group);
  208.  
  209.         if ($success == true)
  210.         {
  211.             return $success;
  212.         }
  213.         else
  214.         {
  215.             return false;
  216.         }
  217.     }
  218.  
  219.     /**
  220.      * Clean cache for a group given a mode.
  221.      *
  222.      * @param   string  $group  The cache data group.
  223.      * @param   string  $mode   The mode for cleaning cache [group|notgroup].
  224.      *  group mode    : cleans all cache in the group
  225.      *  notgroup mode : cleans all cache not in the group
  226.      *
  227.      * @return  boolean  True on success, false otherwise.
  228.      *
  229.      * @since   11.1
  230.      */
  231.     public function clean($group$mode null)
  232.     {
  233.         jimport('joomla.filesystem.folder');
  234.  
  235.         switch ($mode)
  236.         {
  237.             case 'notgroup':
  238.                 $clmode 'notingroup';
  239.                 $success self::$CacheLiteInstance->clean($group$clmode);
  240.                 break;
  241.  
  242.             case 'group':
  243.                 if (is_dir($this->_root . '/' $group))
  244.                 {
  245.                     $clmode $group;
  246.                     self::$CacheLiteInstance->setOption('cacheDir'$this->_root . '/' $group '/');
  247.                     $success self::$CacheLiteInstance->clean($group$clmode);
  248.                     JFolder::delete($this->_root . '/' $group);
  249.                 }
  250.                 else
  251.                 {
  252.                     $success true;
  253.                 }
  254.  
  255.                 break;
  256.  
  257.             default:
  258.                 if (is_dir($this->_root . '/' $group))
  259.                 {
  260.                     $clmode $group;
  261.                     self::$CacheLiteInstance->setOption('cacheDir'$this->_root . '/' $group '/');
  262.                     $success self::$CacheLiteInstance->clean($group$clmode);
  263.                 }
  264.                 else
  265.                 {
  266.                     $success true;
  267.                 }
  268.  
  269.                 break;
  270.         }
  271.  
  272.         if ($success == true)
  273.         {
  274.             return $success;
  275.         }
  276.         else
  277.         {
  278.             return false;
  279.         }
  280.     }
  281.  
  282.     /**
  283.      * Garbage collect expired cache data
  284.      *
  285.      * @return  boolean  True on success, false otherwise.
  286.      *
  287.      * @since   11.1
  288.      */
  289.     public function gc()
  290.     {
  291.         $result true;
  292.         self::$CacheLiteInstance->setOption('automaticCleaningFactor'1);
  293.         self::$CacheLiteInstance->setOption('hashedDirectoryLevel'1);
  294.         $success1 self::$CacheLiteInstance->_cleanDir($this->_root . '/'false'old');
  295.  
  296.         if (!($dh opendir($this->_root . '/')))
  297.         {
  298.             return false;
  299.         }
  300.  
  301.         while ($file readdir($dh))
  302.         {
  303.             if (($file != '.'&& ($file != '..'&& ($file != '.svn'))
  304.             {
  305.                 $file2 $this->_root . '/' $file;
  306.  
  307.                 if (is_dir($file2))
  308.                 {
  309.                     $result ($result && (self::$CacheLiteInstance->_cleanDir($file2 '/'false'old')));
  310.                 }
  311.             }
  312.         }
  313.  
  314.         $success ($success1 && $result);
  315.  
  316.         return $success;
  317.     }
  318.  
  319.     /**
  320.      * Test to see if the cache storage is available.
  321.      *
  322.      * @return  boolean  True on success, false otherwise.
  323.      *
  324.      * @since   12.1
  325.      */
  326.     public static function isSupported()
  327.     {
  328.         @include_once 'Cache/Lite.php';
  329.  
  330.         if (class_exists('Cache_Lite'))
  331.         {
  332.             return true;
  333.         }
  334.         else
  335.         {
  336.             return false;
  337.         }
  338.     }
  339. }

Documentation generated on Tue, 19 Nov 2013 14:54:55 +0100 by phpDocumentor 1.4.3