Rumah  >  Artikel  >  pembangunan bahagian belakang  >  一个php缓存类与调用示例

一个php缓存类与调用示例

WBOY
WBOYasal
2016-07-25 08:59:201110semak imbas
  1. class Cache {
  2. /**
  3. * $dir : 缓存文件存放目录
  4. * $lifetime : 缓存文件有效期,单位为秒
  5. * $cacheid : 缓存文件路径,包含文件名
  6. * $ext : 缓存文件扩展名(可以不用),这里使用是为了查看文件方便
  7. */
  8. private $dir;
  9. private $lifetime;
  10. private $cacheid;
  11. private $ext;
  12. /**
  13. * 析构函数,检查缓存目录是否有效,默认赋值
  14. */
  15. function __construct($dir='',$lifetime=1800) {
  16. if ($this->dir_isvalid($dir)) {
  17. $this->dir = $dir;
  18. $this->lifetime = $lifetime;
  19. $this->ext = '.Php';
  20. $this->cacheid = $this->getcacheid();
  21. }
  22. }
  23. /**
  24. * 检查缓存是否有效
  25. * edit bbs.it-home.org
  26. */
  27. private function isvalid() {
  28. if (!file_exists($this->cacheid)) return false;
  29. if (!(@$mtime = filemtime($this->cacheid))) return false;
  30. if (mktime() - $mtime > $this->lifetime) return false;
  31. return true;
  32. }
  33. /**
  34. * 写入缓存
  35. * $mode == 0 , 以浏览器缓存的方式取得页面内容
  36. * $mode == 1 , 以直接赋值(通过$content参数接收)的方式取得页面内容
  37. * $mode == 2 , 以本地读取(fopen ile_get_contents)的方式取得页面内容(似乎这种方式没什么必要)
  38. */
  39. public function write($mode=0,$content='') {
  40. switch ($mode) {
  41. case 0:
  42. $content = ob_get_contents();
  43. break;
  44. default:
  45. break;
  46. }
  47. ob_end_flush();
  48. try {
  49. file_put_contents($this->cacheid,$content);
  50. }
  51. catch (Exception $e) {
  52. $this->error('写入缓存失败!请检查目录权限!');
  53. }
  54. }
  55. /**
  56. * 加载缓存
  57. * exit() 载入缓存后终止原页面程序的执行,缓存无效则运行原页面程序生成缓存
  58. * ob_start() 开启浏览器缓存用于在页面结尾处取得页面内容
  59. */
  60. public function load() {
  61. if ($this->isvalid()) {
  62. echo "This is Cache. ";
  63. //以下两种方式,哪种方式好?????
  64. require_once($this->cacheid);
  65. //echo file_get_contents($this->cacheid);
  66. exit();
  67. }
  68. else {
  69. ob_start();
  70. }
  71. }
  72. /**
  73. * 清除缓存
  74. */
  75. public function clean() {
  76. try {
  77. unlink($this->cacheid);
  78. }
  79. catch (Exception $e) {
  80. $this->error('清除缓存文件失败!请检查目录权限!');
  81. }
  82. }
  83. /**
  84. * 取得缓存文件路径
  85. */
  86. private function getcacheid() {
  87. return $this->dir.md5($this->geturl()).$this->ext;
  88. }
  89. /**
  90. * 检查目录是否存在或是否可创建
  91. */
  92. private function dir_isvalid($dir) {
  93. if (is_dir($dir)) return true;
  94. try {
  95. mkdir($dir,0777);
  96. }
  97. catch (Exception $e) {
  98. $this->error('所设定缓存目录不存在并且创建失败!请检查目录权限!');
  99. return false;
  100. }
  101. return true;
  102. }
  103. /**
  104. * 取得当前页面完整url
  105. */
  106. private function geturl() {
  107. $url = '';
  108. if (isset($_SERVER['REQUEST_URI'])) {
  109. $url = $_SERVER['REQUEST_URI'];
  110. }
  111. else {
  112. $url = $_SERVER['Php_SELF'];
  113. $url .= empty($_SERVER['QUERY_STRING'])?'':'?'.$_SERVER['QUERY_STRING'];
  114. }
  115. return $url;
  116. }
  117. /**
  118. * 输出错误信息
  119. */
  120. private function error($str) {
  121. echo '
    '.$str.'
    ';
  122. }
  123. }
  124. ?>
复制代码

2,php缓存类的演示代码

  1. /*

  2. * 可自由转载使用,请保留版权信息,谢谢使用!
  3. * Class Name : Cache (For Php5)
  4. * Version : 1.0
  5. * Description : 动态缓存类,用于控制页面自动生成缓存、调用缓存、更新缓存、删除缓存.
  6. * Remark :
  7. 1.此版本为Php5版本
  8. 2.此版本为utf-8编码,如果网站采用其它编码请自行转换,Windows系统用记事本打开另存为,选择相应编码即可(一般ANSI),Linux下请使用相应编辑软件或iconv命令行.
  9. 3.拷贝粘贴的就不用管上面第2条了.
  10. * 关于缓存的一点感想:
  11. * 动态缓存和静态缓存的根本差别在于其是自动的,用户访问页面过程就是生成缓存、浏览缓存、更新缓存的过程,无需人工操作干预.
  12. * 静态缓存指的就是生成静态页面,相关操作一般是在网站后台完成,需人工操作(也就是手动生成).
  13. */
  14. /*
  15. * 使用方法举例
  16. ----------Demo1--------
  17. require_once('cache.inc.php');
  18. $cachedir = './Cache/'; //设定缓存目录
  19. $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
  20. if ($_GET['cacheact'] != 'rewrite') //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存
  21. $cache->load(); //装载缓存,缓存有效则不执行以下页面代码
  22. //页面代码开始
  23. echo date('H:i:s jS F');
  24. //页面代码结束
  25. $cache->write(); //首次运行或缓存过期,生成缓存
  26. ---------Demo2-------

  27. require_once('cache.inc.php');
  28. $cachedir = './Cache/'; //设定缓存目录
  29. $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
  30. if ($_GET['cacheact'] != 'rewrite') //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存
  31. $cache->load(); //装载缓存,缓存有效则不执行以下页面代码
  32. //页面代码开始
  33. $content = date('H:i:s jS F');
  34. echo $content;
  35. //页面代码结束
  36. $cache->write(1,$content); //首次运行或缓存过期,生成缓存
  37. ------------Demo3----

  38. require_once('cache.inc.php');
  39. define('CACHEENABLE',true);
  40. if (CACHEENABLE) {
  41. $cachedir = './Cache/'; //设定缓存目录
  42. $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
  43. if ($_GET['cacheact'] != 'rewrite') //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存
  44. $cache->load(); //装载缓存,缓存有效则不执行以下页面代码
  45. }
  46. //页面代码开始
  47. $content = date('H:i:s jS F');
  48. echo $content;
  49. //页面代码结束
  50. if (CACHEENABLE)
  51. $cache->write(1,$content); //首次运行或缓存过期,生成缓存
  52. */
  53. ?>
复制代码


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