日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot2.x整合redis实战讲解

發布時間:2024/4/13 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()

package net.leon.base_project.controller;import net.leon.base_project.domain.JsonData;import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/api/v1/redis") public class RdisTestController {@Autowiredprivate StringRedisTemplate redisTpl; //jdbcTemplate@GetMapping(value="add")public Object add(){//opsForValue : Returns the operations performed on simple values (or Strings in Redis terminology).redisTpl.opsForValue().set("name", "leon2018");return JsonData.buildSuccess();}@GetMapping(value="get")public Object get(){String value = redisTpl.opsForValue().get("name"); return JsonData.buildSuccess(value);} }

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实战讲解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。