javascript
SpringBoot2.x整合Redis实战 4节课
1、分布式緩存Redis介紹
???? 簡介:講解為什么要用緩存和介紹什么是Redis,新手練習工具
???? 1、redis官網 https://redis.io/download
????
???? 2、新手入門redis在線測試工具:http://try.redis.io/
?
2、源碼編譯安裝Redis4.x
???? 簡介:使用源碼安裝Redis4.x和配置外網訪問
??? 1、快速安裝? https://redis.io/download#installation
???????????? wget http://download.redis.io/releases/redis-4.0.9.tar.gz
???????????? tar xzf redis-4.0.9.tar.gz
???????????? cd redis-4.0.9
???????????? make
??????????? 啟動服務端:src/redis-server
???????????? 啟動客戶端:src/redis-cli
??? 2、默認是本地訪問的,需要開放外網訪問
???????? 1)打開redis.conf文件在NETWORK部分修改
??????????? 注釋掉bind 127.0.0.1可以使所有的ip訪問redis
??????????? 修改 protected-mode,值改為no
?
3、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=6379
???????????? # 連接超時時間 單位 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()
代碼示例:
RdisTestController.java:?
1 package net.xdclass.base_project.controller; 2 3 import net.xdclass.base_project.domain.JsonData; 4 5 import org.json.JSONObject; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.data.redis.core.StringRedisTemplate; 8 import org.springframework.web.bind.annotation.GetMapping; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 12 13 @RestController 14 @RequestMapping("/api/v1/redis") 15 public class RdisTestController { 16 17 18 @Autowired 19 private StringRedisTemplate redisTpl; //jdbcTemplate 20 21 @GetMapping(value="add") 22 public Object add(){ 23 24 //opsForValue : Returns the operations performed on simple values (or Strings in Redis terminology). 25 26 redisTpl.opsForValue().set("name", "xdclass2018"); 27 28 return JsonData.buildSuccess(); 29 30 } 31 32 @GetMapping(value="get") 33 public Object get(){ 34 35 String value = redisTpl.opsForValue().get("name"); 36 return JsonData.buildSuccess(value); 37 38 } 39 40 41 42 43 44 45 }4、Redis工具類封裝講解和實戰
???? 簡介:高效開發方式 Redis工具類封裝講解和實戰
???????? 1、常用客戶端 https://redisdesktop.com/download
???????? 2、封裝redis工具類并操作
裝入對象:
1 @GetMapping(value="save_user") 2 public Object saveUser(){ 3 User user = new User(1,"abc","11",new Date()); 4 //String value = redisTpl.opsForValue().get("name"); 5 String userStr = JsonUtils.obj2String(user); 6 boolean flag = redis.set("base:user:11", userStr); 7 return JsonData.buildSuccess(flag); 8 9 } 10 11 12 @GetMapping(value="find_user") 13 public Object findUser(){ 14 15 String userStr = redis.get("base:user:11"); 16 User user = JsonUtils.string2Obj(userStr, User.class); 17 18 return JsonData.buildSuccess(user); 19 20 } base:user:11該命名方法的內容在可視化工具中會以文件夾形式展示瀏覽器輸入:http://localhost:8080/api/v1/redis/save_user
?
取值:
瀏覽器輸入:http://localhost:8080/api/v1/redis/find_user
返回結果:
?
?
轉載于:https://www.cnblogs.com/116970u/p/10258705.html
總結
以上是生活随笔為你收集整理的SpringBoot2.x整合Redis实战 4节课的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置启动MySQL的Docker容器
- 下一篇: linux c/c++ 文件是否存在