Spring Boot 整合Redis
阿新 • • 發佈:2020-12-15
技術標籤:springBootspring bootredis
(1)新增redis的起步依賴
(2) 配置redis的連線資訊
spring:
artemis:
host: localhost
port: 6379
(3)注入RedisTemplate測試redis操作
@SpringBootTest
class Demo06redisApplicationTests {
@Autowired
RedisTemplate<String, String> rt;
@Test
void test01() {
//查詢是否有快取
String json = rt.boundValueOps("cache1").get();
//有就直接返回
if (json != null) {
System.out.println("來自快取:" + json);
} else {
String json_ = "{name:jack,age:13}";
//沒有就先存,再返回
rt.boundValueOps("cache1" ).set(json_);
System.out.println("儲存json " + json_);
}
}
}