搜索
首页后端开发php教程再来一个缓存类

缓存类
  1. /*
  2. * 缓存类 cache
  3. * 作 者:多菜鸟
  4. * 创建时间:2006-05-05
  5. * 实 例:
  6. include( "cache.php" );
  7. $cache = new cache(30);
  8. $cache->cacheCheck();
  9. echo date("Y-m-d H:i:s");
  10. $cache->caching();
  11. */
  12. class cache {
  13. //缓存目录
  14. var $cacheRoot = "./cache/";
  15. //缓存更新时间秒数,0为不缓存
  16. var $cacheLimitTime = 0;
  17. //缓存文件名
  18. var $cacheFileName = "";
  19. //缓存扩展名
  20. var $cacheFileExt = "php";
  21. /*
  22. * 构造函数
  23. * int $cacheLimitTime 缓存更新时间
  24. */
  25. function cache( $cacheLimitTime ) {
  26. if( intval( $cacheLimitTime ) )
  27. $this->cacheLimitTime = $cacheLimitTime;
  28. $this->cacheFileName = $this->getCacheFileName();
  29. ob_start();
  30. }
  31. /*
  32. * 检查缓存文件是否在设置更新时间之内
  33. * 返回:如果在更新时间之内则返回文件内容,反之则返回失败
  34. */
  35. function cacheCheck(){
  36. if( file_exists( $this->cacheFileName ) ) {
  37. $cTime = $this->getFileCreateTime( $this->cacheFileName );
  38. if( $cTime + $this->cacheLimitTime > time() ) {
  39. echo file_get_contents( $this->cacheFileName );
  40. ob_end_flush();
  41. exit;
  42. }
  43. }
  44. return false;
  45. }
  46. /*
  47. * 缓存文件或者输出静态
  48. * string $staticFileName 静态文件名(含相对路径)
  49. */
  50. function caching( $staticFileName = "" ){
  51. if( $this->cacheFileName ) {
  52. $cacheContent = ob_get_contents();
  53. //echo $cacheContent;
  54. ob_end_flush();
  55. if( $staticFileName ) {
  56. $this->saveFile( $staticFileName, $cacheContent );
  57. }
  58. if( $this->cacheLimitTime )
  59. $this->saveFile( $this->cacheFileName, $cacheContent );
  60. }
  61. }
  62. /*
  63. * 清除缓存文件
  64. * string $fileName 指定文件名(含函数)或者all(全部)
  65. * 返回:清除成功返回true,反之返回false
  66. */
  67. function clearCache( $fileName = "all" ) {
  68. if( $fileName != "all" ) {
  69. $fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt;
  70. if( file_exists( $fileName ) ) {
  71. return @unlink( $fileName );
  72. }else return false;
  73. }
  74. if ( is_dir( $this->cacheRoot ) ) {
  75. if ( $dir = @opendir( $this->cacheRoot ) ) {
  76. while ( $file = @readdir( $dir ) ) {
  77. $check = is_dir( $file );
  78. if ( !$check )
  79. @unlink( $this->cacheRoot . $file );
  80. }
  81. @closedir( $dir );
  82. return true;
  83. }else{
  84. return false;
  85. }
  86. }else{
  87. return false;
  88. }
  89. }
  90. /*
  91. * 根据当前动态文件生成缓存文件名
  92. */
  93. function getCacheFileName() {
  94. return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
  95. }
  96. /*
  97. * 缓存文件建立时间
  98. * string $fileName 缓存文件名(含相对路径)
  99. * 返回:文件生成时间秒数,文件不存在返回0
  100. */
  101. function getFileCreateTime( $fileName ) {
  102. if( ! trim($fileName) ) return 0;
  103. if( file_exists( $fileName ) ) {
  104. return intval(filemtime( $fileName ));
  105. }else return 0;
  106. }
  107. /*
  108. * 保存文件
  109. * string $fileName 文件名(含相对路径)
  110. * string $text 文件内容
  111. * 返回:成功返回ture,失败返回false
  112. */
  113. function saveFile($fileName, $text) {
  114. if( ! $fileName || ! $text ) return false;
  115. if( $this->makeDir( dirname( $fileName ) ) ) {
  116. if( $fp = fopen( $fileName, "w" ) ) {
  117. if( @fwrite( $fp, $text ) ) {
  118. fclose($fp);
  119. return true;
  120. }else {
  121. fclose($fp);
  122. return false;
  123. }
  124. }
  125. }
  126. return false;
  127. }
  128. /*
  129. * 连续建目录
  130. * string $dir 目录字符串
  131. * int $mode 权限数字
  132. * 返回:顺利创建或者全部已建返回true,其它方式返回false
  133. */
  134. function makeDir( $dir, $mode = "0777" ) {
  135. if( ! $dir ) return 0;
  136. $dir = str_replace( "\\", "/", $dir );
  137. $mdir = "";
  138. foreach( explode( "/", $dir ) as $val ) {
  139. $mdir .= $val."/";
  140. if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
  141. if( ! file_exists( $mdir ) ) {
  142. if(!@mkdir( $mdir, $mode )){
  143. return false;
  144. }
  145. }
  146. }
  147. return true;
  148. }
  149. }
  150. ?>
复制代码


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境