Redis.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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\Driver;
  13. /**
  14. * Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
  15. * 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
  16. *
  17. * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
  18. * @author 尘缘 <130775@qq.com>
  19. */
  20. class Redis extends Driver
  21. {
  22. protected $options = [
  23. 'host' => '127.0.0.1',
  24. 'port' => 6379,
  25. 'password' => '',
  26. 'select' => 0,
  27. 'timeout' => 0,
  28. 'expire' => 0,
  29. 'persistent' => false,
  30. 'prefix' => '',
  31. ];
  32. /**
  33. * 构造函数
  34. * @param array $options 缓存参数
  35. * @access public
  36. */
  37. public function __construct($options = [])
  38. {
  39. if (!extension_loaded('redis')) {
  40. throw new \BadFunctionCallException('not support: redis');
  41. }
  42. if (!empty($options)) {
  43. $this->options = array_merge($this->options, $options);
  44. }
  45. $func = $this->options['persistent'] ? 'pconnect' : 'connect';
  46. $this->handler = new \Redis;
  47. $this->handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);
  48. if ('' != $this->options['password']) {
  49. $this->handler->auth($this->options['password']);
  50. }
  51. if (0 != $this->options['select']) {
  52. $this->handler->select($this->options['select']);
  53. }
  54. }
  55. /**
  56. * 判断缓存
  57. * @access public
  58. * @param string $name 缓存变量名
  59. * @return bool
  60. */
  61. public function has($name)
  62. {
  63. return $this->handler->get($this->getCacheKey($name)) ? true : false;
  64. }
  65. /**
  66. * 读取缓存
  67. * @access public
  68. * @param string $name 缓存变量名
  69. * @param mixed $default 默认值
  70. * @return mixed
  71. */
  72. public function get($name, $default = false)
  73. {
  74. $value = $this->handler->get($this->getCacheKey($name));
  75. if (is_null($value)) {
  76. return $default;
  77. }
  78. $jsonData = json_decode($value, true);
  79. // 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 byron sampson<xiaobo.sun@qq.com>
  80. return (null === $jsonData) ? $value : $jsonData;
  81. }
  82. /**
  83. * 写入缓存
  84. * @access public
  85. * @param string $name 缓存变量名
  86. * @param mixed $value 存储数据
  87. * @param integer $expire 有效时间(秒)
  88. * @return boolean
  89. */
  90. public function set($name, $value, $expire = null)
  91. {
  92. if (is_null($expire)) {
  93. $expire = $this->options['expire'];
  94. }
  95. if ($this->tag && !$this->has($name)) {
  96. $first = true;
  97. }
  98. $key = $this->getCacheKey($name);
  99. //对数组/对象数据进行缓存处理,保证数据完整性 byron sampson<xiaobo.sun@qq.com>
  100. $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
  101. if (is_int($expire) && $expire) {
  102. $result = $this->handler->setex($key, $expire, $value);
  103. } else {
  104. $result = $this->handler->set($key, $value);
  105. }
  106. isset($first) && $this->setTagItem($key);
  107. return $result;
  108. }
  109. /**
  110. * 自增缓存(针对数值缓存)
  111. * @access public
  112. * @param string $name 缓存变量名
  113. * @param int $step 步长
  114. * @return false|int
  115. */
  116. public function inc($name, $step = 1)
  117. {
  118. $key = $this->getCacheKey($name);
  119. return $this->handler->incrby($key, $step);
  120. }
  121. /**
  122. * 自减缓存(针对数值缓存)
  123. * @access public
  124. * @param string $name 缓存变量名
  125. * @param int $step 步长
  126. * @return false|int
  127. */
  128. public function dec($name, $step = 1)
  129. {
  130. $key = $this->getCacheKey($name);
  131. return $this->handler->decrby($key, $step);
  132. }
  133. /**
  134. * 删除缓存
  135. * @access public
  136. * @param string $name 缓存变量名
  137. * @return boolean
  138. */
  139. public function rm($name)
  140. {
  141. return $this->handler->delete($this->getCacheKey($name));
  142. }
  143. /**
  144. * 清除缓存
  145. * @access public
  146. * @param string $tag 标签名
  147. * @return boolean
  148. */
  149. public function clear($tag = null)
  150. {
  151. if ($tag) {
  152. // 指定标签清除
  153. $keys = $this->getTagItem($tag);
  154. foreach ($keys as $key) {
  155. $this->handler->delete($key);
  156. }
  157. $this->rm('tag_' . md5($tag));
  158. return true;
  159. }
  160. return $this->handler->flushDB();
  161. }
  162. }