Php abstract cache type

Source: Internet
Author: User
Tags flock
Php abstract cache class & lt ;? Phprequire_oncedirname (_ FILE _)... ModelIfaceIcache. php; abstractclassAllCacheimplementsIcache {static & nbsp; $ cache_dir; const & nbsp; CACHETIME php abstract cache class
Require_once dirname (_ FILE _). "/../Model/Iface/Icache. php ";
Abstract class AllCache implements Icache
{
Static $ cache_dir;
Const CACHETIME = 60; // The cache time is 60 seconds.
Static $ _ data;
Static $ _ interce;
// Constructor
Public function _ construct ()
{
$ Cache_dirname = dirname (_ FILE _). "/../Cache ";
If (! @ Is_dir ($ cache_dirname ))
{
If (! @ Mkdir ($ cache_dirname, 0777 ))
{
Self: msgbox ('The cached file does not exist and cannot be created, you need to manually create .');
Return false;
}
}
Self: $ cache_dir = $ cache_dirname;
}
/**
* Static call
* Self indicates the class itself.
*/
Public static function getInstance ()
{
If (! (Self: $ _ interce instanceof self )){
Self: $ _ interce = new self ();
}
Return self: $ _ interce;
}
// Output exception
Private function msgbox ($ errorstring)
{
Echo" Error:
".$errorstring."
";
}
// Set the cache file
Private function cache_page ($ pageurl, $ pagedata)
{
If (! $ Fso = fopen ($ pageurl, 'w ')){
Self: msgboxs ('cache file cannot be opened. '); // trigger_error
Return false;
}
If (! Flock ($ fso, LOCK_EX) {// LOCK_NB, locking
Self: msgboxs ('unable to lock the cache file. '); // trigger_error
Return false;
}
If (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats
Self: msgboxs ('cache file cannot be written. '); // trigger_error
Return false;
}
Flock ($ fso, LOCK_UN); // release lock
Fclose ($ fso );
Return true;
}
// Read the file status
Private function isOpenCache ($ cacheFile)
{
If (! File_exists ($ cacheFile )){
Self: msgbox ('cache file cannot be read. '); exit;
}
/* Echo 'read the cached file: '. $ cacheFile;
Return unserialize (file_get_contents ($ cacheFile ));
$ Fso = fopen ($ cacheFile, 'r ');
$ Data = fread ($ fso, filesize ($ cacheFile ));
Fclose ($ fso );
Return $ data ;*/
}

/**
* Format data
*/
Private function setFileContent ($ data)
{
$ Html =" $ Html. = "\ $ data =". var_export ($ data, true). "; \ n ";
$ Html. = "?> ";
Return $ html;
}

/**
* Set cache data
* @ Param $ cacheFile cache file name
× @ Param $ _ data cache data source
*/
Public function readdata($cachefile='ulult_cache.txt ', $ _ data)
{
$ _ Data = self: SetFileContent ($ _ data );
$ CacheFile = self: $ cache_dir. "/". $ cacheFile;
If (file_exists ($ cacheFile ))
{
Self: isOpenCache ($ cacheFile );
If (filemtime ($ cacheFile) <(time ()-self: CACHETIME ))
{
Self: cache_page ($ cacheFile, $ _ data );
}
} Else {
Self: cache_page ($ cacheFile, $ _ data );
}
Require_once ($ cacheFile );
Self: $ _ data = $ data;
}

/**
* Getting data
*/
Public function GetData ()
{
Return self: $ _ data;
}
}
/*
AllCache: readData ("cache_news.php", array (0, 1, 2, 3, 5, 6 ));
Print_r (AllCache: GetData ());
*/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.