|
/*
$ Cache_set = array (
// Cache path, and add "/"
'Cacherot' => './cache /',
// Cache time
'Cachetime' => 20,
// Cache type
'Cachetype '=> 1,
// Extension
'Cacheexe '=>'. php'
);
$ Cache = new Cache ($ cache_set );
$ A = array ('1', '2 ');
$ A = "aaa ";
$ B = '';
If ($ cache-> cache_is ("d ")){
$ C = $ cache-> cache_read ("d ");
Echo "c ";
Print_r ($ c );
} Else {
$ B = $ cache-> cache_data ('D', $ );
}
Print_r ($ B );
// $ Cache-> clear ("");
// Echo $ cache-> cache_read ("./cache/d. php ");
// Echo $ d;
*/
/**
* Data cache class v1.0
* @ Author Shoke
* 16:02:26
* Used to cache data, such as variables, but not pages
*/
Class Cache {
// Configure
Public $ config = array (
// Cache path
'Cacherot' => './cache /',
// Cache time
'Cachetime' => 1,
// Cache type 1 stringized Data 2 variable
'Cachetype '=> 2,
// Extension
'Cacheexe '=>'. php'
// Convert the intermediate variable
);
Public $ return_name = array ();
Function _ construct ($ cache_set = array ())
{
If (! Empty ($ cache_set) $ this-> config = array_merge ($ this-> config, $ cache_set );
$ This-> config ['classname'] = _ CLASS __;
}
Public function clear ($ filename = ''){
If (file_exists ($ this-> cache_file ($ filename ))){
@ Unlink ($ this-> cache_file ($ filename ));
} Elseif (empty ($ filename )){
$ This-> clear_dir ($ this-> config ['cacherot']);
} Else {
$ This-> clear_dir ($ this-> config ['cacheroot']. $ filename );
Echo $ this-> config ['cacherot']. $ filename;
}
}
// Delete the path cyclically
Private function clear_dir ($ dir, $ to = false)
{
If ($ list = glob ($ dir .'/*'))
{
Foreach ($ list as $ file)
{
Is_dir ($ file )? $ This-> clear_dir ($ file): unlink ($ file );
}
}
If ($ to = false) rmdir ($ dir );
}
// Write cache
Private function cache_write ($ filename, $ writetext, $ openmod = 'w '){
If (! File_exists ($ filename )){
@ $ This-> makeDir (dirname ($ filename ));
}
If (@ $ fp = fopen ($ filename, $ openmod )){
Flock ($ fp, 2 );
Fwrite ($ fp, $ writetext );
Fclose ($ fp );
Return true;
} Else {
Echo "File: $ filename write error .";
Return false;
}
}
// Return true if the cache validity period is valid
Public function cache_is ($ fileName ){
$ FileName = $ this-> cache_file ($ fileName );
If (file_exists ($ fileName )){
// Never expire if the cache time is negative
If ($ this-> config ['cachetime'] <0 ){
Return true;
}
// If the cache time is 0, it will always expire
If ($ this-> config ['cachetime'] = 0 ){
Return false;
}
// Obtain the cache file creation time
$ Ctime = intval (filemtime ($ fileName ));
// Compare whether the cache time is greater than the cache time. if yes, it expires or it does not expire.
If (time ()-$ ctime> $ this-> config ['cachetime']) {
Return false;
} Else {
Return true;
}
// If the file does not exist, it is deemed as expired.
} Else {
Return false;
}
}
Public function cache_data ($ name, $ data ){
$ Varname = $ name;
$ Name = $ this-> cache_file ($ name );
// Config ['cachetime'] = 0. If cache is not enabled, data is directly returned.
If ($ this-> config ['cachetime'] <> 0 ){
If ($ this-> config ['cachetype '] = 1 ){
$ Write_data =" ". Serialize ($ data );
// Return $ data;
} Else {
$ Write_data ="
$ Write_data. = var_export ($ data, true );
$ Write_data. = "; \ r \ n?> ";
}
$ This-> cache_write ($ name, $ write_data );
}
Return $ data;
}
// Cache file name
Private function cache_file ($ filename ){
Return $ this-> config ['cacherot']. $ filename. $ this-> config ['cacheexe '];
}
// Read the file
Public function cache_read ($ file ){
$ File = $ this-> cache_file ($ file );
If (! File_exists ($ file )){
Return '';
}
If ($ this-> config ['cachetype '] = 1 ){
If (function_exists ('File _ get_contents ')){
$ Cache_Content = file_get_contents ($ file );
} Else {
$ Fopen = fopen ($ file, 'r ');
$ Cache_Content = '';
Do {
$ Data = fread ($ fopen, filesize ($ file ));
If (strlen ($ data) === 0) break;
$ Cache_Content. = $ data;
} While (1 );
Fclose ($ fopen );
}
$ Cache_Content = substr ($ cache_Content, 13);/* Remove */
$ Cache_Content = unserialize ($ cache_Content );
Return $ cache_Content;
} Else {
Include_once ($ file );
Return $ var;
}
}
// Create a directory cyclically
Private function makeDir ($ dir, $ mode = 0777 ){
If (! $ Dir) return 0;
$ Dir = str_replace ("\\\\", "/", $ dir );
$ Mdir = "";
Foreach (explode ("/", $ dir) as $ val ){
$ Mdir. = $ val ."/";
If ($ val = ".." | $ val = "." | trim ($ val) = "") continue;
If (! File_exists ($ mdir )){
If (! @ Mkdir ($ mdir, $ mode )){
Return false;
}
}
}
Return true;
}
}
?>
|