php专区

 首页 > php专区 > PHP高级 > 缓存异常处理 > 自己写的一个PHP页面访问缓存类~

自己写的一个PHP页面访问缓存类~

分享到:
【字体:
导读:
         摘要:自己写的一个PHP页面访问缓存类~ ...

自己写的一个PHP页面访问缓存类~
  1. class Cache {
  2. private $dirCache; //缓存目录,用相对目录
  3. private $lifeTime; //缓存生存生存时间单位秒
  4. private $fileName; //缓存文件(包括相对路径)
  5. private $cacheFlg; //是否开启缓存
  6. private $fileExt; //缓存文件的扩展名
  7. public function __construct() {
  8. $this->dirCache = DIRTMP . '/webcache';
  9. $this->lifeTime = 0; //默认为60秒
  10. $this->cacheFlg = false; //默认为开启
  11. $this->fileExt = '.htm';
  12. $this->setFileName();
  13. }
  14. //获取缓存文件名
  15. private function setFileName() {
  16. $this->fileName = md5($_SERVER['REQUEST_URI']) . $this->fileExt;
  17. }
  18. //开始缓存
  19. public function startCache() {
  20. if ($this->cacheFlg) {
  21. if (file_exists($this->dirCache .
  22. '/' . $this->fileName) //检查文件是否存在
  23. && filesize($this->dirCache . '/' . $this->fileName) > 0 //检查文件大小即文件缓存是否保存成功
  24. && $this->checkCacheExpire() && $this->checkCacheLife()) {
  25. echo file_get_contents($this->dirCache . '/' . $this->fileName); //读取缓存内容
  26. $this->cacheFlg = false; //关闭生成缓存
  27. exit ();
  28. } else {
  29. ob_start(); //开始缓存
  30. }
  31. }
  32. }
  33. //检查被请求文件在缓存未过期时间内是否被修改
  34. private function checkCacheExpire() {
  35. if (filemtime($this->dirCache . '/' . $this->fileName) > filemtime($_SERVER['SCRIPT_FILENAME'])) {
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41. //检查缓存是否过期
  42. private function checkCacheLife() {
  43. if ($this->lifeTime == 0)
  44. return true;
  45. if (time() - filemtime($this->dirCache . '/' . $this->fileName) < $this->lifeTime) {
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. }
  51. //缓存页面结束
  52. public function endCache() {
  53. if ($this->cacheFlg && $this->makeDir()) {
  54. $contents = ob_get_contents(); //得到页面要缓存的内容
  55. ob_end_flush();
  56. if (!$this->createFile($contents)) {
  57. @ unlink($this->dirCache . '/' . $this->fileName);
  58. }
  59. }
  60. }
  61. //创建缓存文件
  62. private function createFile($contents) {
  63. $fp = @ fopen($this->dirCache . '/' . $this->fileName, 'w');
  64. if (@ fwrite($fp, $contents)) {
  65. @ fclose($fp);
  66. return true;
  67. } else {
  68. @ fclose($fp);
  69. return false;
  70. }
  71. }
  72. //创建缓存目录
  73. function makeDir() {
  74. if (file_exists($this->dirCache))
  75. return true;
  76. $dir = explode('/', str_replace("\", '/', $this->dirCache));
  77. $mdir = '';
  78. foreach ($dir as $val) {
  79. $mdir .= $val . "/";
  80. if ($val == '..' || $val == '.' || emptyempty($val))
  81. continue;
  82. if (!file_exists($mdir)) {
  83. if (!@ mkdir($mdir, '0777')) {
  84. return false;
  85. }
  86. }
  87. }
  88. return true;
  89. }
  90. //清除缓存
  91. function clearCache($getFile = '') {
  92. if (emptyempty($getFile)) {
  93. $getFile = $this->dirCache;
  94. }
  95. if (file_exists($getFile) && is_dir($getFile)) {
  96. $handle = opendir($getFile);
  97. while (false != $file = readdir($handle)) {
  98. if ($file == '..' || $file == '.' || emptyempty($file))
  99. continue;
  100. if (strrchr($file, '.') == $this->fileExt) {
  101. @ unlink($getFile . $file);
  102. }
  103. if (is_dir($getFile . $file)) {
  104. if (!@ rmdir($getFile . $file)) {
  105. $this->ClearCache($getFile . $file . '/');
  106. }
  107. }
  108. }
  109. closedir($handle);
  110. $result = @ rmdir($getFile);
  111. } else {
  112. $result = true;
  113.  
  114. }
  115. if (is_file($file))
  116. @ unlink($file);
  117. return $result;
  118. }
  119. }
  120. ?> 
自己写的一个PHP页面访问缓存类~
分享到:
PHP缓存技术的使用技巧分享
PHP缓存技术的使用技巧分享 php缓存技术的应用时相当普遍的,也许有些人还对这项技术不太了解,我们现在就为大家详细的介绍一下PHP缓存技术的相关应用技巧。 几款主流PHP框架的优缺点评比 总结PHP代码转义的相关方法 PHP取整函数的具体使用方法介绍 PHP外部变量的具体含义解析 PHP开发高效WEB系统的技巧讲解 在大...
Memcached深度分析(原创)
Memcached深度分析(原创) Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库 负载,提升性能。关于这个东西,相信很多人都用过,本文意在通过对memcached的实现及代码分析,获得对这个出色的开源软件更深入的了解,并可以根 据我们的需要对其进行更进一步...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……