filecache.init.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 Dao-filecahce 文件缓存
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * $Author:zhuli
  10. * $Dtime:2013-5-29
  11. ***********************************************************************************/
  12. class filecacheInit {
  13. private $cache_path = '.'; //缓存路径
  14. /**
  15. * 文件缓存-设置缓存
  16. * 设置缓存名称,数据,和缓存时间
  17. * @param string $filename 缓存名
  18. * @param array $data 缓存数据
  19. */
  20. public function set_cache($filename, $data, $time = 0) {
  21. $filename = $this->get_cache_filename($filename);
  22. @file_put_contents($filename, '<?php exit;?>' . time() .'('.$time.')' . serialize($data));
  23. clearstatcache();
  24. return true;
  25. }
  26. /**
  27. * 文件缓存-获取缓存
  28. * 获取缓存文件,分离出缓存开始时间和缓存时间
  29. * 返回分离后的缓存数据,解序列化
  30. * @param string $filename 缓存名
  31. * @return array
  32. */
  33. public function get_cache($filename) {
  34. $filename = $this->get_cache_filename($filename);
  35. /* 缓存不存在的情况 */
  36. if (!file_exists($filename)) return false;
  37. $data = file_get_contents($filename); //获取缓存
  38. /* 缓存过期的情况 */
  39. $filetime = substr($data, 13, 10);
  40. $pos = strpos($data, ')');
  41. $cachetime = substr($data, 24, $pos - 24);
  42. $data = substr($data, $pos +1);
  43. if ($cachetime == 0) return @unserialize($data);
  44. if (time() > ($filetime + $cachetime)) {
  45. @unlink($filename);
  46. return false; //缓存过期
  47. }
  48. return @unserialize($data);
  49. }
  50. /**
  51. * 文件缓存-清除缓存
  52. * 删除缓存文件
  53. * @param string $filename 缓存名
  54. * @return array
  55. */
  56. public function clear($filename) {
  57. $filename = $this->get_cache_filename($filename);
  58. if (!file_exists($filename)) return true;
  59. @unlink($filename);
  60. return true;
  61. }
  62. /**
  63. * 文件缓存-清除全部缓存
  64. * 删除整个缓存文件夹文件,一般情况下不建议使用
  65. * @param string $filename 缓存名
  66. * @return array
  67. */
  68. public function clear_all() {
  69. @set_time_limit(3600);
  70. $path = opendir($this->cache_path);
  71. while (false !== ($filename = readdir($path))) {
  72. if ($filename !== '.' && $filename !== '..') {
  73. @unlink($this->cache_path . '/' .$filename);
  74. }
  75. }
  76. closedir($path);
  77. return true;
  78. }
  79. /**
  80. * 设置文件缓存路径
  81. * 在配置文件中配置了该缓存文件
  82. * $InitPHP_conf['cache']['filepath'] = 'data/filecache';
  83. * @param string $path 路径
  84. * @return string
  85. */
  86. public function set_cache_path($path) {
  87. return $this->cache_path = $path;
  88. }
  89. /**
  90. * 获取缓存文件名
  91. * @param string $filename 缓存名
  92. * @return string
  93. */
  94. private function get_cache_filename($filename) {
  95. $filename = md5($filename); //文件名MD5加密
  96. $filename = $this->cache_path .'/'. $filename . '.php';
  97. return $filename;
  98. }
  99. }