引用
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>
启动类
@EnableCaching
cacheable
@RequestMapping("/hello")@Cacheable(value="helloCache")public String hello(String name) {System.out.println("没有走缓存!");return "hello "+name;}
- value:缓存的名称。
- key:缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写;如果不指定,则缺省按照方法的所有参数进行组合。
- condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持 SpEL。
@RequestMapping("/getUsers")@Cacheable(value="usersCache",key="#nickname",condition="#nickname.length() >= 6")public List<User> getUsers(String nickname) {List<User> users=userRepository.findByNickname(nickname);System.out.println("执行了数据库操作");return users;}
CachePut
更新@RequestMapping("/getPutUsers")@CachePut(value="usersCache",key="#nickname")public List<User> getPutUsers(String nickname) {List<User> users=userRepository.findByNickname(nickname);System.out.println("执行了数据库操作");return users;}
CacheEvict
清除缓存
