Spring Boot 集成Redis
(1)添加redis的起步依赖
(2) 配置redis的连接信息
src\main\resources\application.properties
spring.redis.host=localhost
spring.redis.port=6379
创建
(1)勾选Redis添加依赖
(2)打开Redis服务器
(3)数据模拟查找数据
@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_);
}
}
}
(4)运行结果
-
第一次运行结果
-
多次运行结果(2、3、。。。)