一个php缓存类与调用示例

php中文网
发布: 2016-07-25 08:59:20
原创
1363人浏览过
  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----

    存了个图
    存了个图

    视频图片解析/字幕/剪辑,视频高清保存/图片源图提取

    存了个图 17
    查看详情 存了个图
  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. ?>
复制代码


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号