You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.7 KiB

3 years ago
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Cache\Driver;
  12. use Think\Cache;
  13. defined('THINK_PATH') or exit();
  14. /**
  15. * Redis缓存驱动
  16. * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
  17. */
  18. class Redis extends Cache {
  19. /**
  20. * 架构函数
  21. * @param array $options 缓存参数
  22. * @access public
  23. */
  24. public function __construct($options=array()) {
  25. if ( !extension_loaded('redis') ) {
  26. E(L('_NOT_SUPPORT_').':redis');
  27. }
  28. $options = array_merge(array (
  29. 'host' => C('REDIS_HOST') ? : '127.0.0.1',
  30. 'port' => C('REDIS_PORT') ? : 6379,
  31. 'timeout' => C('DATA_CACHE_TIMEOUT') ? : false,
  32. 'persistent' => false,
  33. ),$options);
  34. $this->options = $options;
  35. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  36. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  37. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  38. $func = $options['persistent'] ? 'pconnect' : 'connect';
  39. $this->handler = new \Redis;
  40. $options['timeout'] === false ?
  41. $this->handler->$func($options['host'], $options['port']) :
  42. $this->handler->$func($options['host'], $options['port'], $options['timeout']);
  43. }
  44. /**
  45. * 读取缓存
  46. * @access public
  47. * @param string $name 缓存变量名
  48. * @return mixed
  49. */
  50. public function get($name) {
  51. N('cache_read',1);
  52. $value = $this->handler->get($this->options['prefix'].$name);
  53. $jsonData = json_decode( $value, true );
  54. return ($jsonData === NULL) ? $value : $jsonData; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
  55. }
  56. /**
  57. * 写入缓存
  58. * @access public
  59. * @param string $name 缓存变量名
  60. * @param mixed $value 存储数据
  61. * @param integer $expire 有效时间(秒)
  62. * @return boolean
  63. */
  64. public function set($name, $value, $expire = null) {
  65. N('cache_write',1);
  66. if(is_null($expire)) {
  67. $expire = $this->options['expire'];
  68. }
  69. $name = $this->options['prefix'].$name;
  70. //对数组/对象数据进行缓存处理,保证数据完整性
  71. $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
  72. if(is_int($expire) && $expire) {
  73. $result = $this->handler->setex($name, $expire, $value);
  74. }else{
  75. $result = $this->handler->set($name, $value);
  76. }
  77. if($result && $this->options['length']>0) {
  78. // 记录缓存队列
  79. $this->queue($name);
  80. }
  81. return $result;
  82. }
  83. /**
  84. * 删除缓存
  85. * @access public
  86. * @param string $name 缓存变量名
  87. * @return boolean
  88. */
  89. public function rm($name) {
  90. return $this->handler->delete($this->options['prefix'].$name);
  91. }
  92. /**
  93. * 清除缓存
  94. * @access public
  95. * @return boolean
  96. */
  97. public function clear() {
  98. return $this->handler->flushDB();
  99. }
  100. }