PHP 페이지 캐싱 클래스

WBOY
풀어 주다: 2016-07-25 09:10:33
원래의
1053명이 탐색했습니다.
  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; 지금과 다음 캐시*/

  15. public $LastTimePoke = 0; /* 마지막 캐시 시간*/

  16. public $ CurrentTimePoke = 0;/* 현재 캐시된 시간 */
  17. public $NextTimePoke = 0; /* 다음 캐시 시간*/

  18. public $DataLength = 0; /* 캐시 영역 콘텐츠 길이*/

  19. public $ CacheToPage = ''; /* 캐시 파일 내용*/
  20. private $SplitTeam = false /* 캐시 파일을 그룹으로 저장할지 여부*/

  21. < ;p>public $Cache = false; /* 캐싱이 필요한지 여부는 사용자가 외부에서 판단할 수 있습니다*/

  22. private $_IsCache = false /* 캐싱이 가능한지 여부*/

    p>공용 함수 Cache($SetTimeOut = 20,$CacheDir = 'Cache',$SplitTeam = false,$SetExt = '.cache')
  23. {
  24. $this->CacheDir = $CacheDir;
  25. $this->SplitTeam = $SplitTeam;
  26. if(!is_numeric($SetTimeOut))
  27. {
  28. $this->ErrResponse('캐시 만료 시간 설정이 잘못되었습니다.') ;
  29. false 반환;
  30. } else {
  31. $this->SetTimeOut = $SetTimeOut;
  32. }
  33. $this->SetExt = $SetExt;

  34. < p>/* 캐시 시작*/
  35. ob_clean();
  36. ob_start();
  37. ob_implicit_flush(0);
  38. $this->CreateCache();
  39. true 반환 ;
  40. }

  41. 비공개 함수 CreateCache()

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

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

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

  50. if($this->SplitTeam)

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

  63. < p>if(!file_exists($_CacheUrl))
  64. {
  65. $hanld = @fopen($_CacheUrl,"w");
  66. @fclose($hanld);
  67. }< ;/p> ;
  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. true를 반환합니다.
  77. }
  78. 비공개 함수 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("연월일 H:i:s",$_FileEditTime);

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

  90. $_TxtInformation = "마지막 캐시 타임스탬프: $this->LastUnixTimePoke ";

  91. $_TxtInformation .= "현재 캐시 타임스탬프: $this->CurrentUnixTimePoke ";
  92. $_TxtInformation .= "다음 캐시 타임스탬프: $this->NextUnixTimePoke ";

  93. < p>$_TxtInformation . = "마지막 캐시 시간: $this->LastTimePoke ";
  94. $_TxtInformation .= "현재 캐시 시간: $this->CurrentTimePoke ";
  95. $_TxtInformation .= "다음 캐시 시간: $this- >NextTimePoke ";

  96. $_TxtInformation .= "다음 캐시 스탬프: $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. }
  108. 비공개 함수 ClearCacheFile()

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

  113. 공용 함수 PutCache()

  114. {
  115. $this->DataLength = ob_get_length();
  116. $ PutData = ob_get_contents();
  117. if(!file_exists($this->CacheFileUrl))
  118. {
  119. $CreateOK = $this->CreateCache();
  120. if(!$CreateOK)
  121. {
  122. $ this->ErrResponse('캐시 파일을 확인하는 중 오류가 발생하여 캐시 파일 생성에 실패했습니다');
  123. return false;
  124. }
  125. } else {
  126. $hanld = @fopen($ this->CacheFileUrl,"w");
  127. if($hanld)
  128. {

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

  130. {
  131. @flock($hanld, LOCK_EX);
  132. $_PutData = @fwrite($hanld,$PutData);
  133. @flock($hanld, LOCK_UN);
  134. if(!$_PutData )
  135. {
  136. $this->ErrResponse('현재 캐시 파일의 내용을 변경할 수 없습니다.');
  137. return false;
  138. } else {
  139. @fclose($hanld );
  140. true를 반환;
  141. }
  142. } else {
  143. $this->ErrResponse('캐시 파일을 쓸 수 없습니다.');
  144. false를 반환;
  145. }
  146. } else {

  147. $this->ErrResponse('캐시 파일을 열 때 치명적인 오류가 발생했습니다.');

  148. return false;
  149. }
  150. }
  151. }< ;/p>
  152. 공용 함수 GetCacheData()

  153. {
  154. $hanld = @fopen($this->CacheFileUrl,"r");
  155. if($hanld)
  156. {
  157. if(@is_reader($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. 비공개 함수 ErrResponse($Msg)

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

코드 복사


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!