cari

  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. ?>
复制代码


Kenyataan
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Penalaan prestasi PHP untuk laman web trafik yang tinggiPenalaan prestasi PHP untuk laman web trafik yang tinggiMay 14, 2025 am 12:13 AM

Thesecrettokeepingaphp-poweredwebsiterunningsmoothlyunderheavyloadinVolvesserVeSkeystrategies: 1) pelaksanaanPodeCachingWithopCachetoreduceScriptexecutionTime, 2) UsedataBasequerycachingWnithSoRessendataBaBAboad, 3)

Suntikan Ketergantungan dalam PHP: Contoh Kod untuk PemulaSuntikan Ketergantungan dalam PHP: Contoh Kod untuk PemulaMay 14, 2025 am 12:08 AM

Anda harus mengambil berat tentang kebergantungan (DI) kerana ia menjadikan kod anda lebih jelas dan lebih mudah untuk dikekalkan. 1) Di menjadikannya lebih modular dengan decoupling kelas, 2) meningkatkan kemudahan ujian dan fleksibiliti kod, 3) menggunakan bekas DI untuk menguruskan kebergantungan kompleks, tetapi memberi perhatian kepada kesan prestasi dan kebergantungan bulat, 4) Amalan terbaik adalah bergantung kepada antara muka abstrak untuk mencapai gandingan longgar.

Prestasi PHP: Adakah mungkin untuk mengoptimumkan aplikasi?Prestasi PHP: Adakah mungkin untuk mengoptimumkan aplikasi?May 14, 2025 am 12:04 AM

Ya, OptimizingaphpapplicationIspossibleandessential.1) pelaksanaanCachingUsingAputeDeducedeDataBaseload.2) OptimisedataTabaseseseshithindexing, eficientqueries, danConnectionPooling.3) EnhancecodeWithBuilt-Infungsi, EveringGlobalVariables

Pengoptimuman Prestasi PHP: Panduan TerbaikPengoptimuman Prestasi PHP: Panduan TerbaikMay 14, 2025 am 12:02 AM

ThekeystrategiestoSignificLantantlyboostphpapplicationperformanceare: 1) useopcodecachinglikLikeopcachetoreduceExecutionTime, 2) OptimizedataBaseInteractionsWithPreparedStatementsandProperindexing, 3) ConfigureWebserverserverLikenginxWithPmforbetterShipter.

Kontena Suntikan Ketergantungan PHP: Permulaan yang cepatKontena Suntikan Ketergantungan PHP: Permulaan yang cepatMay 13, 2025 am 12:11 AM

AphpdependencyInjectionContainerisatoLthatMatagesClassDependencies, EnhancingCodeModularity, Testability, andMaintainability.itactsascentralHubforcreatingandinjectingdependencies, sheReducingTightCouplingandeaseaseaseSunittesting.

Suntikan ketergantungan berbanding pencari perkhidmatan di phpSuntikan ketergantungan berbanding pencari perkhidmatan di phpMay 13, 2025 am 12:10 AM

Pilih DependencyInjection (DI) Untuk aplikasi besar, servicelocator sesuai untuk projek kecil atau prototaip. 1) DI meningkatkan kesesuaian dan modulariti kod melalui suntikan pembina. 2) ServiceLocator memperoleh perkhidmatan melalui pendaftaran pusat, yang mudah tetapi boleh menyebabkan peningkatan gandingan kod.

Strategi Pengoptimuman Prestasi PHP.Strategi Pengoptimuman Prestasi PHP.May 13, 2025 am 12:06 AM

Phpapplicationscanbeoptimizedforspeedandeficiencyby: 1) enablingopcacheinphp.ini, 2) menggunakan preparedSwithpdofordatabasequeries, 3) menggantikanloopswitharray_filterandarray_mapfordataprocessing, 4) configuringnginywinginywinyvinyvinginy

Pengesahan E -mel PHP: Memastikan e -mel dihantar dengan betulPengesahan E -mel PHP: Memastikan e -mel dihantar dengan betulMay 13, 2025 am 12:06 AM

PhpeMailvalidationInvolvestHreesteps: 1) formatValidationingRegularExpressionStocheckTheemailFormat; 2) dnsvalidationtoensurethedomainhasavalidmxrecord;

See all articles

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Nordhold: Sistem Fusion, dijelaskan
4 minggu yang laluBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers of the Witch Tree - Cara Membuka Kunci Cangkuk Bergelut
3 minggu yang laluBy尊渡假赌尊渡假赌尊渡假赌

Alat panas

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

VSCode Windows 64-bit Muat Turun

VSCode Windows 64-bit Muat Turun

Editor IDE percuma dan berkuasa yang dilancarkan oleh Microsoft

PhpStorm versi Mac

PhpStorm versi Mac

Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).

Penyesuai Pelayan SAP NetWeaver untuk Eclipse

Penyesuai Pelayan SAP NetWeaver untuk Eclipse

Integrasikan Eclipse dengan pelayan aplikasi SAP NetWeaver.

Pelayar Peperiksaan Selamat

Pelayar Peperiksaan Selamat

Pelayar Peperiksaan Selamat ialah persekitaran pelayar selamat untuk mengambil peperiksaan dalam talian dengan selamat. Perisian ini menukar mana-mana komputer menjadi stesen kerja yang selamat. Ia mengawal akses kepada mana-mana utiliti dan menghalang pelajar daripada menggunakan sumber yang tidak dibenarkan.