* Upgraded to PEAR Cache_Lite 1.7.8
This commit is contained in:
@ -19,7 +19,7 @@
|
|||||||
*
|
*
|
||||||
* @package Cache_Lite
|
* @package Cache_Lite
|
||||||
* @category Caching
|
* @category Caching
|
||||||
* @version $Id: Lite.php,v 1.30 2005/06/13 20:50:48 fab Exp $
|
* @version $Id: Lite.php,v 1.54 2009/07/07 05:34:37 tacker Exp $
|
||||||
* @author Fabien MARTY <fab@php.net>
|
* @author Fabien MARTY <fab@php.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -51,6 +51,8 @@ class Cache_Lite
|
|||||||
/**
|
/**
|
||||||
* Cache lifetime (in seconds)
|
* Cache lifetime (in seconds)
|
||||||
*
|
*
|
||||||
|
* If null, the cache is valid forever.
|
||||||
|
*
|
||||||
* @var int $_lifeTime
|
* @var int $_lifeTime
|
||||||
*/
|
*/
|
||||||
var $_lifeTime = 3600;
|
var $_lifeTime = 3600;
|
||||||
@ -234,6 +236,18 @@ class Cache_Lite
|
|||||||
*/
|
*/
|
||||||
var $_hashedDirectoryUmask = 0700;
|
var $_hashedDirectoryUmask = 0700;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API break for error handling in CACHE_LITE_ERROR_RETURN mode
|
||||||
|
*
|
||||||
|
* In CACHE_LITE_ERROR_RETURN mode, error handling was not good because
|
||||||
|
* for example save() method always returned a boolean (a PEAR_Error object
|
||||||
|
* would be better in CACHE_LITE_ERROR_RETURN mode). To correct this without
|
||||||
|
* breaking the API, this option (false by default) can change this handling.
|
||||||
|
*
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
|
var $_errorHandlingAPIBreak = false;
|
||||||
|
|
||||||
// --- Public methods ---
|
// --- Public methods ---
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -253,10 +267,11 @@ class Cache_Lite
|
|||||||
* 'onlyMemoryCaching' => enable / disable only memory caching (boolean),
|
* 'onlyMemoryCaching' => enable / disable only memory caching (boolean),
|
||||||
* 'memoryCachingLimit' => max nbr of records to store into memory caching (int),
|
* 'memoryCachingLimit' => max nbr of records to store into memory caching (int),
|
||||||
* 'fileNameProtection' => enable / disable automatic file name protection (boolean),
|
* 'fileNameProtection' => enable / disable automatic file name protection (boolean),
|
||||||
* 'automaticSerialization' => enable / disable automatic serialization (boolean)
|
* 'automaticSerialization' => enable / disable automatic serialization (boolean),
|
||||||
* 'automaticCleaningFactor' => distable / tune automatic cleaning process (int)
|
* 'automaticCleaningFactor' => distable / tune automatic cleaning process (int),
|
||||||
* 'hashedDirectoryLevel' => level of the hashed directory system (int)
|
* 'hashedDirectoryLevel' => level of the hashed directory system (int),
|
||||||
* 'hashedDirectoryUmask' => umask for hashed directory structure (int)
|
* 'hashedDirectoryUmask' => umask for hashed directory structure (int),
|
||||||
|
* 'errorHandlingAPIBreak' => API break for better error handling ? (boolean)
|
||||||
* );
|
* );
|
||||||
*
|
*
|
||||||
* @param array $options options
|
* @param array $options options
|
||||||
@ -264,15 +279,28 @@ class Cache_Lite
|
|||||||
*/
|
*/
|
||||||
function Cache_Lite($options = array(NULL))
|
function Cache_Lite($options = array(NULL))
|
||||||
{
|
{
|
||||||
$availableOptions = array('hashedDirectoryUmask', 'hashedDirectoryLevel', 'automaticCleaningFactor', 'automaticSerialization', 'fileNameProtection', 'memoryCaching', 'onlyMemoryCaching', 'memoryCachingLimit', 'cacheDir', 'caching', 'lifeTime', 'fileLocking', 'writeControl', 'readControl', 'readControlType', 'pearErrorMode');
|
|
||||||
foreach($options as $key => $value) {
|
foreach($options as $key => $value) {
|
||||||
if(in_array($key, $availableOptions)) {
|
$this->setOption($key, $value);
|
||||||
$property = '_'.$key;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic way to set a Cache_Lite option
|
||||||
|
*
|
||||||
|
* see Cache_Lite constructor for available options
|
||||||
|
*
|
||||||
|
* @var string $name name of the option
|
||||||
|
* @var mixed $value value of the option
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function setOption($name, $value)
|
||||||
|
{
|
||||||
|
$availableOptions = array('errorHandlingAPIBreak', 'hashedDirectoryUmask', 'hashedDirectoryLevel', 'automaticCleaningFactor', 'automaticSerialization', 'fileNameProtection', 'memoryCaching', 'onlyMemoryCaching', 'memoryCachingLimit', 'cacheDir', 'caching', 'lifeTime', 'fileLocking', 'writeControl', 'readControl', 'readControlType', 'pearErrorMode');
|
||||||
|
if (in_array($name, $availableOptions)) {
|
||||||
|
$property = '_'.$name;
|
||||||
$this->$property = $value;
|
$this->$property = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->_refreshTime = time() - $this->_lifeTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if a cache is available and (if yes) return it
|
* Test if a cache is available and (if yes) return it
|
||||||
@ -280,7 +308,7 @@ class Cache_Lite
|
|||||||
* @param string $id cache id
|
* @param string $id cache id
|
||||||
* @param string $group name of the cache group
|
* @param string $group name of the cache group
|
||||||
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
|
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
|
||||||
* @return string data of the cache (or false if no cache available)
|
* @return string data of the cache (else : false)
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function get($id, $group = 'default', $doNotTestCacheValidity = false)
|
function get($id, $group = 'default', $doNotTestCacheValidity = false)
|
||||||
@ -289,21 +317,21 @@ class Cache_Lite
|
|||||||
$this->_group = $group;
|
$this->_group = $group;
|
||||||
$data = false;
|
$data = false;
|
||||||
if ($this->_caching) {
|
if ($this->_caching) {
|
||||||
|
$this->_setRefreshTime();
|
||||||
$this->_setFileName($id, $group);
|
$this->_setFileName($id, $group);
|
||||||
|
clearstatcache();
|
||||||
if ($this->_memoryCaching) {
|
if ($this->_memoryCaching) {
|
||||||
if (isset($this->_memoryCachingArray[$this->_file])) {
|
if (isset($this->_memoryCachingArray[$this->_file])) {
|
||||||
if ($this->_automaticSerialization) {
|
if ($this->_automaticSerialization) {
|
||||||
return unserialize($this->_memoryCachingArray[$this->_file]);
|
return unserialize($this->_memoryCachingArray[$this->_file]);
|
||||||
} else {
|
}
|
||||||
return $this->_memoryCachingArray[$this->_file];
|
return $this->_memoryCachingArray[$this->_file];
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if ($this->_onlyMemoryCaching) {
|
if ($this->_onlyMemoryCaching) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (($doNotTestCacheValidity) || (is_null($this->_refreshTime))) {
|
||||||
if ($doNotTestCacheValidity) {
|
|
||||||
if (file_exists($this->_file)) {
|
if (file_exists($this->_file)) {
|
||||||
$data = $this->_read();
|
$data = $this->_read();
|
||||||
}
|
}
|
||||||
@ -313,7 +341,7 @@ class Cache_Lite
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (($data) and ($this->_memoryCaching)) {
|
if (($data) and ($this->_memoryCaching)) {
|
||||||
$this->_memoryCacheAdd($this->_file, $data);
|
$this->_memoryCacheAdd($data);
|
||||||
}
|
}
|
||||||
if (($this->_automaticSerialization) and (is_string($data))) {
|
if (($this->_automaticSerialization) and (is_string($data))) {
|
||||||
$data = unserialize($data);
|
$data = unserialize($data);
|
||||||
@ -329,7 +357,7 @@ class Cache_Lite
|
|||||||
* @param string $data data to put in cache (can be another type than strings if automaticSerialization is on)
|
* @param string $data data to put in cache (can be another type than strings if automaticSerialization is on)
|
||||||
* @param string $id cache id
|
* @param string $id cache id
|
||||||
* @param string $group name of the cache group
|
* @param string $group name of the cache group
|
||||||
* @return boolean true if no problem
|
* @return boolean true if no problem (else : false or a PEAR_Error object)
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function save($data, $id = NULL, $group = 'default')
|
function save($data, $id = NULL, $group = 'default')
|
||||||
@ -342,27 +370,34 @@ class Cache_Lite
|
|||||||
$this->_setFileName($id, $group);
|
$this->_setFileName($id, $group);
|
||||||
}
|
}
|
||||||
if ($this->_memoryCaching) {
|
if ($this->_memoryCaching) {
|
||||||
$this->_memoryCacheAdd($this->_file, $data);
|
$this->_memoryCacheAdd($data);
|
||||||
if ($this->_onlyMemoryCaching) {
|
if ($this->_onlyMemoryCaching) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($this->_automaticCleaningFactor>0) {
|
if ($this->_automaticCleaningFactor>0 && ($this->_automaticCleaningFactor==1 || mt_rand(1, $this->_automaticCleaningFactor)==1)) {
|
||||||
$rand = rand(1, $this->_automaticCleaningFactor);
|
|
||||||
if ($rand==1) {
|
|
||||||
$this->clean(false, 'old');
|
$this->clean(false, 'old');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if ($this->_writeControl) {
|
if ($this->_writeControl) {
|
||||||
if (!$this->_writeAndControl($data)) {
|
$res = $this->_writeAndControl($data);
|
||||||
@touch($this->_file, time() - 2*abs($this->_lifeTime));
|
if (is_bool($res)) {
|
||||||
return false;
|
if ($res) {
|
||||||
} else {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
// if $res if false, we need to invalidate the cache
|
||||||
return $this->_write($data);
|
@touch($this->_file, time() - 2*abs($this->_lifeTime));
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$res = $this->_write($data);
|
||||||
|
}
|
||||||
|
if (is_object($res)) {
|
||||||
|
// $res is a PEAR_Error object
|
||||||
|
if (!($this->_errorHandlingAPIBreak)) {
|
||||||
|
return false; // we return false (old API)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -372,10 +407,11 @@ class Cache_Lite
|
|||||||
*
|
*
|
||||||
* @param string $id cache id
|
* @param string $id cache id
|
||||||
* @param string $group name of the cache group
|
* @param string $group name of the cache group
|
||||||
|
* @param boolean $checkbeforeunlink check if file exists before removing it
|
||||||
* @return boolean true if no problem
|
* @return boolean true if no problem
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function remove($id, $group = 'default')
|
function remove($id, $group = 'default', $checkbeforeunlink = false)
|
||||||
{
|
{
|
||||||
$this->_setFileName($id, $group);
|
$this->_setFileName($id, $group);
|
||||||
if ($this->_memoryCaching) {
|
if ($this->_memoryCaching) {
|
||||||
@ -387,6 +423,9 @@ class Cache_Lite
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ( $checkbeforeunlink ) {
|
||||||
|
if (!file_exists($this->_file)) return true;
|
||||||
|
}
|
||||||
return $this->_unlink($this->_file);
|
return $this->_unlink($this->_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -417,7 +456,7 @@ class Cache_Lite
|
|||||||
*/
|
*/
|
||||||
function setToDebug()
|
function setToDebug()
|
||||||
{
|
{
|
||||||
$this->_pearErrorMode = CACHE_LITE_ERROR_DIE;
|
$this->setOption('pearErrorMode', CACHE_LITE_ERROR_DIE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -429,7 +468,7 @@ class Cache_Lite
|
|||||||
function setLifeTime($newLifeTime)
|
function setLifeTime($newLifeTime)
|
||||||
{
|
{
|
||||||
$this->_lifeTime = $newLifeTime;
|
$this->_lifeTime = $newLifeTime;
|
||||||
$this->_refreshTime = time() - $newLifeTime;
|
$this->_setRefreshTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -444,7 +483,7 @@ class Cache_Lite
|
|||||||
if ($this->_caching) {
|
if ($this->_caching) {
|
||||||
$array = array(
|
$array = array(
|
||||||
'counter' => $this->_memoryCachingCounter,
|
'counter' => $this->_memoryCachingCounter,
|
||||||
'array' => $this->_memoryCachingState
|
'array' => $this->_memoryCachingArray
|
||||||
);
|
);
|
||||||
$data = serialize($array);
|
$data = serialize($array);
|
||||||
$this->save($data, $id, $group);
|
$this->save($data, $id, $group);
|
||||||
@ -477,7 +516,8 @@ class Cache_Lite
|
|||||||
*
|
*
|
||||||
* @return int last modification time
|
* @return int last modification time
|
||||||
*/
|
*/
|
||||||
function lastModified() {
|
function lastModified()
|
||||||
|
{
|
||||||
return @filemtime($this->_file);
|
return @filemtime($this->_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -494,12 +534,38 @@ class Cache_Lite
|
|||||||
*/
|
*/
|
||||||
function raiseError($msg, $code)
|
function raiseError($msg, $code)
|
||||||
{
|
{
|
||||||
include_once(dirname(__FILE__) . '/../PEAR.php');
|
include_once dirname(__FILE__) . '/../PEAR.php';
|
||||||
PEAR::raiseError($msg, $code, $this->_pearErrorMode);
|
return PEAR::raiseError($msg, $code, $this->_pearErrorMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend the life of a valid cache file
|
||||||
|
*
|
||||||
|
* see http://pear.php.net/bugs/bug.php?id=6681
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function extendLife()
|
||||||
|
{
|
||||||
|
@touch($this->_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Private methods ---
|
// --- Private methods ---
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute & set the refresh time
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
function _setRefreshTime()
|
||||||
|
{
|
||||||
|
if (is_null($this->_lifeTime)) {
|
||||||
|
$this->_refreshTime = null;
|
||||||
|
} else {
|
||||||
|
$this->_refreshTime = time() - $this->_lifeTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a file
|
* Remove a file
|
||||||
*
|
*
|
||||||
@ -510,11 +576,9 @@ class Cache_Lite
|
|||||||
function _unlink($file)
|
function _unlink($file)
|
||||||
{
|
{
|
||||||
if (!@unlink($file)) {
|
if (!@unlink($file)) {
|
||||||
$this->raiseError('Cache_Lite : Unable to remove cache !', -3);
|
return $this->raiseError('Cache_Lite : Unable to remove cache !', -3);
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -535,8 +599,8 @@ class Cache_Lite
|
|||||||
$motif = ($group) ? 'cache_'.$group.'_' : 'cache_';
|
$motif = ($group) ? 'cache_'.$group.'_' : 'cache_';
|
||||||
}
|
}
|
||||||
if ($this->_memoryCaching) {
|
if ($this->_memoryCaching) {
|
||||||
while (list($key, $value) = each($this->_memoryCachingArray)) {
|
foreach($this->_memoryCachingArray as $key => $v) {
|
||||||
if (strpos($key, $motif, 0)) {
|
if (strpos($key, $motif) !== false) {
|
||||||
unset($this->_memoryCachingArray[$key]);
|
unset($this->_memoryCachingArray[$key]);
|
||||||
$this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
|
$this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
|
||||||
}
|
}
|
||||||
@ -546,8 +610,7 @@ class Cache_Lite
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!($dh = opendir($dir))) {
|
if (!($dh = opendir($dir))) {
|
||||||
$this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
|
return $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
$result = true;
|
$result = true;
|
||||||
while ($file = readdir($dh)) {
|
while ($file = readdir($dh)) {
|
||||||
@ -558,12 +621,14 @@ class Cache_Lite
|
|||||||
switch (substr($mode, 0, 9)) {
|
switch (substr($mode, 0, 9)) {
|
||||||
case 'old':
|
case 'old':
|
||||||
// files older than lifeTime get deleted from cache
|
// files older than lifeTime get deleted from cache
|
||||||
if ((mktime() - filemtime($file2)) > $this->_lifeTime) {
|
if (!is_null($this->_lifeTime)) {
|
||||||
|
if ((time() - @filemtime($file2)) > $this->_lifeTime) {
|
||||||
$result = ($result and ($this->_unlink($file2)));
|
$result = ($result and ($this->_unlink($file2)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'notingrou':
|
case 'notingrou':
|
||||||
if (!strpos($file2, $motif, 0)) {
|
if (strpos($file2, $motif) === false) {
|
||||||
$result = ($result and ($this->_unlink($file2)));
|
$result = ($result and ($this->_unlink($file2)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -575,7 +640,7 @@ class Cache_Lite
|
|||||||
break;
|
break;
|
||||||
case 'ingroup':
|
case 'ingroup':
|
||||||
default:
|
default:
|
||||||
if (strpos($file2, $motif, 0)) {
|
if (strpos($file2, $motif) !== false) {
|
||||||
$result = ($result and ($this->_unlink($file2)));
|
$result = ($result and ($this->_unlink($file2)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -593,15 +658,14 @@ class Cache_Lite
|
|||||||
/**
|
/**
|
||||||
* Add some date in the memory caching array
|
* Add some date in the memory caching array
|
||||||
*
|
*
|
||||||
* @param string $id cache id
|
|
||||||
* @param string $data data to cache
|
* @param string $data data to cache
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _memoryCacheAdd($id, $data)
|
function _memoryCacheAdd($data)
|
||||||
{
|
{
|
||||||
$this->_memoryCachingArray[$this->_file] = $data;
|
$this->_memoryCachingArray[$this->_file] = $data;
|
||||||
if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) {
|
if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) {
|
||||||
list($key, $value) = each($this->_memoryCachingArray);
|
list($key, ) = each($this->_memoryCachingArray);
|
||||||
unset($this->_memoryCachingArray[$key]);
|
unset($this->_memoryCachingArray[$key]);
|
||||||
} else {
|
} else {
|
||||||
$this->_memoryCachingCounter = $this->_memoryCachingCounter + 1;
|
$this->_memoryCachingCounter = $this->_memoryCachingCounter + 1;
|
||||||
@ -637,7 +701,7 @@ class Cache_Lite
|
|||||||
/**
|
/**
|
||||||
* Read the cache file and return the content
|
* Read the cache file and return the content
|
||||||
*
|
*
|
||||||
* @return string content of the cache file
|
* @return string content of the cache file (else : false or a PEAR_Error object)
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _read()
|
function _read()
|
||||||
@ -645,10 +709,12 @@ class Cache_Lite
|
|||||||
$fp = @fopen($this->_file, "rb");
|
$fp = @fopen($this->_file, "rb");
|
||||||
if ($this->_fileLocking) @flock($fp, LOCK_SH);
|
if ($this->_fileLocking) @flock($fp, LOCK_SH);
|
||||||
if ($fp) {
|
if ($fp) {
|
||||||
clearstatcache(); // because the filesize can be cached by PHP itself...
|
clearstatcache();
|
||||||
$length = @filesize($this->_file);
|
$length = @filesize($this->_file);
|
||||||
$mqr = get_magic_quotes_runtime();
|
$mqr = get_magic_quotes_runtime();
|
||||||
|
if ($mqr) {
|
||||||
set_magic_quotes_runtime(0);
|
set_magic_quotes_runtime(0);
|
||||||
|
}
|
||||||
if ($this->_readControl) {
|
if ($this->_readControl) {
|
||||||
$hashControl = @fread($fp, 32);
|
$hashControl = @fread($fp, 32);
|
||||||
$length = $length - 32;
|
$length = $length - 32;
|
||||||
@ -658,73 +724,87 @@ class Cache_Lite
|
|||||||
} else {
|
} else {
|
||||||
$data = '';
|
$data = '';
|
||||||
}
|
}
|
||||||
|
if ($mqr) {
|
||||||
set_magic_quotes_runtime($mqr);
|
set_magic_quotes_runtime($mqr);
|
||||||
|
}
|
||||||
if ($this->_fileLocking) @flock($fp, LOCK_UN);
|
if ($this->_fileLocking) @flock($fp, LOCK_UN);
|
||||||
@fclose($fp);
|
@fclose($fp);
|
||||||
if ($this->_readControl) {
|
if ($this->_readControl) {
|
||||||
$hashData = $this->_hash($data, $this->_readControlType);
|
$hashData = $this->_hash($data, $this->_readControlType);
|
||||||
if ($hashData != $hashControl) {
|
if ($hashData != $hashControl) {
|
||||||
|
if (!(is_null($this->_lifeTime))) {
|
||||||
@touch($this->_file, time() - 2*abs($this->_lifeTime));
|
@touch($this->_file, time() - 2*abs($this->_lifeTime));
|
||||||
|
} else {
|
||||||
|
@unlink($this->_file);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
$this->raiseError('Cache_Lite : Unable to read cache !', -2);
|
return $this->raiseError('Cache_Lite : Unable to read cache !', -2);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the given data in the cache file
|
* Write the given data in the cache file
|
||||||
*
|
*
|
||||||
* @param string $data data to put in cache
|
* @param string $data data to put in cache
|
||||||
* @return boolean true if ok
|
* @return boolean true if ok (a PEAR_Error object else)
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _write($data)
|
function _write($data)
|
||||||
{
|
{
|
||||||
$try = 1;
|
if ($this->_hashedDirectoryLevel > 0) {
|
||||||
while ($try<=2) {
|
$hash = md5($this->_fileName);
|
||||||
|
$root = $this->_cacheDir;
|
||||||
|
for ($i=0 ; $i<$this->_hashedDirectoryLevel ; $i++) {
|
||||||
|
$root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/';
|
||||||
|
if (!(@is_dir($root))) {
|
||||||
|
@mkdir($root, $this->_hashedDirectoryUmask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
$fp = @fopen($this->_file, "wb");
|
$fp = @fopen($this->_file, "wb");
|
||||||
if ($fp) {
|
if ($fp) {
|
||||||
if ($this->_fileLocking) @flock($fp, LOCK_EX);
|
if ($this->_fileLocking) @flock($fp, LOCK_EX);
|
||||||
if ($this->_readControl) {
|
if ($this->_readControl) {
|
||||||
@fwrite($fp, $this->_hash($data, $this->_readControlType), 32);
|
@fwrite($fp, $this->_hash($data, $this->_readControlType), 32);
|
||||||
}
|
}
|
||||||
$len = strlen($data);
|
$mqr = get_magic_quotes_runtime();
|
||||||
@fwrite($fp, $data, $len);
|
if ($mqr) {
|
||||||
|
set_magic_quotes_runtime(0);
|
||||||
|
}
|
||||||
|
@fwrite($fp, $data);
|
||||||
|
if ($mqr) {
|
||||||
|
set_magic_quotes_runtime($mqr);
|
||||||
|
}
|
||||||
if ($this->_fileLocking) @flock($fp, LOCK_UN);
|
if ($this->_fileLocking) @flock($fp, LOCK_UN);
|
||||||
@fclose($fp);
|
@fclose($fp);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
if (($try==1) and ($this->_hashedDirectoryLevel>0)) {
|
|
||||||
$hash = md5($this->_fileName);
|
|
||||||
$root = $this->_cacheDir;
|
|
||||||
for ($i=0 ; $i<$this->_hashedDirectoryLevel ; $i++) {
|
|
||||||
$root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/';
|
|
||||||
@mkdir($root, $this->_hashedDirectoryUmask);
|
|
||||||
}
|
}
|
||||||
$try = 2;
|
return $this->raiseError('Cache_Lite : Unable to write cache file : '.$this->_file, -1);
|
||||||
} else {
|
|
||||||
$try = 999;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->raiseError('Cache_Lite : Unable to write cache file : '.$this->_file, -1);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the given data in the cache file and control it just after to avoir corrupted cache entries
|
* Write the given data in the cache file and control it just after to avoir corrupted cache entries
|
||||||
*
|
*
|
||||||
* @param string $data data to put in cache
|
* @param string $data data to put in cache
|
||||||
* @return boolean true if the test is ok
|
* @return boolean true if the test is ok (else : false or a PEAR_Error object)
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _writeAndControl($data)
|
function _writeAndControl($data)
|
||||||
{
|
{
|
||||||
$this->_write($data);
|
$result = $this->_write($data);
|
||||||
$dataRead = $this->_read($data);
|
if (is_object($result)) {
|
||||||
|
return $result; # We return the PEAR_Error object
|
||||||
|
}
|
||||||
|
$dataRead = $this->_read();
|
||||||
|
if (is_object($dataRead)) {
|
||||||
|
return $dataRead; # We return the PEAR_Error object
|
||||||
|
}
|
||||||
|
if ((is_bool($dataRead)) && (!$dataRead)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return ($dataRead==$data);
|
return ($dataRead==$data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -746,7 +826,7 @@ class Cache_Lite
|
|||||||
case 'strlen':
|
case 'strlen':
|
||||||
return sprintf('% 32d', strlen($data));
|
return sprintf('% 32d', strlen($data));
|
||||||
default:
|
default:
|
||||||
$this->raiseError('Unknown controlType ! (available values are only \'md5\', \'crc32\', \'strlen\')', -5);
|
return $this->raiseError('Unknown controlType ! (available values are only \'md5\', \'crc32\', \'strlen\')', -5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,12 +11,12 @@
|
|||||||
* Technical choices are described in the 'docs/technical' file
|
* Technical choices are described in the 'docs/technical' file
|
||||||
*
|
*
|
||||||
* @package Cache_Lite
|
* @package Cache_Lite
|
||||||
* @version $Id: Function.php,v 1.6 2004/02/03 17:07:13 fab Exp $
|
* @version $Id: Function.php,v 1.11 2006/12/14 12:59:43 cweiske Exp $
|
||||||
* @author Sebastian BERGMANN <sb@sebastian-bergmann.de>
|
* @author Sebastian BERGMANN <sb@sebastian-bergmann.de>
|
||||||
* @author Fabien MARTY <fab@php.net>
|
* @author Fabien MARTY <fab@php.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once(dirname(__FILE__) . '/../Lite.php');
|
require_once dirname(__FILE__) . '/../Lite.php';
|
||||||
|
|
||||||
class Cache_Lite_Function extends Cache_Lite
|
class Cache_Lite_Function extends Cache_Lite
|
||||||
{
|
{
|
||||||
@ -30,6 +30,37 @@ class Cache_Lite_Function extends Cache_Lite
|
|||||||
*/
|
*/
|
||||||
var $_defaultGroup = 'Cache_Lite_Function';
|
var $_defaultGroup = 'Cache_Lite_Function';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't cache the method call when its output contains the string "NOCACHE"
|
||||||
|
*
|
||||||
|
* if set to true, the output of the method will never be displayed (because the output is used
|
||||||
|
* to control the cache)
|
||||||
|
*
|
||||||
|
* @var boolean $_dontCacheWhenTheOutputContainsNOCACHE
|
||||||
|
*/
|
||||||
|
var $_dontCacheWhenTheOutputContainsNOCACHE = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't cache the method call when its result is false
|
||||||
|
*
|
||||||
|
* @var boolean $_dontCacheWhenTheResultIsFalse
|
||||||
|
*/
|
||||||
|
var $_dontCacheWhenTheResultIsFalse = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't cache the method call when its result is null
|
||||||
|
*
|
||||||
|
* @var boolean $_dontCacheWhenTheResultIsNull
|
||||||
|
*/
|
||||||
|
var $_dontCacheWhenTheResultIsNull = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Debug the Cache_Lite_Function caching process
|
||||||
|
*
|
||||||
|
* @var boolean $_debugCacheLiteFunction
|
||||||
|
*/
|
||||||
|
var $_debugCacheLiteFunction = false;
|
||||||
|
|
||||||
// --- Public methods ----
|
// --- Public methods ----
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,7 +72,11 @@ class Cache_Lite_Function extends Cache_Lite
|
|||||||
* Comparing to Cache_Lite constructor, there is another option :
|
* Comparing to Cache_Lite constructor, there is another option :
|
||||||
* $options = array(
|
* $options = array(
|
||||||
* (...) see Cache_Lite constructor
|
* (...) see Cache_Lite constructor
|
||||||
* 'defaultGroup' => default cache group for function caching (string)
|
* 'debugCacheLiteFunction' => (bool) debug the caching process,
|
||||||
|
* 'defaultGroup' => default cache group for function caching (string),
|
||||||
|
* 'dontCacheWhenTheOutputContainsNOCACHE' => (bool) don't cache when the function output contains "NOCACHE",
|
||||||
|
* 'dontCacheWhenTheResultIsFalse' => (bool) don't cache when the function result is false,
|
||||||
|
* 'dontCacheWhenTheResultIsNull' => (bool don't cache when the function result is null
|
||||||
* );
|
* );
|
||||||
*
|
*
|
||||||
* @param array $options options
|
* @param array $options options
|
||||||
@ -49,9 +84,14 @@ class Cache_Lite_Function extends Cache_Lite
|
|||||||
*/
|
*/
|
||||||
function Cache_Lite_Function($options = array(NULL))
|
function Cache_Lite_Function($options = array(NULL))
|
||||||
{
|
{
|
||||||
if (isset($options['defaultGroup'])) {
|
$availableOptions = array('debugCacheLiteFunction', 'defaultGroup', 'dontCacheWhenTheOutputContainsNOCACHE', 'dontCacheWhenTheResultIsFalse', 'dontCacheWhenTheResultIsNull');
|
||||||
$this->_defaultGroup = $options['defaultGroup'];
|
while (list($name, $value) = each($options)) {
|
||||||
|
if (in_array($name, $availableOptions)) {
|
||||||
|
$property = '_'.$name;
|
||||||
|
$this->$property = $value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
reset($options);
|
||||||
$this->Cache_Lite($options);
|
$this->Cache_Lite($options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,26 +109,33 @@ class Cache_Lite_Function extends Cache_Lite
|
|||||||
function call()
|
function call()
|
||||||
{
|
{
|
||||||
$arguments = func_get_args();
|
$arguments = func_get_args();
|
||||||
$id = serialize($arguments); // Generate a cache id
|
$id = $this->_makeId($arguments);
|
||||||
if (!$this->_fileNameProtection) {
|
|
||||||
$id = md5($id);
|
|
||||||
// if fileNameProtection is set to false, then the id has to be hashed
|
|
||||||
// because it's a very bad file name in most cases
|
|
||||||
}
|
|
||||||
$data = $this->get($id, $this->_defaultGroup);
|
$data = $this->get($id, $this->_defaultGroup);
|
||||||
if ($data !== false) {
|
if ($data !== false) {
|
||||||
|
if ($this->_debugCacheLiteFunction) {
|
||||||
|
echo "Cache hit !\n";
|
||||||
|
}
|
||||||
$array = unserialize($data);
|
$array = unserialize($data);
|
||||||
$output = $array['output'];
|
$output = $array['output'];
|
||||||
$result = $array['result'];
|
$result = $array['result'];
|
||||||
} else {
|
} else {
|
||||||
|
if ($this->_debugCacheLiteFunction) {
|
||||||
|
echo "Cache missed !\n";
|
||||||
|
}
|
||||||
ob_start();
|
ob_start();
|
||||||
ob_implicit_flush(false);
|
ob_implicit_flush(false);
|
||||||
$target = array_shift($arguments);
|
$target = array_shift($arguments);
|
||||||
|
if (is_array($target)) {
|
||||||
|
// in this case, $target is for example array($obj, 'method')
|
||||||
|
$object = $target[0];
|
||||||
|
$method = $target[1];
|
||||||
|
$result = call_user_func_array(array(&$object, $method), $arguments);
|
||||||
|
} else {
|
||||||
if (strstr($target, '::')) { // classname::staticMethod
|
if (strstr($target, '::')) { // classname::staticMethod
|
||||||
list($class, $method) = explode('::', $target);
|
list($class, $method) = explode('::', $target);
|
||||||
$result = call_user_func_array(array($class, $method), $arguments);
|
$result = call_user_func_array(array($class, $method), $arguments);
|
||||||
} else if (strstr($target, '->')) { // object->method
|
} else if (strstr($target, '->')) { // object->method
|
||||||
// use a stupid name ($objet_123456789 because) of problems when the object
|
// use a stupid name ($objet_123456789 because) of problems where the object
|
||||||
// name is the same as this var name
|
// name is the same as this var name
|
||||||
list($object_123456789, $method) = explode('->', $target);
|
list($object_123456789, $method) = explode('->', $target);
|
||||||
global $$object_123456789;
|
global $$object_123456789;
|
||||||
@ -96,8 +143,26 @@ class Cache_Lite_Function extends Cache_Lite
|
|||||||
} else { // function
|
} else { // function
|
||||||
$result = call_user_func_array($target, $arguments);
|
$result = call_user_func_array($target, $arguments);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$output = ob_get_contents();
|
$output = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
if ($this->_dontCacheWhenTheResultIsFalse) {
|
||||||
|
if ((is_bool($result)) && (!($result))) {
|
||||||
|
echo($output);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($this->_dontCacheWhenTheResultIsNull) {
|
||||||
|
if (is_null($result)) {
|
||||||
|
echo($output);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($this->_dontCacheWhenTheOutputContainsNOCACHE) {
|
||||||
|
if (strpos($output, 'NOCACHE') > -1) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
$array['output'] = $output;
|
$array['output'] = $output;
|
||||||
$array['result'] = $result;
|
$array['result'] = $result;
|
||||||
$this->save(serialize($array), $id, $this->_defaultGroup);
|
$this->save(serialize($array), $id, $this->_defaultGroup);
|
||||||
@ -106,6 +171,41 @@ class Cache_Lite_Function extends Cache_Lite
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drop a cache file
|
||||||
|
*
|
||||||
|
* Arguments of this method are read with func_get_args. So it doesn't appear
|
||||||
|
* in the function definition. Synopsis :
|
||||||
|
* remove('functionName', $arg1, $arg2, ...)
|
||||||
|
* (arg1, arg2... are arguments of 'functionName')
|
||||||
|
*
|
||||||
|
* @return boolean true if no problem
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function drop()
|
||||||
|
{
|
||||||
|
$id = $this->_makeId(func_get_args());
|
||||||
|
return $this->remove($id, $this->_defaultGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make an id for the cache
|
||||||
|
*
|
||||||
|
* @var array result of func_get_args for the call() or the remove() method
|
||||||
|
* @return string id
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
function _makeId($arguments)
|
||||||
|
{
|
||||||
|
$id = serialize($arguments); // Generate a cache id
|
||||||
|
if (!$this->_fileNameProtection) {
|
||||||
|
$id = md5($id);
|
||||||
|
// if fileNameProtection is set to false, then the id has to be hashed
|
||||||
|
// because it's a very bad file name in most cases
|
||||||
|
}
|
||||||
|
return $id;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@ -7,11 +7,11 @@
|
|||||||
* Technical choices are described in the 'docs/technical' file
|
* Technical choices are described in the 'docs/technical' file
|
||||||
*
|
*
|
||||||
* @package Cache_Lite
|
* @package Cache_Lite
|
||||||
* @version $Id: Output.php,v 1.3 2005/04/17 21:40:18 fab Exp $
|
* @version $Id: Output.php,v 1.4 2006/01/29 00:22:07 fab Exp $
|
||||||
* @author Fabien MARTY <fab@php.net>
|
* @author Fabien MARTY <fab@php.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once(dirname(__FILE__) . '/../Lite.php');
|
require_once dirname(__FILE__) . '/../Lite.php';
|
||||||
|
|
||||||
class Cache_Lite_Output extends Cache_Lite
|
class Cache_Lite_Output extends Cache_Lite
|
||||||
{
|
{
|
||||||
@ -47,12 +47,11 @@ class Cache_Lite_Output extends Cache_Lite
|
|||||||
if ($data !== false) {
|
if ($data !== false) {
|
||||||
echo($data);
|
echo($data);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
ob_start();
|
ob_start();
|
||||||
ob_implicit_flush(false);
|
ob_implicit_flush(false);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop the cache
|
* Stop the cache
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
Version 1.6 ()
|
Version 1.6 ()
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
* Upgraded to PEAR Cache_Lite 1.7.8
|
||||||
|
|
||||||
* Added experimental global variable $i18n_filename_utf8 that can
|
* Added experimental global variable $i18n_filename_utf8 that can
|
||||||
be set in a serendipity_config_local.inc.php or language include
|
be set in a serendipity_config_local.inc.php or language include
|
||||||
file, which will return Unicode-Permalinks.
|
file, which will return Unicode-Permalinks.
|
||||||
|
Reference in New Issue
Block a user