Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Cache_Lite

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 /pear/cache/Lite.php

Documentation is available at Lite.php

  1. <?php
  2.  
  3. /**
  4. * Fast, light and safe Cache Class
  5. *
  6. * Cache_Lite is a fast, light and safe cache system. It's optimized
  7. * for file containers. It is fast and safe (because it uses file
  8. * locking and/or anti-corruption tests).
  9. *
  10. * There are some examples in the 'docs/examples' file
  11. * Technical choices are described in the 'docs/technical' file
  12. *
  13. * Memory Caching is from an original idea of
  14. * Mike BENOIT <[email protected]>
  15. *
  16. * Nota : A chinese documentation (thanks to RainX <[email protected]>) is
  17. * available at :
  18. * http://rainx.phpmore.com/manual/cache_lite.html
  19. *
  20. @package Cache_Lite
  21. @category Caching
  22. @version $Id: Lite.php,v 1.45 2006/06/03 08:10:33 fab Exp $
  23. @author Fabien MARTY <[email protected]>
  24. */
  25.  
  26. define('CACHE_LITE_ERROR_RETURN'1);
  27. define('CACHE_LITE_ERROR_DIE'8);
  28.  
  29. class Cache_Lite
  30. {
  31.  
  32.     // --- Private properties ---
  33.  
  34.     
  35.     /**
  36.     * Directory where to put the cache files
  37.     * (make sure to add a trailing slash)
  38.     *
  39.     * @var string $_cacheDir 
  40.     */
  41.     var $_cacheDir = '/tmp/';
  42.  
  43.     /**
  44.     * Enable / disable caching
  45.     *
  46.     * (can be very usefull for the debug of cached scripts)
  47.     *
  48.     * @var boolean $_caching 
  49.     */
  50.     var $_caching = true;
  51.  
  52.     /**
  53.     * Cache lifetime (in seconds)
  54.     *
  55.     * If null, the cache is valid forever.
  56.     *
  57.     * @var int $_lifeTime 
  58.     */
  59.     var $_lifeTime = 3600;
  60.  
  61.     /**
  62.     * Enable / disable fileLocking
  63.     *
  64.     * (can avoid cache corruption under bad circumstances)
  65.     *
  66.     * @var boolean $_fileLocking 
  67.     */
  68.     var $_fileLocking = true;
  69.  
  70.     /**
  71.     * Timestamp of the last valid cache
  72.     *
  73.     * @var int $_refreshTime 
  74.     */
  75.     var $_refreshTime;
  76.  
  77.     /**
  78.     * File name (with path)
  79.     *
  80.     * @var string $_file 
  81.     */
  82.     var $_file;
  83.  
  84.     /**
  85.     * File name (without path)
  86.     *
  87.     * @var string $_fileName 
  88.     */
  89.     var $_fileName;
  90.  
  91.     /**
  92.     * Enable / disable write control (the cache is read just after writing to detect corrupt entries)
  93.     *
  94.     * Enable write control will lightly slow the cache writing but not the cache reading
  95.     * Write control can detect some corrupt cache files but maybe it's not a perfect control
  96.     *
  97.     * @var boolean $_writeControl 
  98.     */
  99.     var $_writeControl = true;
  100.  
  101.     /**
  102.     * Enable / disable read control
  103.     *
  104.     * If enabled, a control key is embeded in cache file and this key is compared with the one
  105.     * calculated after the reading.
  106.     *
  107.     * @var boolean $_writeControl 
  108.     */
  109.     var $_readControl = true;
  110.  
  111.     /**
  112.     * Type of read control (only if read control is enabled)
  113.     *
  114.     * Available values are :
  115.     * 'md5' for a md5 hash control (best but slowest)
  116.     * 'crc32' for a crc32 hash control (lightly less safe but faster, better choice)
  117.     * 'strlen' for a length only test (fastest)
  118.     *
  119.     * @var boolean $_readControlType 
  120.     */
  121.     var $_readControlType = 'crc32';
  122.  
  123.     /**
  124.     * Pear error mode (when raiseError is called)
  125.     *
  126.     * (see PEAR doc)
  127.     *
  128.     * @see setToDebug()
  129.     * @var int $_pearErrorMode 
  130.     */
  131.     var $_pearErrorMode = CACHE_LITE_ERROR_RETURN;
  132.  
  133.     /**
  134.     * Current cache id
  135.     *
  136.     * @var string $_id 
  137.     */
  138.     var $_id;
  139.  
  140.     /**
  141.     * Current cache group
  142.     *
  143.     * @var string $_group 
  144.     */
  145.     var $_group;
  146.  
  147.     /**
  148.     * Enable / Disable "Memory Caching"
  149.     *
  150.     * NB : There is no lifetime for memory caching !
  151.     *
  152.     * @var boolean $_memoryCaching 
  153.     */
  154.     var $_memoryCaching = false;
  155.  
  156.     /**
  157.     * Enable / Disable "Only Memory Caching"
  158.     * (be carefull, memory caching is "beta quality")
  159.     *
  160.     * @var boolean $_onlyMemoryCaching 
  161.     */
  162.     var $_onlyMemoryCaching = false;
  163.  
  164.     /**
  165.     * Memory caching array
  166.     *
  167.     * @var array $_memoryCachingArray 
  168.     */
  169.     var $_memoryCachingArray = array();
  170.  
  171.     /**
  172.     * Memory caching counter
  173.     *
  174.     * @var int $memoryCachingCounter 
  175.     */
  176.     var $_memoryCachingCounter = 0;
  177.  
  178.     /**
  179.     * Memory caching limit
  180.     *
  181.     * @var int $memoryCachingLimit 
  182.     */
  183.     var $_memoryCachingLimit = 1000;
  184.  
  185.     /**
  186.     * File Name protection
  187.     *
  188.     * if set to true, you can use any cache id or group name
  189.     * if set to false, it can be faster but cache ids and group names
  190.     * will be used directly in cache file names so be carefull with
  191.     * special characters...
  192.     *
  193.     * @var boolean $fileNameProtection 
  194.     */
  195.     var $_fileNameProtection = true;
  196.  
  197.     /**
  198.     * Enable / disable automatic serialization
  199.     *
  200.     * it can be used to save directly datas which aren't strings
  201.     * (but it's slower)
  202.     *
  203.     * @var boolean $_serialize 
  204.     */
  205.     var $_automaticSerialization = false;
  206.  
  207.     /**
  208.     * Disable / Tune the automatic cleaning process
  209.     *
  210.     * The automatic cleaning process destroy too old (for the given life time)
  211.     * cache files when a new cache file is written.
  212.     * 0               => no automatic cache cleaning
  213.     * 1               => systematic cache cleaning
  214.     * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
  215.     *
  216.     * @var int $_automaticCleaning 
  217.     */
  218.     var $_automaticCleaningFactor = 0;
  219.  
  220.     /**
  221.     * Nested directory level
  222.     *
  223.     * Set the hashed directory structure level. 0 means "no hashed directory
  224.     * structure", 1 means "one level of directory", 2 means "two levels"...
  225.     * This option can speed up Cache_Lite only when you have many thousands of
  226.     * cache file. Only specific benchs can help you to choose the perfect value
  227.     * for you. Maybe, 1 or 2 is a good start.
  228.     *
  229.     * @var int $_hashedDirectoryLevel 
  230.     */
  231.     var $_hashedDirectoryLevel = 0;
  232.  
  233.     /**
  234.     * Umask for hashed directory structure
  235.     *
  236.     * @var int $_hashedDirectoryUmask 
  237.     */
  238.     var $_hashedDirectoryUmask = 0700;
  239.  
  240.     /**
  241.      * API break for error handling in CACHE_LITE_ERROR_RETURN mode
  242.      *
  243.      * In CACHE_LITE_ERROR_RETURN mode, error handling was not good because
  244.      * for example save() method always returned a boolean (a PEAR_Error object
  245.      * would be better in CACHE_LITE_ERROR_RETURN mode). To correct this without
  246.      * breaking the API, this option (false by default) can change this handling.
  247.      *
  248.      * @var boolean 
  249.      */
  250.     var $_errorHandlingAPIBreak = false;
  251.  
  252.     // --- Public methods ---
  253.  
  254.     
  255.     /**
  256.     * Constructor
  257.     *
  258.     * $options is an assoc. Available options are :
  259.     * $options = array(
  260.     *     'cacheDir' => directory where to put the cache files (string),
  261.     *     'caching' => enable / disable caching (boolean),
  262.     *     'lifeTime' => cache lifetime in seconds (int),
  263.     *     'fileLocking' => enable / disable fileLocking (boolean),
  264.     *     'writeControl' => enable / disable write control (boolean),
  265.     *     'readControl' => enable / disable read control (boolean),
  266.     *     'readControlType' => type of read control 'crc32', 'md5', 'strlen' (string),
  267.     *     'pearErrorMode' => pear error mode (when raiseError is called) (cf PEAR doc) (int),
  268.     *     'memoryCaching' => enable / disable memory caching (boolean),
  269.     *     'onlyMemoryCaching' => enable / disable only memory caching (boolean),
  270.     *     'memoryCachingLimit' => max nbr of records to store into memory caching (int),
  271.     *     'fileNameProtection' => enable / disable automatic file name protection (boolean),
  272.     *     'automaticSerialization' => enable / disable automatic serialization (boolean),
  273.     *     'automaticCleaningFactor' => distable / tune automatic cleaning process (int),
  274.     *     'hashedDirectoryLevel' => level of the hashed directory system (int),
  275.     *     'hashedDirectoryUmask' => umask for hashed directory structure (int),
  276.     *     'errorHandlingAPIBreak' => API break for better error handling ? (boolean)
  277.     * );
  278.     *
  279.     * @param array $options options
  280.     * @access public
  281.     */
  282.     function Cache_Lite($options array(NULL))
  283.     {
  284.         foreach($options as $key => $value{
  285.             $this->setOption($key$value);
  286.         }
  287.     }
  288.  
  289.     /**
  290.     * Generic way to set a Cache_Lite option
  291.     *
  292.     * see Cache_Lite constructor for available options
  293.     *
  294.     * @var string $name name of the option
  295.     * @var mixed $value value of the option
  296.     * @access public
  297.     */
  298.     function setOption($name$value)
  299.     {
  300.         $availableOptions array('errorHandlingAPIBreak''hashedDirectoryUmask''hashedDirectoryLevel''automaticCleaningFactor''automaticSerialization''fileNameProtection''memoryCaching''onlyMemoryCaching''memoryCachingLimit''cacheDir''caching''lifeTime''fileLocking''writeControl''readControl''readControlType''pearErrorMode');
  301.         if (in_array($name$availableOptions)) {
  302.             $property '_'.$name;
  303.             $this->$property $value;
  304.         }
  305.     }
  306.  
  307.     /**
  308.     * Test if a cache is available and (if yes) return it
  309.     *
  310.     * @param string $id cache id
  311.     * @param string $group name of the cache group
  312.     * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  313.     * @return string data of the cache (else : false)
  314.     * @access public
  315.     */
  316.     function get($id$group 'default'$doNotTestCacheValidity false)
  317.     {
  318.         $this->_id = $id;
  319.         $this->_group = $group;
  320.         $data false;
  321.         if ($this->_caching{
  322.             $this->_setRefreshTime();
  323.             $this->_setFileName($id$group);
  324.             clearstatcache();
  325.             if ($this->_memoryCaching{
  326.                 if (isset($this->_memoryCachingArray[$this->_file])) {
  327.                     if ($this->_automaticSerialization{
  328.                         return unserialize($this->_memoryCachingArray[$this->_file]);
  329.                     }
  330.                     return $this->_memoryCachingArray[$this->_file];
  331.                 }
  332.                 if ($this->_onlyMemoryCaching{
  333.                     return false;
  334.                 }
  335.             }
  336.             if (($doNotTestCacheValidity|| (is_null($this->_refreshTime))) {
  337.                 if (file_exists($this->_file)) {
  338.                     $data $this->_read();
  339.                 }
  340.             else {
  341.                 if ((file_exists($this->_file)) && (@filemtime($this->_file$this->_refreshTime)) {
  342.                     $data $this->_read();
  343.                 }
  344.             }
  345.             if (($dataand ($this->_memoryCaching)) {
  346.                 $this->_memoryCacheAdd($data);
  347.             }
  348.             if (($this->_automaticSerializationand (is_string($data))) {
  349.                 $data unserialize($data);
  350.             }
  351.             return $data;
  352.         }
  353.         return false;
  354.     }
  355.  
  356.     /**
  357.     * Save some data in a cache file
  358.     *
  359.     * @param string $data data to put in cache (can be another type than strings if automaticSerialization is on)
  360.     * @param string $id cache id
  361.     * @param string $group name of the cache group
  362.     * @return boolean true if no problem (else : false or a PEAR_Error object)
  363.     * @access public
  364.     */
  365.     function save($data$id NULL$group 'default')
  366.     {
  367.         if ($this->_caching{
  368.             if ($this->_automaticSerialization{
  369.                 $data serialize($data);
  370.             }
  371.             if (isset($id)) {
  372.                 $this->_setFileName($id$group);
  373.             }
  374.             if ($this->_memoryCaching{
  375.                 $this->_memoryCacheAdd($data);
  376.                 if ($this->_onlyMemoryCaching{
  377.                     return true;
  378.                 }
  379.             }
  380.             if ($this->_automaticCleaningFactor>0{
  381.                 $rand rand(1$this->_automaticCleaningFactor);
  382.                 if ($rand==1{
  383.                     $this->clean(false'old');
  384.                 }
  385.             }
  386.             if ($this->_writeControl{
  387.                 $res $this->_writeAndControl($data);
  388.                 if (is_bool($res)) {
  389.                     if ($res{
  390.                         return true;
  391.                     }
  392.                     // if $res if false, we need to invalidate the cache
  393.                     @touch($this->_filetime(2*abs($this->_lifeTime));
  394.                     return false;
  395.                 }
  396.             else {
  397.                 $res $this->_write($data);
  398.             }
  399.             if (is_object($res)) {
  400.                 // $res is a PEAR_Error object
  401.                 if (!($this->_errorHandlingAPIBreak)) {
  402.                     return false// we return false (old API)
  403.                 }
  404.             }
  405.             return $res;
  406.         }
  407.         return false;
  408.     }
  409.  
  410.     /**
  411.     * Remove a cache file
  412.     *
  413.     * @param string $id cache id
  414.     * @param string $group name of the cache group
  415.     * @return boolean true if no problem
  416.     * @access public
  417.     */
  418.     function remove($id$group 'default')
  419.     {
  420.         $this->_setFileName($id$group);
  421.         if ($this->_memoryCaching{
  422.             if (isset($this->_memoryCachingArray[$this->_file])) {
  423.                 unset($this->_memoryCachingArray[$this->_file]);
  424.                 $this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
  425.             }
  426.             if ($this->_onlyMemoryCaching{
  427.                 return true;
  428.             }
  429.         }
  430.         return $this->_unlink($this->_file);
  431.     }
  432.  
  433.     /**
  434.     * Clean the cache
  435.     *
  436.     * if no group is specified all cache files will be destroyed
  437.     * else only cache files of the specified group will be destroyed
  438.     *
  439.     * @param string $group name of the cache group
  440.     * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup',
  441.     *                                         'callback_myFunction'
  442.     * @return boolean true if no problem
  443.     * @access public
  444.     */
  445.     function clean($group false$mode 'ingroup')
  446.     {
  447.         return $this->_cleanDir($this->_cacheDir$group$mode);
  448.     }
  449.  
  450.     /**
  451.     * Set to debug mode
  452.     *
  453.     * When an error is found, the script will stop and the message will be displayed
  454.     * (in debug mode only).
  455.     *
  456.     * @access public
  457.     */
  458.     function setToDebug()
  459.     {
  460.         $this->setOption('pearErrorMode'CACHE_LITE_ERROR_DIE);
  461.     }
  462.  
  463.     /**
  464.     * Set a new life time
  465.     *
  466.     * @param int $newLifeTime new life time (in seconds)
  467.     * @access public
  468.     */
  469.     function setLifeTime($newLifeTime)
  470.     {
  471.         $this->_lifeTime = $newLifeTime;
  472.         $this->_setRefreshTime();
  473.     }
  474.  
  475.     /**
  476.     * Save the state of the caching memory array into a cache file cache
  477.     *
  478.     * @param string $id cache id
  479.     * @param string $group name of the cache group
  480.     * @access public
  481.     */
  482.     function saveMemoryCachingState($id$group 'default')
  483.     {
  484.         if ($this->_caching{
  485.             $array array(
  486.                 'counter' => $this->_memoryCachingCounter,
  487.                 'array' => $this->_memoryCachingState
  488.             );
  489.             $data serialize($array);
  490.             $this->save($data$id$group);
  491.         }
  492.     }
  493.  
  494.     /**
  495.     * Load the state of the caching memory array from a given cache file cache
  496.     *
  497.     * @param string $id cache id
  498.     * @param string $group name of the cache group
  499.     * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  500.     * @access public
  501.     */
  502.     function getMemoryCachingState($id$group 'default'$doNotTestCacheValidity false)
  503.     {
  504.         if ($this->_caching{
  505.             if ($data $this->get($id$group$doNotTestCacheValidity)) {
  506.                 $array unserialize($data);
  507.                 $this->_memoryCachingCounter = $array['counter'];
  508.                 $this->_memoryCachingArray = $array['array'];
  509.             }
  510.         }
  511.     }
  512.  
  513.     /**
  514.     * Return the cache last modification time
  515.     *
  516.     * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY !
  517.     *
  518.     * @return int last modification time
  519.     */
  520.     function lastModified()
  521.     {
  522.         return @filemtime($this->_file);
  523.     }
  524.  
  525.     /**
  526.     * Trigger a PEAR error
  527.     *
  528.     * To improve performances, the PEAR.php file is included dynamically.
  529.     * The file is so included only when an error is triggered. So, in most
  530.     * cases, the file isn't included and perfs are much better.
  531.     *
  532.     * @param string $msg error message
  533.     * @param int $code error code
  534.     * @access public
  535.     */
  536.     function raiseError($msg$code)
  537.     {
  538.         jimport('pear.PEAR');
  539.         return PEAR::raiseError($msg$code$this->_pearErrorMode);
  540.     }
  541.  
  542.     /**
  543.      * Extend the life of a valid cache file
  544.      *
  545.      * see http://pear.php.net/bugs/bug.php?id=6681
  546.      *
  547.      * @access public
  548.      */
  549.     function extendLife()
  550.     {
  551.         @touch($this->_file);
  552.     }
  553.  
  554.     // --- Private methods ---
  555.  
  556.     
  557.     /**
  558.     * Compute & set the refresh time
  559.     *
  560.     * @access private
  561.     */
  562.     function _setRefreshTime()
  563.     {
  564.         if (is_null($this->_lifeTime)) {
  565.             $this->_refreshTime = null;
  566.         else {
  567.             $this->_refreshTime = time($this->_lifeTime;
  568.         }
  569.     }
  570.  
  571.     /**
  572.     * Remove a file
  573.     *
  574.     * @param string $file complete file path and name
  575.     * @return boolean true if no problem
  576.     * @access private
  577.     */
  578.     function _unlink($file)
  579.     {
  580.         if (!@unlink($file)) {
  581.             return $this->raiseError('Cache_Lite : Unable to remove cache !'-3);
  582.         }
  583.         return true;
  584.     }
  585.  
  586.     /**
  587.     * Recursive function for cleaning cache file in the given directory
  588.     *
  589.     * @param string $dir directory complete path (with a trailing slash)
  590.     * @param string $group name of the cache group
  591.     * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup',
  592.                                              'callback_myFunction'
  593.     * @return boolean true if no problem
  594.     * @access private
  595.     */
  596.     function _cleanDir($dir$group false$mode 'ingroup')
  597.     {
  598.         if ($this->_fileNameProtection{
  599.             $motif ($group'cache_'.md5($group).'_' 'cache_';
  600.         else {
  601.             $motif ($group'cache_'.$group.'_' 'cache_';
  602.         }
  603.         if ($this->_memoryCaching{
  604.             while (list($keyeach($this->_memoryCachingArray)) {
  605.                 if (strpos($key$motif0)) {
  606.                     unset($this->_memoryCachingArray[$key]);
  607.                     $this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
  608.                 }
  609.             }
  610.             if ($this->_onlyMemoryCaching{
  611.                 return true;
  612.             }
  613.         }
  614.         if (!($dh opendir($dir))) {
  615.             return $this->raiseError('Cache_Lite : Unable to open cache directory !'-4);
  616.         }
  617.         $result true;
  618.         while ($file readdir($dh)) {
  619.             if (($file != '.'&& ($file != '..')) {
  620.                 if (substr($file06)=='cache_'{
  621.                     $file2 $dir $file;
  622.                     if (is_file($file2)) {
  623.                         switch (substr($mode09)) {
  624.                             case 'old':
  625.                                 // files older than lifeTime get deleted from cache
  626.                                 if (!is_null($this->_lifeTime)) {
  627.                                     if ((mktime(@filemtime($file2)) $this->_lifeTime{
  628.                                         $result ($result and ($this->_unlink($file2)));
  629.                                     }
  630.                                 }
  631.                                 break;
  632.                             case 'notingrou':
  633.                                 if (!strpos($file2$motif0)) {
  634.                                     $result ($result and ($this->_unlink($file2)));
  635.                                 }
  636.                                 break;
  637.                             case 'callback_':
  638.                                 $func substr($mode9strlen($mode9);
  639.                                 if ($func($file2$group)) {
  640.                                     $result ($result and ($this->_unlink($file2)));
  641.                                 }
  642.                                 break;
  643.                             case 'ingroup':
  644.                             default:
  645.                                 if (strpos($file2$motif0)) {
  646.                                     $result ($result and ($this->_unlink($file2)));
  647.                                 }
  648.                                 break;
  649.                         }
  650.                     }
  651.                     if ((is_dir($file2)) and ($this->_hashedDirectoryLevel>0)) {
  652.                         $result ($result and ($this->_cleanDir($file2 '/'$group$mode)));
  653.                     }
  654.                 }
  655.             }
  656.         }
  657.         return $result;
  658.     }
  659.  
  660.     /**
  661.     * Add some date in the memory caching array
  662.     *
  663.     * @param string $data data to cache
  664.     * @access private
  665.     */
  666.     function _memoryCacheAdd($data)
  667.     {
  668.         $this->_memoryCachingArray[$this->_file$data;
  669.         if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit{
  670.             list($keyeach($this->_memoryCachingArray);
  671.             unset($this->_memoryCachingArray[$key]);
  672.         else {
  673.             $this->_memoryCachingCounter = $this->_memoryCachingCounter + 1;
  674.         }
  675.     }
  676.  
  677.     /**
  678.     * Make a file name (with path)
  679.     *
  680.     * @param string $id cache id
  681.     * @param string $group name of the group
  682.     * @access private
  683.     */
  684.     function _setFileName($id$group)
  685.     {
  686.  
  687.         if ($this->_fileNameProtection{
  688.             $suffix 'cache_'.md5($group).'_'.md5($id);
  689.         else {
  690.             $suffix 'cache_'.$group.'_'.$id;
  691.         }
  692.         $root $this->_cacheDir;
  693.         if ($this->_hashedDirectoryLevel>0{
  694.             $hash md5($suffix);
  695.             for ($i=$i<$this->_hashedDirectoryLevel $i++{
  696.                 $root $root 'cache_' substr($hash0$i 1'/';
  697.             }
  698.         }
  699.         $this->_fileName = $suffix;
  700.         $this->_file = $root.$suffix;
  701.     }
  702.  
  703.     /**
  704.     * Read the cache file and return the content
  705.     *
  706.     * @return string content of the cache file (else : false or a PEAR_Error object)
  707.     * @access private
  708.     */
  709.     function _read()
  710.     {
  711.         $fp @fopen($this->_file"rb");
  712.         if ($this->_fileLocking@flock($fpLOCK_SH);
  713.         if ($fp{
  714.             clearstatcache();
  715.             $length @filesize($this->_file);
  716.             $mqr get_magic_quotes_runtime();
  717.             set_magic_quotes_runtime(0);
  718.             if ($this->_readControl{
  719.                 $hashControl @fread($fp32);
  720.                 $length $length 32;
  721.             }
  722.             if ($length{
  723.                 $data @fread($fp$length);
  724.             else {
  725.                 $data '';
  726.             }
  727.             set_magic_quotes_runtime($mqr);
  728.             if ($this->_fileLocking@flock($fpLOCK_UN);
  729.             @fclose($fp);
  730.             if ($this->_readControl{
  731.                 $hashData $this->_hash($data$this->_readControlType);
  732.                 if ($hashData != $hashControl{
  733.                     if (!(is_null($this->_lifeTime))) {
  734.                         @touch($this->_filetime(2*abs($this->_lifeTime));
  735.                     else {
  736.                         @unlink($this->_file);
  737.                     }
  738.                     return false;
  739.                 }
  740.             }
  741.             return $data;
  742.         }
  743.         return $this->raiseError('Cache_Lite : Unable to read cache !'-2);
  744.     }
  745.  
  746.     /**
  747.     * Write the given data in the cache file
  748.     *
  749.     * @param string $data data to put in cache
  750.     * @return boolean true if ok (a PEAR_Error object else)
  751.     * @access private
  752.     */
  753.     function _write($data)
  754.     {
  755.         if ($this->_hashedDirectoryLevel > 0{
  756.             $hash md5($this->_fileName);
  757.             $root $this->_cacheDir;
  758.             for ($i=$i<$this->_hashedDirectoryLevel $i++{
  759.                 $root $root 'cache_' substr($hash0$i 1'/';
  760.                 if (!(@is_dir($root))) {
  761.                     @mkdir($root$this->_hashedDirectoryUmask);
  762.                 }
  763.             }
  764.         }
  765.         $fp @fopen($this->_file"wb");
  766.         if ($fp{
  767.             if ($this->_fileLocking@flock($fpLOCK_EX);
  768.             if ($this->_readControl{
  769.                 @fwrite($fp$this->_hash($data$this->_readControlType)32);
  770.             }
  771.             $len strlen($data);
  772.             @fwrite($fp$data$len);
  773.             if ($this->_fileLocking@flock($fpLOCK_UN);
  774.             @fclose($fp);
  775.             return true;
  776.         }
  777.         return $this->raiseError('Cache_Lite : Unable to write cache file : '.$this->_file-1);
  778.     }
  779.  
  780.     /**
  781.     * Write the given data in the cache file and control it just after to avoir corrupted cache entries
  782.     *
  783.     * @param string $data data to put in cache
  784.     * @return boolean true if the test is ok (else : false or a PEAR_Error object)
  785.     * @access private
  786.     */
  787.     function _writeAndControl($data)
  788.     {
  789.         $result $this->_write($data);
  790.         if (is_object($result)) {
  791.             return $result# We return the PEAR_Error object
  792.         }
  793.         $dataRead $this->_read();
  794.         if (is_object($dataRead)) {
  795.             return $result# We return the PEAR_Error object
  796.         }
  797.         if ((is_bool($dataRead)) && (!$dataRead)) {
  798.             return false;
  799.         }
  800.         return ($dataRead==$data);
  801.     }
  802.  
  803.     /**
  804.     * Make a control key with the string containing datas
  805.     *
  806.     * @param string $data data
  807.     * @param string $controlType type of control 'md5', 'crc32' or 'strlen'
  808.     * @return string control key
  809.     * @access private
  810.     */
  811.     function _hash($data$controlType)
  812.     {
  813.         switch ($controlType{
  814.         case 'md5':
  815.             return md5($data);
  816.         case 'crc32':
  817.             return sprintf('% 32d'crc32($data));
  818.         case 'strlen':
  819.             return sprintf('% 32d'strlen($data));
  820.         default:
  821.             return $this->raiseError('Unknown controlType ! (available values are only \'md5\', \'crc32\', \'strlen\')'-5);
  822.         }
  823.     }
  824.  
  825. }
  826.  
  827. ?>

Documentation generated on Mon, 05 Mar 2007 21:09:50 +0000 by phpDocumentor 1.3.1