欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

一个php的页面缓存类

程序员文章站 2022-03-04 15:31:57
...
  1. /* $cache = new Cache("../cache/",20); // 构造函数,创建缓存类对象

  2. $cache->PutCache(); // 倒出缓存
  3. */
  4. class Cache
  5. {
  6. private $CacheDir = ’Cache’; /* 缓存目录 */
  7. private $SetTimeOut = 10; /* 缓存过期时间 */
  8. private $SetExt = ’.cache’; /* 缓存文件后缀名 */
  9. private $CacheFileUrl = ’’; /* 缓存文件所在地址 */
  10. private $CacheConfigFile = ’’; /* 缓存文件配置信息 */
  11. public $LastUnixTimePoke = 0; /* 上一次缓存的 Unix 时间戳 */

  12. public $CurrentUnixTimePoke = 0;/* 当前缓存的 Unix 时间戳 */
  13. public $NextUnixTimePoke = 0; /* 下一次缓存的 Unix 时间戳 */
  14. public $UnixNowToNext = 0; /* 现在和下一次缓存相差的 Unix 时间戳 */
  15. public $LastTimePoke = 0; /* 上一次缓存的时间 */

  16. public $CurrentTimePoke = 0;/* 当前缓存的时间 */
  17. public $NextTimePoke = 0; /* 下一次缓存的时间 */
  18. public $DataLength = 0; /* 缓存区内容长度 */

  19. public $CacheToPage = ’’; /* 缓存文件内容 */
  20. private $SplitTeam = false; /* 是否分组存放Cache文件 */
  21. public $Cache = false; /* 是否需要缓存,用户外界判断 */

  22. private $_IsCache = false; /* 是否能够缓存 */

  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(’缓存过期时间设置无效’);
  30. return false;
  31. } else {
  32. $this->SetTimeOut = $SetTimeOut;
  33. }
  34. $this->SetExt = $SetExt;
  35. /* 缓存开始 */

  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 . $_CacheFile;
  62. $_CacheConfigUrl = $this->CacheDir . $_CacheConfig;
  63. }
  64. if(!file_exists($_CacheUrl))

  65. {
  66. $hanld = @fopen($_CacheUrl,"w");
  67. @fclose($hanld);
  68. }
  69. if(!file_exists($_CacheConfigUrl))

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

  75. $this->CacheFileUrl = $_CacheUrl;
  76. $this->CheckCache();
  77. return true;
  78. }
  79. private function CheckCache()

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

  85. $this->NextUnixTimePoke = $_IsTimeOut;
  86. $this->CurrentUnixTimePoke = time();
  87. $this->UnixNowToNext = $this->NextUnixTimePoke - time();
  88. $this->LastTimePoke = date("Y-m-d H:i:s",$_FileEditTime);

  89. $this->NextTimePoke = date("Y-m-d H:i:s",$_IsTimeOut);
  90. $this->CurrentTimePoke = date("Y-m-d H:i:s",time());
  91. $_TxtInformation = "上次缓存时间戳: $this->LastUnixTimePoke ";

  92. $_TxtInformation .= "当前缓存时间戳: $this->CurrentUnixTimePoke ";
  93. $_TxtInformation .= "下次缓存时间戳: $this->NextUnixTimePoke ";
  94. $_TxtInformation .= "上次缓存时间: $this->LastTimePoke ";

  95. $_TxtInformation .= "当前缓存时间: $this->CurrentTimePoke ";
  96. $_TxtInformation .= "下次缓存时间: $this->NextTimePoke ";
  97. $_TxtInformation .= "距离下次缓存戳: $this->UnixNowToNext ";

  98. $handl = @fopen($this->CacheConfigFile,’w’);

  99. if($handl)
  100. {
  101. @fwrite($handl,$_TxtInformation);
  102. @fclose($handl);
  103. }
  104. if($_IsTimeOut >= time())

  105. {
  106. $this->GetCacheData();
  107. }
  108. }
  109. private function ClearCacheFile()

  110. {
  111. @unlink($this->CacheFileUrl);
  112. @unlink($this->CacheConfigFile);
  113. }
  114. public function PutCache()

  115. {
  116. $this->DataLength = ob_get_length();
  117. $PutData = ob_get_contents();
  118. if(!file_exists($this->CacheFileUrl))
  119. {
  120. $CreateOK = $this->CreateCache();
  121. if(!$CreateOK)
  122. {
  123. $this->ErrResponse(’检查缓存文件时产生错误,缓存文件创建失败’);
  124. return false;
  125. }
  126. } else {
  127. $hanld = @fopen($this->CacheFileUrl,"w");
  128. if($hanld)
  129. {
  130. if(@is_writable($this->CacheFileUrl))

  131. {
  132. @flock($hanld, LOCK_EX);
  133. $_PutData = @fwrite($hanld,$PutData);
  134. @flock($hanld, LOCK_UN);
  135. if(!$_PutData)
  136. {
  137. $this->ErrResponse(’无法更改当前缓存文件内容’);
  138. return false;
  139. } else {
  140. @fclose($hanld);
  141. return true;
  142. }
  143. } else {
  144. $this->ErrResponse(’缓存文件不可写’);
  145. return false;
  146. }
  147. } else {
  148. $this->ErrResponse(’打开缓存文件发生致命错误’);

  149. return false;
  150. }
  151. }
  152. }
  153. public function GetCacheData()

  154. {
  155. $hanld = @fopen($this->CacheFileUrl,"r");
  156. if($hanld)
  157. {
  158. if(@is_readable($this->CacheFileUrl))
  159. {
  160. $this->CacheToPage = @file_get_contents($this->CacheFileUrl);
  161. $IsEmpty = count(file($this->CacheFileUrl)); //判断缓存文件是否为空
  162. if($IsEmpty > 0)

  163. {
  164. echo $this->CacheToPage;
  165. @fclose($hanld);
  166. ob_end_flush();
  167. exit();
  168. }
  169. } else {
  170. $this->ErrResponse(’读取缓存文件内容失败’);
  171. return false;
  172. }
  173. } else {
  174. $this->ErrResponse(’打开缓存文件失败’);
  175. return false;
  176. }
  177. }
  178. private function ErrResponse($Msg)

  179. {
  180. echo $Msg;
  181. }
  182. }
  183. ?>
复制代码