Deep in cache_lite
Wednesday, 21. February 2007, 03:09:05
if u don't know what cache_lite, please google it.
cache_lite source code(we can use it out of this class)
cache_lite
Cache_lite//getOption
clean//clean cache
extendLife//extend the life of avalid cache file
get//if cache is available return it
getMemoryCachingState//load the state of the caching memory array from a given file cache
lastModified//return the cache last modification time
raiseError//trigger a pear error
remove//remove a cache file
save//save some data in a cache file
saveMemoryCachingState//save the state of the caching memory array into a cache file cache
setLifeTime//set a new life time
setOption//generic way to set a cache_lite option
setToDebug//when an error is found, the script will stop and the message will be displayed(in debug mode)
we can find import function: getMemoryCachingState, saveMemoryState, save, get.
getMemoryCachingState, saveMemoeyState means what? php can define memory space like c? no.
let's see its source code.
we can find it use array like global array, memoryCache is just Cache_lite public array.
we must know hash(google "data structures") when we analyze set, get function.
cache_lite use some string(hash string) to set directory when use hashedDirectoryLevel(u can find it in cache_lite document) .
if we know how to store it, and it is easy how to get it.
Cache_lite is a small file cache, and it only fit for small system, test system and only php system.
Memcached is a high-performance, distributed memory object caching system。
It fit for any system, and it not only fit for php system.
Write by JL
2007-2-21
cache_lite source code(we can use it out of this class)
cache_lite
Cache_lite//getOption
clean//clean cache
extendLife//extend the life of avalid cache file
get//if cache is available return it
getMemoryCachingState//load the state of the caching memory array from a given file cache
lastModified//return the cache last modification time
raiseError//trigger a pear error
remove//remove a cache file
save//save some data in a cache file
saveMemoryCachingState//save the state of the caching memory array into a cache file cache
setLifeTime//set a new life time
setOption//generic way to set a cache_lite option
setToDebug//when an error is found, the script will stop and the message will be displayed(in debug mode)
we can find import function: getMemoryCachingState, saveMemoryState, save, get.
getMemoryCachingState, saveMemoeyState means what? php can define memory space like c? no.
let's see its source code.
function saveMemoryCachingState($id, $group = 'default')
{
if ($this->_caching) {
$array = array(
'counter' => $this->_memoryCachingCounter,
'array' => $this->_memoryCachingState
);
$data = serialize($array);
$this->save($data, $id, $group);
}
}
function save($data, $id = NULL, $group = 'default')
{
if ($this->_caching) {
if ($this->_automaticSerialization) {
$data = serialize($data);
}
if (isset($id)) {
$this->_setFileName($id, $group);
}
if ($this->_memoryCaching) {
$this->_memoryCacheAdd($data);
if ($this->_onlyMemoryCaching) {
return true;
}
}
if ($this->_automaticCleaningFactor>0) {
$rand = rand(1, $this->_automaticCleaningFactor);
if ($rand==1) {
$this->clean(false, 'old');
}
}
if ($this->_writeControl) {
$res = $this->_writeAndControl($data);
if (is_bool($res)) {
if ($res) {
return true;
}
// if $res if false, we need to invalidate the cache
@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;
}
function _memoryCacheAdd($data)
{
[B]$this->_memoryCachingArray[$this->_file] = $data;[/B]
if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) {
list($key, ) = each($this->_memoryCachingArray);
unset($this->_memoryCachingArray[$key]);
} else {
$this->_memoryCachingCounter = $this->_memoryCachingCounter + 1;
}
}
we can find it use array like global array, memoryCache is just Cache_lite public array.
we must know hash(google "data structures") when we analyze set, get function.
function _write($data)
{
if ($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) . '/';
if (!(@is_dir($root))) {
@mkdir($root, $this->_hashedDirectoryUmask);
}
}
}
$fp = @fopen($this->_file, "wb");
if ($fp) {
if ($this->_fileLocking) @flock($fp, LOCK_EX);
if ($this->_readControl) {
@fwrite($fp, $this->_hash($data, $this->_readControlType), 32);
}
$len = strlen($data);
@fwrite($fp, $data, $len);
if ($this->_fileLocking) @flock($fp, LOCK_UN);
@fclose($fp);
return true;
}
return $this->raiseError('Cache_Lite : Unable to write cache file : '.$this->_file, -1);
}
cache_lite use some string(hash string) to set directory when use hashedDirectoryLevel(u can find it in cache_lite document) .
if we know how to store it, and it is easy how to get it.
function _read()
{
$fp = @fopen($this->_file, "rb");
if ($this->_fileLocking) @flock($fp, LOCK_SH);
if ($fp) {
clearstatcache();
$length = @filesize($this->_file);
$mqr = get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
if ($this->_readControl) {
$hashControl = @fread($fp, 32);
$length = $length - 32;
}
if ($length) {
$data = @fread($fp, $length);
} else {
$data = '';
}
set_magic_quotes_runtime($mqr);
if ($this->_fileLocking) @flock($fp, LOCK_UN);
@fclose($fp);
if ($this->_readControl) {
$hashData = $this->_hash($data, $this->_readControlType);
if ($hashData != $hashControl) {
if (!(is_null($this->_lifeTime))) {
@touch($this->_file, time() - 2*abs($this->_lifeTime));
} else {
@unlink($this->_file);
}
return false;
}
}
return $data;
}
return $this->raiseError('Cache_Lite : Unable to read cache !', -2);
}
Cache_lite is a small file cache, and it only fit for small system, test system and only php system.
Memcached is a high-performance, distributed memory object caching system。
It fit for any system, and it not only fit for php system.
Write by JL
2007-2-21





