Home > Backend Development > PHP Tutorial > php+redis cache class

php+redis cache class

WBOY
Release: 2016-07-25 08:46:29
Original
1025 people have browsed it
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. //Usage:
  113. // require_once('redisCache.php');
  114. // $cache = new redisCache(10); //Set cache lifetime
  115. // if ($ _GET['clearCache']) $cache->clean();
  116. // else $cache->load(); //Load cache. If the cache is valid, the following page code will not be executed.
  117. // //Start of page code
  118. // //End of page code
  119. // $cache->write(); // First run or cache expiration, generate cache
  120. ?>
Copy code

php, redis
This topic was moved by Xiaobei on 2015-11-12 08:43


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template