SpringBoot整合Redis

Spring Boot 集成Redis
(1)添加redis的起步依赖
(2) 配置redis的连接信息
spring.redis.host=localhost
spring.redis.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_);
}
}
}
本文介绍了Spring Boot整合Redis的相关内容,包括Spring Boot集成Redis的步骤,如添加redis的起步依赖、配置redis的连接信息,以及注入RedisTemplate测试redis操作。
4万+

被折叠的 条评论
为什么被折叠?



