maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
注入:
@SpringBootApplication
@EnableCaching
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class, args);
}
}
写个controller进行清缓存
@RestController
public class APIController {
@Autowired
private CacheManager managerServer;
@DeleteMapping("/delete/cache")
public RestAPIResponse<Collection<String>> clean() {
try {
Set<String> names = (Set<String>) managerServer.getCacheNames();
names.forEach(e -> {
Cache c = managerServer.getCache(e);
if (Objects.isNull(c)) {
return;
}
c.clear();
});
return "clean cache list:" + managerServer.getCacheNames();
} catch (Exception e) {
log.error(e.getMessage(), e);
return "failed, reason:" + e.getMessage();
}
}
}