Redis.php 5.3 KB

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