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_);
}
}
}