暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Redis.php 5.8KB

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