首頁 > 後端開發 > php教程 > 一個php的頁面緩存類

一個php的頁面緩存類

WBOY
發布: 2016-07-25 09:10:33
原創
1102 人瀏覽過
  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; /Next = 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🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$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 .PacheDirache. $_CacheConfigUrl = $this->CacheDir . $_CacheConfigDir . $_CacheConfig;
  59. } else {
  60. $_CacheUrl = $this->CacheDir . $_CacheFile;}
  61. if(!file_exists($_CacheUrl))

  62. {
  63. $hanld = @fopen($_CacheUrl,"w");
  64. @fclose ($hanld);
  65. }
  66. if(!file_exists($_CacheConfigUrl))

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

  72. $this->CacheFileUrl = $_CacheUrl;
  73. $this->$this- >CheckCache();
  74. return true;
  75. }
  76. private function CheckCache()

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

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

  87. $this->NextTimePoke = date("Y-m-d H:i:s",$_IsTimeOut);
  88. $this->CurrentTimePoke = date("Y-m-d H:i:s",time()) ;
  89. $_TxtInformation = "上次快取時間戳記: $this->LastUnixTimePoke ";

  90. $_TxtInformation .= "目前快取時間戳: $this->CurrentUnixTimePoke ";
  91. $_TxtInformation .= "下次快取時間戳: $this->NextUnixTimePoke ";
  92. $_TxtInformation .= "上次快取時間: $this->LastTimePoke ";

  93. $ _TxtInformation .= "目前快取時間: $this->CurrentTimePoke ";
  94. $_TxtInformation .= "下次快取時間: $this->NextTimePoke ";
  95. $_TxtInformation .= "距離下次快取戳記: $this->UnixNowToNext ";

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

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

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

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

  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('檢查快取檔案時產生錯誤,快取檔案建立失敗');
  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. UN);
  134. if(!$_PutData)
  135. {
  136. $this->ErrResponse('無法更改目前快取檔案內容');
  137. return false;
  138. } else {
  139. @fclose($hanld) ;
  140. return true;
  141. }
  142. } else {
  143. $this->ErrResponse('快取檔案不可寫');
  144. return false;
  145. }
  146. } else {
  147. $this->ErrResponse('開啟快取檔案發生致命錯誤');

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

  153. {
  154. $hanld = @fopen($this->CacheFileUrl,"r");
  155. if($hanld)
  156. {
  157. if( @is_readable($this->CacheFileUrl))
  158. {
  159. $this->CacheToPage = @file_get_contents($this->CacheFileUrl);
  160. $IsEmpty = count(file($this->CacheFileUrl); ; //判斷快取檔案是否為空
  161. if($IsEmpty > 0)

  162. {
  163. echo $this->CacheToPage;
  164. @fclose($hanld);
  165. ob_end_flush();
  166. exit();
  167. }
  168. } else {
  169. $this->ErrResponse('讀取快取檔案內容失敗');
  170. return false;
  171. }
  172. } else {
  173. $this->ErrResponse('開啟快取檔案失敗');
  174. return false;
  175. }
  176. }
  177. }
  178. }
  179. private function ErrResponse($Msg)

  180. {
echo $Msg;
}}?>
🎜>


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板