javascript
SpringBoot2.x整合redis实战讲解
SpringBoot2.x整合redis實戰講解
?? ?簡介:使用springboot-starter整合reids實戰
?? ??? ?1、官網:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-redis
?? ??? ??? ?集群文檔:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#cluster
?? ??? ?2、springboot整合redis相關依賴引入
?? ??? ??? ?<dependency>
?? ? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
?? ? ? ? ? ? ? ?<artifactId>spring-boot-starter-data-redis</artifactId>
?? ? ? ? ? ?</dependency>
?? ? ? ?
?? ? ? ?3、相關配置文件配置
?? ??? ??? ?#=========redis基礎配置=========
?? ??? ??? ?spring.redis.database=0
?? ??? ??? ?spring.redis.host=127.0.0.1
?? ??? ??? ?spring.redis.port=6390
?? ??? ??? ?# 連接超時時間 單位 ms(毫秒)
?? ??? ??? ?spring.redis.timeout=3000
?? ??? ??? ?#=========redis線程池設置=========
?? ??? ??? ?# 連接池中的最大空閑連接,默認值也是8。
?? ??? ??? ?spring.redis.pool.max-idle=200
?? ??? ??? ?#連接池中的最小空閑連接,默認值也是0。
?? ??? ??? ?spring.redis.pool.min-idle=200
?? ??? ??? ?
?? ??? ??? ?# 如果賦值為-1,則表示不限制;pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。
?? ??? ??? ?spring.redis.pool.max-active=2000
?? ??? ??? ?# 等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時
?? ??? ??? ?spring.redis.pool.max-wait=1000
?? ??? ?4、常見redistemplate種類講解和緩存實操(使用自動注入)
?? ??? ??? ?1、注入模板
?? ??? ??? ?@Autowired
?? ??? ??? ?private StirngRedisTemplate strTplRedis
?? ??? ??? ?2、類型String,List,Hash,Set,ZSet
?? ??? ??? ?對應的方法分別是opsForValue()、opsForList()、opsForHash()、opsForSet()、opsForZSet()
Redis
Redis?is a cache, message broker, and richly-featured key-value store. Spring Boot offers basic auto-configuration for the?Lettuce?and?Jedis?client libraries and the abstractions on top of them provided by?Spring Data Redis.
There is a?spring-boot-starter-data-redis?“Starter” for collecting the dependencies in a convenient way. By default, it uses?Lettuce. That starter handles both traditional and reactive applications.
| we also provide a?spring-boot-starter-data-redis-reactive?“Starter” for consistency with the other stores with reactive support. |
Connecting to Redis
You can inject an auto-configured?RedisConnectionFactory,?StringRedisTemplate, or vanilla?RedisTemplate?instance as you would any other Spring Bean. By default, the instance tries to connect to a Redis server at?localhost:6379. The following listing shows an example of such a bean:
@Component public class MyBean {private StringRedisTemplate template;@Autowiredpublic MyBean(StringRedisTemplate template) {this.template = template;}// ...}| You can also register an arbitrary number of beans that implement?LettuceClientConfigurationBuilderCustomizer?for more advanced customizations. If you use Jedis,?JedisClientConfigurationBuilderCustomizer?is also available. |
If you add your own?@Bean?of any of the auto-configured types, it replaces the default (except in the case of?RedisTemplate, when the exclusion is based on the bean name,?redisTemplate, not its type). By default, if?commons-pool2?is on the classpath, you get a pooled connection factory.
Maven configuration
Add the Maven dependency:
<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>${version}.RELEASE</version> </dependency>If you’d rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.
package net.leon.base_project.domain;import java.io.Serializable;/*** 功能描述:響應結果類*/ public class JsonData implements Serializable {/*** */private static final long serialVersionUID = 1L;private Integer code; // 狀態碼 0 表示成功,1表示處理中,-1表示失敗private Object data; // 數據private String msg;// 描述public JsonData() {}public JsonData(Integer code, Object data, String msg) {this.code = code;this.data = data;this.msg = msg;}// 成功,傳入數據public static JsonData buildSuccess() {return new JsonData(0, null, null);}// 成功,傳入數據public static JsonData buildSuccess(Object data) {return new JsonData(0, data, null);}// 失敗,傳入描述信息public static JsonData buildError(String msg) {return new JsonData(-1, null, msg);}// 失敗,傳入描述信息,狀態碼public static JsonData buildError(String msg, Integer code) {return new JsonData(code, null, msg);}// 成功,傳入數據,及描述信息public static JsonData buildSuccess(Object data, String msg) {return new JsonData(0, data, msg);}// 成功,傳入數據,及狀態碼public static JsonData buildSuccess(Object data, int code) {return new JsonData(code, data, null);}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}@Overridepublic String toString() {return "JsonData [code=" + code + ", data=" + data + ", msg=" + msg+ "]";}}?
總結
以上是生活随笔為你收集整理的SpringBoot2.x整合redis实战讲解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分布式缓存Redis介绍
- 下一篇: Redis工具类封装讲解和实战