<?
/*
* This file is part of Webgenerator-X,
* an object oriented website management engine working an top of
* Apache/PHP4/MySQL.
* http://www.webgenerator-x.com
* @2001 REGNI Giorgio
* hide@address.com
*
* Webgenerator-X is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Webgenerator-X is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*************************************************************************/
/* REGNI Giorgio
20/04/2001
WG-X
Cache File, WG-X Cache Engine
This file allow module to know if they need to upadte specific content
or can retreive them from file.
A data is named by its module and its data_code.
The Cache Data itself is a small php script which set 2 variables with content and time
20/05/2001 added clear_cache
*/
class A_CacheEngine
{
var $cache_dir;
var $WIN32;
var $nosql=false;
var $CACHED_FILE = array(); // stores file there to cache multiples fopen
// only data is stored here, not the timestamp
var $time; // actual time ( the same time is used through the script)
// Must know where to store the data
// No trailing slash
// Use default cache dir if nothing entered
function A_CacheEngine($newcache_dir="")
{
global $win32,$cache_dir,$databaseoff,$cache_expire,$root_dir;
$this->WIN32 = $win32;
if ($newcache_dir!="")
$this->cache_dir =$newcache_dir;
else
$this->cache_dir = $cache_dir;
$this->cache_dir = $root_dir.'/'.$this->cache_dir;
$this->nosql = $databaseoff;
$this->time = date ("U");
}
// Get the cache Data associated with the data_code
// return an array [ok] bool succes
// [data] the data in the cache if success
// need to call is uptodate first,else the file is not loaded !
function Get_Data($module,$data_code,$tags="")
{
$filename = $this->Generate_Filename( $module,$data_code,$tags );
// check the file is opened
if ( isset($this->CACHED_FILE[ $filename ]) )
{
// file in cache
return array(ok=>true,data=>$this->CACHED_FILE[ $filename ]);
}
else
return array(ok=>false,data=>"You need to call Is_Uptodate first !");
}
// Store the cache Data associated with the data_code and tags
// return bool success
// you can specify how many time to try before aborting
function Store_Data($module,$data_code,$tags,$data)
{
global $db_prefix;
$success=false;
$max_try = 5;
$filename = $this->Generate_Filename( $module,$data_code,$tags );
$data = "<? \$modtime=$this->time;\n \$data='".addslashes($data)."'; ?>";
for( $i=0; ($i<$max_try) and ($success==false);$i++)
{
if ( $fp=fopen( $filename, "w" ) )
{
// file opened
if ( flock($fp,3) == true ) // lock writing
{
if (fwrite($fp,$data) == true)
{
$success = true;
}
}
fclose( $fp );
}
else
usleep(10); // WAIT 10msecond
}
return $success;
}
// return true if cache file exist else return false
// if file exist, cache it in CACHE_FILE[] too
// $expire = time laps to say cache is too old (seconds)
// $expire = 0 : this module data is always taken from cache
function Is_UptoDate($module,$data_code,$tags,$expire)
{
$noneedupdate = false;
$filename = $this->Generate_Filename( $module,$data_code,$tags );
if (file_exists($filename))
{
// If the cache file exist, go.
// I am so proud of this !!!!!
// using include... i am great !
// this include set values $modtime and $data
include ($filename);
if ($expire==0 or $this->nosql) // =0 means always cache + take from cache if mysql not available now
{
$this->CACHED_FILE[$filename] = stripslashes($data);
$noneedupdate = true;
}
else
{
if (($modtime + $expire) > $this->time)
{
$this->CACHED_FILE[$filename] = stripslashes($data);
$noneedupdate = true;
}
}
}
return $noneedupdate;
}
// delete a cache file
// used to force update after changes
// delete all languages and tags belonging to $module $data_code
// return true also if the cache entry did not exist
function clear_cache($module,$data_code)
{
global $root_dir,$cache_dir;
$ok =true;
$cache = $root_dir."/".$cache_dir."/";
$match = "^[a-zA-Z0-9]*" . "-".strtolower(trim($module))."-".strtolower(trim($data_code))."-". "[a-zA-Z0-9\-]*$";
$handle=opendir($cache);
while (($file = readdir($handle))!=false) {
if (ereg($match, $file))
{
//echo "$cache.$file<br>\n";
if (!unlink ( $cache.$file ))
$ok = false;
}
}
closedir($handle);
return $ok;
}
// generate a filename with $module-$data_code-implode($tags)
// use a implode of the array $tags or $tags if not array
function Generate_Filename( $module,$data_code,$tags="" )
{
global $language; // cache are different uppon language
if ($tags!="" and is_array($tags) )
$tags=implode('-',$tags);
$module = strtolower(trim($module));
$data_code = strtolower(trim($data_code));
$tags = strtolower(trim($tags));
return concat_dirs($this->cache_dir,$language."-".$module."-".$data_code."-".$tags);
}
} // End Class
?>