Home > Backend Development > PHP Tutorial > A php page caching class

A php page caching class

WBOY
Release: 2016-07-25 09:10:33
Original
1101 people have browsed it
  1. /* $cache = new Cache("../cache/",20); // Constructor, creates cache class object

  2. $cache->PutCache (); // Pour out cache
  3. */
  4. class Cache
  5. {
  6. private $CacheDir = 'Cache'; /* Cache directory*/
  7. private $SetTimeOut = 10; /* Cache expiration time*/
  8. private $SetExt = '.cache'; /* Cache file suffix name*/
  9. private $CacheFileUrl = ''; /* Address of cache file*/
  10. private $CacheConfigFile = ''; /* Cache file configuration information*/

  11. public $LastUnixTimePoke = 0; /* Last cached Unix timestamp*/

  12. public $CurrentUnixTimePoke = 0;/* Current cached Unix timestamp*/
  13. public $NextUnixTimePoke = 0; /* Next time Cached Unix timestamp*/
  14. public $UnixNowToNext = 0; /* Unix timestamp difference between now and next cache*/

  15. public $LastTimePoke = 0; /* Last cached Time*/

  16. public $CurrentTimePoke = 0;/* Current cached time*/
  17. public $NextTimePoke = 0; /* Next cached time*/

  18. public $DataLength = 0; /* Cache area content length*/

  19. public $CacheToPage = ''; /* Cache file content*/
  20. private $SplitTeam = false; /* Whether to store Cache files in groups*/

  21. public $Cache = false; /* Whether caching is needed, the user can judge externally*/

  22. private $_IsCache = false; /* Whether caching is possible*/

  23. public function Cache($SetTimeOut = 20,$CacheDir = 'Cache',$SplitTeam = false,$SetExt = '.cache')

  24. {
  25. $this->CacheDir = $CacheDir;
  26. $this->SplitTeam = $ SplitTeam;
  27. if(!is_numeric($SetTimeOut))
  28. {
  29. $this->ErrResponse('The cache expiration time setting is invalid');
  30. return false;
  31. } else {
  32. $this->SetTimeOut = $SetTimeOut;
  33. }
  34. $this->SetExt = $SetExt;

  35. /* Cache start*/

  36. ob_clean();
  37. ob_start();
  38. ob_implicit_flush(0);
  39. $this-> ;CreateCache();
  40. return true;
  41. }

  42. private function CreateCache()

  43. {
  44. $_CacheFile = str_replace('.','_',basename($_SERVER['PHP_SELF' ])) . '_' .
  45. md5(basename($_SERVER['PHP_SELF'])) . $this->SetExt;
  46. $_CacheConfig = str_replace('.','_',basename($_SERVER[' PHP_SELF'])) . '_' . '.cof';

  47. if(!file_exists($this->CacheDir))

  48. {
  49. mkdir($this->CacheDir, 0777);
  50. }

  51. if($this->SplitTeam)

  52. {
  53. $_CacheConfigDir = $this->CacheDir . str_replace('.','_',basename($ _SERVER['PHP_SELF'])) . '_/';
  54. if(!file_exists($_CacheConfigDir))
  55. {
  56. mkdir($_CacheConfigDir,0777);
  57. }
  58. $_CacheUrl = $this->CacheDir . $_CacheConfigDir . $_CacheFile;
  59. $_CacheConfigUrl = $this->CacheDir . $_CacheConfigDir . $_CacheConfig;
  60. } else {
  61. $_CacheUrl = $this->CacheDir . $_CacheConfig;
  62. }

  63. if(!file_exists($_CacheUrl))

  64. {
  65. $hanld = @fopen($_CacheUrl,"w");
  66. @fclose($hanld);
  67. }

  68. if(!file_exists($_CacheConfigUrl))

  69. {
  70. $hanld = @fopen($_CacheConfigUrl,"w");
  71. @fclose($hanld);
  72. }
  73. $this->CacheConfigFile = $_CacheConfigUrl;

  74. $this->CacheFileUrl = $_CacheUrl;
  75. $this->CheckCache();
  76. return true;
  77. }

  78. private function CheckCache()

  79. {
  80. $_FileEditTime = @filemtime($this->CacheFileUrl);
  81. $_TimeOut = $this->SetTimeOut;
  82. $_IsTimeOut = $_FileEditTime $ _TimeOut;

  83. $this->LastUnixTimePoke = $_FileEditTime;

  84. $this->NextUnixTimePoke = $_IsTimeOut;
  85. $this->CurrentUnixTimePoke = time();
  86. $this-> ;UnixNowToNext = $this->NextUnixTimePoke - time();

  87. $this->LastTimePoke = date("Y-m-d H:i:s",$_FileEditTime);

  88. $this- >NextTimePoke = date("Y-m-d H:i:s",$_IsTimeOut);
  89. $this->CurrentTimePoke = date("Y-m-d H:i:s",time());

  90. < ;p>$_TxtInformation = "Last cache timestamp: $this->LastUnixTimePoke ";
  91. $_TxtInformation .= "Current cache timestamp: $this->CurrentUnixTimePoke ";
  92. $_TxtInformation .= "Next cache time Poke: $this->NextUnixTimePoke ";

  93. $_TxtInformation .= "Last cache time: $this->LastTimePoke ";

  94. $_TxtInformation .= "Current cache time: $this ->CurrentTimePoke ";
  95. $_TxtInformation .= "Next cache time: $this->NextTimePoke ";

  96. $_TxtInformation .= "Next cache stamp: $this-> ;UnixNowToNext ";

  97. $handl = @fopen($this->CacheConfigFile,'w');

  98. if($handl)
  99. {
  100. @fwrite($handl,$_TxtInformation) ;
  101. @fclose($handl);
  102. }

  103. if($_IsTimeOut >= time())

  104. {
  105. $this->GetCacheData();
  106. }
  107. }< /p>
  108. private function ClearCacheFile()

  109. {
  110. @unlink($this->CacheFileUrl);
  111. @unlink($this->CacheConfigFile);
  112. }

  113. {
  114. $this->DataLength = ob_get_length();
  115. $PutData = ob_get_contents();
  116. if(!file_exists($this->CacheFileUrl))
  117. {
  118. $CreateOK = $this ->CreateCache();
  119. if(!$CreateOK)
  120. {
  121. $this->ErrResponse('An error occurred while checking the cache file and the cache file creation failed');
  122. return false;
  123. }
  124. } else {
  125. $hanld = @fopen($this->CacheFileUrl,"w");
  126. if($hanld)
  127. {

  128. if(@is_writable($this->CacheFileUrl))

  129. {
  130. @flock($hanld, LOCK_EX);
  131. $_PutData = @fwrite($hanld,$PutData);
  132. @flock($hanld, LOCK_UN);
  133. if(!$_PutData)
  134. {
  135. $this-> ErrResponse('Cannot change the content of the current cached file');
  136. return false;
  137. } else {
  138. @fclose($hanld);
  139. return true;
  140. }
  141. } else {
  142. $this->ErrResponse('The cached file cannot be write');
  143. return false;
  144. }
  145. } else {

  146. $this->ErrResponse('A fatal error occurred while opening the cache file');

  147. return false;
  148. }
  149. }
  150. }

  151. public function GetCacheData()

  152. {
  153. $hanld = @fopen($this->CacheFileUrl,"r");
  154. if($hanld)
  155. {
  156. if(@is_readable ($this->CacheFileUrl))
  157. {
  158. $this->CacheToPage = @file_get_contents($this->CacheFileUrl);
  159. $IsEmpty = count(file($this->CacheFileUrl)); //Judge Whether the cache file is empty

  160. if($IsEmpty > 0)

  161. {
  162. echo $this->CacheToPage;
  163. @fclose($hanld);
  164. ob_end_flush();
  165. exit( );
  166. }
  167. } else {
  168. $this->ErrResponse('Failed to read cache file content');
  169. return false;
  170. }
  171. } else {
  172. $this->ErrResponse('Failed to open cache file' );
  173. return false;
  174. }
  175. }

  176. private function ErrResponse($Msg)

  177. {
  178. echo $Msg;
  179. }
  180. }
  181. ?>

Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template