port = "6379"; $redis = new Redis();">
当前位置:Gxlcms > PHP教程 > php+redis缓存类

php+redis缓存类

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

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

php, redis 本主题由 小贝 于 2015-11-12 08:43 移动

人气教程排行