Redis.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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)) ? true : false;
  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 $tag 标签名
  168. * @return boolean
  169. */
  170. public function clear($tag = null)
  171. {
  172. if ($tag) {
  173. // 指定标签清除
  174. $keys = $this->getTagItem($tag);
  175. $this->handler->del($keys);
  176. $tagName = $this->getTagKey($tag);
  177. $this->handler->del($tagName);
  178. return true;
  179. }
  180. $this->writeTimes++;
  181. return $this->handler->flushDB();
  182. }
  183. /**
  184. * 缓存标签
  185. * @access public
  186. * @param string $name 标签名
  187. * @param string|array $keys 缓存标识
  188. * @param bool $overlay 是否覆盖
  189. * @return $this
  190. */
  191. public function tag($name, $keys = null, $overlay = false)
  192. {
  193. if (is_null($keys)) {
  194. $this->tag = $name;
  195. } else {
  196. $tagName = $this->getTagKey($name);
  197. if ($overlay) {
  198. $this->handler->del($tagName);
  199. }
  200. foreach ($keys as $key) {
  201. $this->handler->sAdd($tagName, $key);
  202. }
  203. }
  204. return $this;
  205. }
  206. /**
  207. * 更新标签
  208. * @access protected
  209. * @param string $name 缓存标识
  210. * @return void
  211. */
  212. protected function setTagItem($name)
  213. {
  214. if ($this->tag) {
  215. $tagName = $this->getTagKey($this->tag);
  216. $this->handler->sAdd($tagName, $name);
  217. }
  218. }
  219. /**
  220. * 获取标签包含的缓存标识
  221. * @access protected
  222. * @param string $tag 缓存标签
  223. * @return array
  224. */
  225. protected function getTagItem($tag)
  226. {
  227. $tagName = $this->getTagKey($tag);
  228. return $this->handler->sMembers($tagName);
  229. }
  230. }