Post a Cache class personal use is good

xiaoxiao2021-03-06  60

PHP code:

Class Cache

{

Var $ cachedirectory;

Var $ cachedure = 3600;

Var $ cachefilename;

Function Cache ($ CACHEDURATION = 3600, $ CacheDirectory = '. / cache')

{

$ this-> cachedure = 0;

$ this-> cachefilename = '';

$ this-> cachedirectory = '.';

$ this-> Updatecache ($ Cacheduration, $ Cachedirectory;

}

Function _makecachefolder ()

{

IF (! is_dir ($ this-> cachedirectory))

{

$ TEMP = EXPLODE ('/', $ this-> cachedirectory);

$ CUR_DIR = '';

For ($ i = 0; $ i

{

$ CUR_DIR. = $ TEMP [$ I]. '/';

IF (! is_dir ($ CUR_DIR))

{

IF (@mkdir ($ Cur_Dir, 777) && ($ CUR_DIR! = getcwd ()))

{

$ this -> _ writefile ($ cur_dir. '. htaccess', 'deny from all';

$ this -> _ writefile ($ cur_dir.'index.html ',' ');

}

}

}

}

}

Function getCacheFileName ()

{

Return $ this-> cachefilename;

}

Function_SetcacheFileName ($ Contents)

{

$ this-> cachefilename = $ this-> cachedirectory. '/'. MD5 ($ contents). '. txt';

}

Function Incache ($ Contents, $ STY = ')

{

$ this -> _ setcachefilename ($ contents);

IF ($ STY == 1)

{

Return File_exists ($ this-> cachefilename);

}

Else

{

IF (file_exists ($ this-> cachefilename))

{

$ TIM = FilemTime ($ this-> cachefilename);

IF ((Time () - $ TIM> $ this-> cacheduration)

{

Return False;

}

Else

{

Return True;

}

}

Else

{

Return False;

}

}

}

Function readcache ()

{

$ contents = '';

$ fp = @fopen ($ this-> cachefilename, 'r');

IF ($ fp)

{

While (! Feof ($ fp) $ contents. = FREAD ($ FP, 4096);

Fclose ($ fp);

}

RETURN $ Contents;

}

Function updatecache ($ Cacheduration = 3600, $ CacheFolder = '. / cache') {

$ this-> cachedure = $ cacheduration;

$ this-> cachedirectory = $ cachefolder;

$ this -> _ makecachefolder ();

}

Function SaveIncache ($ Contents, $ FileName = ')

{

$ TIM = FilemTime ($ this-> cachefilename);

IF (TRIM ($ filename) == ') $ filename = $ contents;

IF ($ this-> Incache ($ FileName, 1) && (Time () - $ TIM)> $ this-> cacheduration)

{

@unlink ($ this-> cachefilename);

}

$ this -> _ writefile ($ this-> cachefilename, $ contents);

}

Function_Writefile ($ FileName, $ Contents)

{

IF (! File_exists ($ filename))

{

$ fp = @fopen ($ filename, 'w');

IF ($ fp)

{

FPUTS ($ FP, $ Contents);

Fclose ($ fp);

}

}

Else

{

Unlink ($ filename);

$ fp = @fopen ($ filename, 'w');

IF ($ fp)

{

FPUTS ($ FP, $ Contents);

Fclose ($ fp);

}

}

}

}

?>

PHP code:

Instructions:

Include_once (_conpath _. "class / class_cache.php");

$ cache = new cache ();

IF ($ cache-> incache ($ _ server ['query_string')))

{

$ output = $ cache-> readcache ();

}

Else

{

Ob_start ();

/ / Here is the content of the output

$ output = ob_get_clean ();

$ Cache-> SaveIncache ($ OTPUT, $ _ Server ['Query_String']);

}

Unset ($ cache);

Print ($ OUTPUT);

EXIT;

转载请注明原文地址:https://www.9cbs.com/read-112265.html

New Post(0)