Redis.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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. $this->handler = new \Redis;
  46. if ($this->options['persistent']) {
  47. $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  48. } else {
  49. $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
  50. }
  51. if ('' != $this->options['password']) {
  52. $this->handler->auth($this->options['password']);
  53. }
  54. if (0 != $this->options['select']) {
  55. $this->handler->select($this->options['select']);
  56. }
  57. }
  58. /**
  59. * 判断缓存
  60. * @access public
  61. * @param string $name 缓存变量名
  62. * @return bool
  63. */
  64. public function has($name)
  65. {
  66. return (bool) $this->handler->exists($this->getCacheKey($name));
  67. }
  68. /**
  69. * 读取缓存
  70. * @access public
  71. * @param string $name 缓存变量名
  72. * @param mixed $default 默认值
  73. * @return mixed
  74. */
  75. public function get($name, $default = false)
  76. {
  77. $value = $this->handler->get($this->getCacheKey($name));
  78. if (is_null($value) || false === $value) {
  79. return $default;
  80. }
  81. try {
  82. $result = 0 === strpos($value, 'think_serialize:') ? unserialize(substr($value, 16)) : $value;
  83. } catch (\Exception $e) {
  84. $result = $default;
  85. }
  86. return $result;
  87. }
  88. /**
  89. * 写入缓存
  90. * @access public
  91. * @param string $name 缓存变量名
  92. * @param mixed $value 存储数据
  93. * @param integer|\DateTime $expire 有效时间(秒)
  94. * @return boolean
  95. */
  96. public function set($name, $value, $expire = null)
  97. {
  98. if (is_null($expire)) {
  99. $expire = $this->options['expire'];
  100. }
  101. if ($expire instanceof \DateTime) {
  102. $expire = $expire->getTimestamp() - time();
  103. }
  104. if ($this->tag && !$this->has($name)) {
  105. $first = true;
  106. }
  107. $key = $this->getCacheKey($name);
  108. $value = is_scalar($value) ? $value : 'think_serialize:' . serialize($value);
  109. if ($expire) {
  110. $result = $this->handler->setex($key, $expire, $value);
  111. } else {
  112. $result = $this->handler->set($key, $value);
  113. }
  114. isset($first) && $this->setTagItem($key);
  115. return $result;
  116. }
  117. /**
  118. * 自增缓存(针对数值缓存)
  119. * @access public
  120. * @param string $name 缓存变量名
  121. * @param int $step 步长
  122. * @return false|int
  123. */
  124. public function inc($name, $step = 1)
  125. {
  126. $key = $this->getCacheKey($name);
  127. return $this->handler->incrby($key, $step);
  128. }
  129. /**
  130. * 自减缓存(针对数值缓存)
  131. * @access public
  132. * @param string $name 缓存变量名
  133. * @param int $step 步长
  134. * @return false|int
  135. */
  136. public function dec($name, $step = 1)
  137. {
  138. $key = $this->getCacheKey($name);
  139. return $this->handler->decrby($key, $step);
  140. }
  141. /**
  142. * 删除缓存
  143. * @access public
  144. * @param string $name 缓存变量名
  145. * @return boolean
  146. */
  147. public function rm($name)
  148. {
  149. return $this->handler->del($this->getCacheKey($name));
  150. }
  151. /**
  152. * 清除缓存
  153. * @access public
  154. * @param string $tag 标签名
  155. * @return boolean
  156. */
  157. public function clear($tag = null)
  158. {
  159. if ($tag) {
  160. // 指定标签清除
  161. $keys = $this->getTagItem($tag);
  162. foreach ($keys as $key) {
  163. $this->handler->del($key);
  164. }
  165. $this->rm('tag_' . md5($tag));
  166. return true;
  167. }
  168. return $this->handler->flushDB();
  169. }
  170. }