Redis.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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) || false === $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|\DateTime $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 ($expire instanceof \DateTime) {
  96. $expire = $expire->getTimestamp() - time();
  97. }
  98. if ($this->tag && !$this->has($name)) {
  99. $first = true;
  100. }
  101. $key = $this->getCacheKey($name);
  102. //对数组/对象数据进行缓存处理,保证数据完整性 byron sampson<xiaobo.sun@qq.com>
  103. $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
  104. if (is_int($expire) && $expire) {
  105. $result = $this->handler->setex($key, $expire, $value);
  106. } else {
  107. $result = $this->handler->set($key, $value);
  108. }
  109. isset($first) && $this->setTagItem($key);
  110. return $result;
  111. }
  112. /**
  113. * 自增缓存(针对数值缓存)
  114. * @access public
  115. * @param string $name 缓存变量名
  116. * @param int $step 步长
  117. * @return false|int
  118. */
  119. public function inc($name, $step = 1)
  120. {
  121. $key = $this->getCacheKey($name);
  122. return $this->handler->incrby($key, $step);
  123. }
  124. /**
  125. * 自减缓存(针对数值缓存)
  126. * @access public
  127. * @param string $name 缓存变量名
  128. * @param int $step 步长
  129. * @return false|int
  130. */
  131. public function dec($name, $step = 1)
  132. {
  133. $key = $this->getCacheKey($name);
  134. return $this->handler->decrby($key, $step);
  135. }
  136. /**
  137. * 删除缓存
  138. * @access public
  139. * @param string $name 缓存变量名
  140. * @return boolean
  141. */
  142. public function rm($name)
  143. {
  144. return $this->handler->delete($this->getCacheKey($name));
  145. }
  146. /**
  147. * 清除缓存
  148. * @access public
  149. * @param string $tag 标签名
  150. * @return boolean
  151. */
  152. public function clear($tag = null)
  153. {
  154. if ($tag) {
  155. // 指定标签清除
  156. $keys = $this->getTagItem($tag);
  157. foreach ($keys as $key) {
  158. $this->handler->delete($key);
  159. }
  160. $this->rm('tag_' . md5($tag));
  161. return true;
  162. }
  163. return $this->handler->flushDB();
  164. }
  165. }