Redis.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. 'serialize' => true,
  32. ];
  33. /**
  34. * 架构函数
  35. * @access public
  36. * @param array $options 缓存参数
  37. */
  38. public function __construct($options = [])
  39. {
  40. if (!empty($options)) {
  41. $this->options = array_merge($this->options, $options);
  42. }
  43. if (extension_loaded('redis')) {
  44. $this->handler = new \Redis;
  45. if ($this->options['persistent']) {
  46. $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  47. } else {
  48. $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
  49. }
  50. if ('' != $this->options['password']) {
  51. $this->handler->auth($this->options['password']);
  52. }
  53. if (0 != $this->options['select']) {
  54. $this->handler->select($this->options['select']);
  55. }
  56. } elseif (class_exists('\Predis\Client')) {
  57. $params = [];
  58. foreach ($this->options as $key => $val) {
  59. if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
  60. $params[$key] = $val;
  61. unset($this->options[$key]);
  62. }
  63. }
  64. if ('' == $this->options['password']) {
  65. unset($this->options['password']);
  66. }
  67. $this->handler = new \Predis\Client($this->options, $params);
  68. $this->options['prefix'] = '';
  69. } else {
  70. throw new \BadFunctionCallException('not support: redis');
  71. }
  72. }
  73. /**
  74. * 判断缓存
  75. * @access public
  76. * @param string $name 缓存变量名
  77. * @return bool
  78. */
  79. public function has($name)
  80. {
  81. return $this->handler->exists($this->getCacheKey($name));
  82. }
  83. /**
  84. * 读取缓存
  85. * @access public
  86. * @param string $name 缓存变量名
  87. * @param mixed $default 默认值
  88. * @return mixed
  89. */
  90. public function get($name, $default = false)
  91. {
  92. $this->readTimes++;
  93. $value = $this->handler->get($this->getCacheKey($name));
  94. if (is_null($value) || false === $value) {
  95. return $default;
  96. }
  97. return $this->unserialize($value);
  98. }
  99. /**
  100. * 写入缓存
  101. * @access public
  102. * @param string $name 缓存变量名
  103. * @param mixed $value 存储数据
  104. * @param integer|\DateTime $expire 有效时间(秒)
  105. * @return boolean
  106. */
  107. public function set($name, $value, $expire = null)
  108. {
  109. $this->writeTimes++;
  110. if (is_null($expire)) {
  111. $expire = $this->options['expire'];
  112. }
  113. if ($this->tag && !$this->has($name)) {
  114. $first = true;
  115. }
  116. $key = $this->getCacheKey($name);
  117. $expire = $this->getExpireTime($expire);
  118. $value = $this->serialize($value);
  119. if ($expire) {
  120. $result = $this->handler->setex($key, $expire, $value);
  121. } else {
  122. $result = $this->handler->set($key, $value);
  123. }
  124. isset($first) && $this->setTagItem($key);
  125. return $result;
  126. }
  127. /**
  128. * 自增缓存(针对数值缓存)
  129. * @access public
  130. * @param string $name 缓存变量名
  131. * @param int $step 步长
  132. * @return false|int
  133. */
  134. public function inc($name, $step = 1)
  135. {
  136. $this->writeTimes++;
  137. $key = $this->getCacheKey($name);
  138. return $this->handler->incrby($key, $step);
  139. }
  140. /**
  141. * 自减缓存(针对数值缓存)
  142. * @access public
  143. * @param string $name 缓存变量名
  144. * @param int $step 步长
  145. * @return false|int
  146. */
  147. public function dec($name, $step = 1)
  148. {
  149. $this->writeTimes++;
  150. $key = $this->getCacheKey($name);
  151. return $this->handler->decrby($key, $step);
  152. }
  153. /**
  154. * 删除缓存
  155. * @access public
  156. * @param string $name 缓存变量名
  157. * @return boolean
  158. */
  159. public function rm($name)
  160. {
  161. $this->writeTimes++;
  162. return $this->handler->del($this->getCacheKey($name));
  163. }
  164. /**
  165. * 批量删除缓存
  166. * @access public
  167. * @param string $name 缓存变量名
  168. * @return boolean
  169. */
  170. public function rms($name)
  171. {
  172. if(strpos($name, '*') !== false){
  173. $key = $this->handler->keys($this->getCacheKey($name));
  174. }else{
  175. $key = $this->getCacheKey($name);
  176. }
  177. return $this->handler->delete($key);
  178. }
  179. /**
  180. * 清除缓存
  181. * @access public
  182. * @param string $tag 标签名
  183. * @return boolean
  184. */
  185. public function clear($tag = null)
  186. {
  187. if ($tag) {
  188. // 指定标签清除
  189. $keys = $this->getTagItem($tag);
  190. $this->handler->del($keys);
  191. $tagName = $this->getTagKey($tag);
  192. $this->handler->del($tagName);
  193. return true;
  194. }
  195. $this->writeTimes++;
  196. return $this->handler->flushDB();
  197. }
  198. /**
  199. * 缓存标签
  200. * @access public
  201. * @param string $name 标签名
  202. * @param string|array $keys 缓存标识
  203. * @param bool $overlay 是否覆盖
  204. * @return $this
  205. */
  206. public function tag($name, $keys = null, $overlay = false)
  207. {
  208. if (is_null($keys)) {
  209. $this->tag = $name;
  210. } else {
  211. $tagName = $this->getTagKey($name);
  212. if ($overlay) {
  213. $this->handler->del($tagName);
  214. }
  215. foreach ($keys as $key) {
  216. $this->handler->sAdd($tagName, $key);
  217. }
  218. }
  219. return $this;
  220. }
  221. /**
  222. * 更新标签
  223. * @access protected
  224. * @param string $name 缓存标识
  225. * @return void
  226. */
  227. protected function setTagItem($name)
  228. {
  229. if ($this->tag) {
  230. $tagName = $this->getTagKey($this->tag);
  231. $this->handler->sAdd($tagName, $name);
  232. }
  233. }
  234. /**
  235. * 获取标签包含的缓存标识
  236. * @access protected
  237. * @param string $tag 缓存标签
  238. * @return array
  239. */
  240. protected function getTagItem($tag)
  241. {
  242. $tagName = $this->getTagKey($tag);
  243. return $this->handler->sMembers($tagName);
  244. }
  245. }