/* $ Cache = new Cache ("../cache/", 20); // Constructor to create a cache class object
- $ Cache-> PutCache (); // reverse cache
- */
- Class Cache
- {
- Private $ CacheDir = 'cache';/* Cache directory */
- Private $ SetTimeOut = 10;/* cache expiration time */
- Private $ SetExt = '. cache';/* cache file suffix */
- Private $ CacheFileUrl = '';/* address of the cached file */
- Private $ CacheConfigFile = '';/* cache file configuration information */
Public $ LastUnixTimePoke = 0;/* The last cached Unix timestamp */
- Public $ CurrentUnixTimePoke = 0;/* Unix timestamp currently cached */
- Public $ NextUnixTimePoke = 0;/* Unix timestamp cached next time */
- Public $ UnixNowToNext = 0;/* Unix timestamp that is different from the next cache */
Public $ LastTimePoke = 0;/* last cache time */
- Public $ CurrentTimePoke = 0;/* current cache time */
- Public $ NextTimePoke = 0;/* The next cache time */
Public $ DataLength = 0;/* cache content length */
- Public $ CacheToPage = '';/* cache file content */
- Private $ SplitTeam = false;/* Whether to group and store Cache files */
Public $ Cache = false;/* whether the Cache is required, which is determined by the user */
Private $ _ IsCache = false;/* indicates whether data can be cached */
Public function Cache ($ SetTimeOut = 20, $ CacheDir = 'cache', $ SplitTeam = false, $ SetExt = '. cache ')
- {
- $ This-> CacheDir = $ CacheDir;
- $ This-> SplitTeam = $ SplitTeam;
- If (! Is_numeric ($ SetTimeOut ))
- {
- $ This-> ErrResponse ('invalid cache expiration time settings ');
- Return false;
- } Else {
- $ This-> SetTimeOut = $ SetTimeOut;
- }
- $ This-> SetExt = $ SetExt;
/* Cache start */
- Ob_clean ();
- Ob_start ();
- Ob_implicit_flush (0 );
- $ This-> CreateCache ();
- Return true;
- }
Private function CreateCache ()
- {
- $ _ CacheFile = str_replace ('.', '_', basename ($ _ SERVER ['php _ SELF ']).' _ '.
- Md5 (basename ($ _ SERVER ['php _ SELF ']). $ this-> SetExt;
- $ _ CacheConfig = str_replace ('.', '_', basename ($ _ SERVER ['php _ SELF ']).' _ '.'. cof ';
If (! File_exists ($ this-> CacheDir ))
- {
- Mkdir ($ this-> CacheDir, 0777 );
- }
If ($ this-> SplitTeam)
- {
- $ _ CacheConfigDir = $ this-> CacheDir. str_replace ('.', '_', basename ($ _ SERVER ['php _ SELF ']).' _/';
- If (! File_exists ($ _ CacheConfigDir ))
- {
- Mkdir ($ _ CacheConfigDir, 0777 );
- }
- $ _ CacheUrl = $ this-> CacheDir. $ _ CacheConfigDir. $ _ CacheFile;
- $ _ CacheConfigUrl = $ this-> CacheDir. $ _ CacheConfigDir. $ _ CacheConfig;
- } Else {
- $ _ CacheUrl = $ this-> CacheDir. $ _ CacheFile;
- $ _ CacheConfigUrl = $ this-> CacheDir. $ _ CacheConfig;
- }
If (! File_exists ($ _ CacheUrl ))
- {
- $ Hanld = @ fopen ($ _ CacheUrl, "w ");
- @ Fclose ($ hanld );
- }
If (! File_exists ($ _ CacheConfigUrl ))
- {
- $ Hanld = @ fopen ($ _ CacheConfigUrl, "w ");
- @ Fclose ($ hanld );
- }
$ This-> CacheConfigFile =$ _ CacheConfigUrl;
- $ This-> CacheFileUrl =$ _ CacheUrl;
- $ This-> CheckCache ();
- Return true;
- }
Private function CheckCache ()
- {
- $ _ FileEditTime = @ filemtime ($ this-> CacheFileUrl );
- $ _ TimeOut = $ this-> SetTimeOut;
- $ _ IsTimeOut =$ _ FileEditTime $ _ TimeOut;
$ This-> LastUnixTimePoke =$ _ FileEditTime;
- $ This-> NextUnixTimePoke =$ _ IsTimeOut;
- $ This-> CurrentUnixTimePoke = time ();
- $ This-> UnixNowToNext = $ this-> NextUnixTimePoke-time ();
$ This-> LastTimePoke = date ("Y-m-d H: I: s", $ _ FileEditTime );
- $ This-> NextTimePoke = date ("Y-m-d H: I: s", $ _ IsTimeOut );
- $ This-> CurrentTimePoke = date ("Y-m-d H: I: s", time ());
$ _ TxtInformation = "Last cache timestamp: $ this-> LastUnixTimePoke ";
- $ _ TxtInformation. = "current cache timestamp: $ this-> CurrentUnixTimePoke ";
- $ _ TxtInformation. = "Next cache timestamp: $ this-> NextUnixTimePoke ";
$ _ TxtInformation. = "Last cache Time: $ this-> LastTimePoke ";
- $ _ TxtInformation. = "current cache Time: $ this-> CurrentTimePoke ";
- $ _ TxtInformation. = "Next cache Time: $ this-> NextTimePoke ";
$ _ TxtInformation. = "distance from the next cache timestamp: $ this-> UnixNowToNext ";
$ Handl = @ fopen ($ this-> CacheConfigFile, 'w ');
- If ($ handl)
- {
- @ Fwrite ($ handl, $ _ TxtInformation );
- @ Fclose ($ handl );
- }
If ($ _ IsTimeOut> = time ())
- {
- $ This-> GetCacheData ();
- }
- }
Private function implements achefile ()
- {
- @ Unlink ($ this-> CacheFileUrl );
- @ Unlink ($ this-> CacheConfigFile );
- }
Public function PutCache ()
- {
- $ This-> DataLength = ob_get_length ();
- $ PutData = ob_get_contents ();
- If (! File_exists ($ this-> CacheFileUrl ))
- {
- $ CreateOK = $ this-> CreateCache ();
- If (! $ CreateOK)
- {
- $ This-> ErrResponse ('errors occurred during cache file check, cache file creation failed ');
- Return false;
- }
- } Else {
- $ Hanld = @ fopen ($ this-> CacheFileUrl, "w ");
- If ($ hanld)
- {
If (@ is_writable ($ this-> CacheFileUrl ))
- {
- @ Flock ($ hanld, LOCK_EX );
- $ _ PutData = @ fwrite ($ hanld, $ PutData );
- @ Flock ($ hanld, LOCK_UN );
- If (! $ _ PutData)
- {
- $ This-> ErrResponse ('unable to change the content of the current cached file ');
- Return false;
- } Else {
- @ Fclose ($ hanld );
- Return true;
- }
- } Else {
- $ This-> ErrResponse ('writable cached files ');
- Return false;
- }
- } Else {
$ This-> ErrResponse ('fatal error occurred when opening the cached file ');
- Return false;
- }
- }
- }
Public function GetCacheData ()
- {
- $ Hanld = @ fopen ($ this-> CacheFileUrl, "r ");
- If ($ hanld)
- {
- If (@ is_readable ($ this-> CacheFileUrl ))
- {
- $ This-> CacheToPage = @ file_get_contents ($ this-> CacheFileUrl );
- $ IsEmpty = count (file ($ this-> CacheFileUrl); // determines whether the cached file is empty.
If ($ IsEmpty> 0)
- {
- Echo $ this-> CacheToPage;
- @ Fclose ($ hanld );
- Ob_end_flush ();
- Exit ();
- }
- } Else {
- $ This-> ErrResponse ('failed to read cached file content ');
- Return false;
- }
- } Else {
- $ This-> ErrResponse ('failed to open the cache file ');
- Return false;
- }
- }
Private function ErrResponse ($ Msg)
- {
- Echo $ Msg;
- }
- }
- ?>