redis 的依赖引用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
host: 192.168.8.108
port: 6379
timeout: 1000
database: 0
pool:
max-active: 15
max-idle: 8
min-idle: 2
max-wait: 20
1、作为缓存工具
@Resource
private RedisTemplate<String, String> redisTemplate;
@RequestMapping("/test")
public void test() {
redisTemplate.opsForValue().set("test123", "test123");
System.out.println(this.redisTemplate.opsForValue().get("test123"));
}
2、作为查询缓存
启动类上加上注解:@EnableCaching
@SpringBootApplication
@EnableCaching
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
具体调用类上加上注解:@Cacheable ,value:在redis中key
@Cacheable(value="findUser")
@Override
public List<User> findUser() {
System.out.println("========到数据库中查找======");
return userMapper.findUser() ;
}