当前位置:Gxlcms > PHP教程 > php中关于redis缓存类定义与使用详解

php中关于redis缓存类定义与使用详解

时间:2021-07-01 10:21:17 帮助过:7人阅读

本文实例讲述了php实现的redis缓存类定义与使用方法。分享给大家供大家参考,具体如下:

php+redis缓存类


  1. <?php
  2. class redisCache {
  3. /**
  4. * $host : redis服务器ip
  5. * $port : redis服务器端口
  6. * $lifetime : 缓存文件有效期,单位为秒
  7. * $cacheid : 缓存文件路径,包含文件名
  8. */
  9. private $host;
  10. private $port;
  11. private $lifetime;
  12. private $cacheid;
  13. private $data;
  14. public $redis;
  15. /**
  16. * 析构函数,检查缓存目录是否有效,默认赋值
  17. */
  18. function __construct($lifetime=1800) {
  19. //配置
  20. $this->host = "127.0.0.1";
  21. $this->port = "6379";
  22. $redis = new Redis();
  23. $redis->pconnect($this->host,$this->port);
  24. $this->redis=$redis;
  25. $this->cacheid = $this->getcacheid();
  26. $this->lifetime = $lifetime;
  27. $this->data=$redis->hMGet($this->cacheid, array('content','creattime'));
  28. //print_r($this->redis);
  29. //print_r($this->data);
  30. }
  31. /**
  32. * 检查缓存是否有效
  33. */
  34. private function isvalid(){
  35. $data=$this->data;
  36. if (!$data['content']) return false;
  37. if (time() - $data['creattime'] > $this->lifetime) return false;
  38. return true;
  39. }
  40. /**
  41. * 写入缓存
  42. * $mode == 0 , 以浏览器缓存的方式取得页面内容
  43. */
  44. public function write($mode=0,$content='') {
  45. switch ($mode) {
  46. case 0:
  47. $content = ob_get_contents();
  48. break;
  49. default:
  50. break;
  51. }
  52. ob_end_flush();
  53. try {
  54. $this->redis->hMset($this->cacheid, array('content'=>$content,'creattime'=>time()));
  55. $this->redis->expireAt($this->cacheid, time() + $this->lifetime);
  56. }
  57. catch (Exception $e) {
  58. $this->error('写入缓存失败!');
  59. }
  60. }
  61. /**
  62. * 加载缓存
  63. * exit() 载入缓存后终止原页面程序的执行,缓存无效则运行原页面程序生成缓存
  64. * ob_start() 开启浏览器缓存用于在页面结尾处取得页面内容
  65. */
  66. public function load() {
  67. if ($this->isvalid()) {
  68. echo $this->data['content'];
  69. exit();
  70. }
  71. else {
  72. ob_start();
  73. }
  74. }
  75. /**
  76. * 清除缓存
  77. */
  78. public function clean() {
  79. try {
  80. $this->redis->hDel($this->cacheid, array('content','creattime'));
  81. }
  82. catch (Exception $e) {
  83. $this->error('清除缓存失败!');
  84. }
  85. }
  86. /**
  87. * 取得缓存文件路径
  88. */
  89. private function getcacheid() {
  90. return $this->dir.md5($this->geturl()).$this->ext;
  91. }
  92. /**
  93. * 取得当前页面完整url
  94. */
  95. private function geturl() {
  96. $url = '';
  97. if (isset($_SERVER['REQUEST_URI'])) {
  98. $url = $_SERVER['REQUEST_URI'];
  99. }
  100. else {
  101. $url = $_SERVER['Php_SELF'];
  102. $url .= empty($_SERVER['QUERY_STRING'])?'':'?'.$_SERVER['QUERY_STRING'];
  103. }
  104. return $url;
  105. }
  106. /**
  107. *
输出错误信息 */ private function error($str) { echo '<p style="color:red;">'.$str.'</p>'; } } //用法: // require_once('redisCache.php'); // $cache = new redisCache(10); //设置缓存生存期 // if ($_GET['clearCache']) $cache->clean(); // else $cache->load(); //装载缓存,缓存有效则不执行以下页面代码 // //页面代码开始 // //页面代码结束 // $cache->write(); //首次运行或缓存过期,生成缓存 ?>

以上就是php中关于redis缓存类定义与使用详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行