【发布时间】:2020-08-27 21:49:46
【问题描述】:
我有一个 springboot 应用程序,我在一个方法上应用了缓存来缓存我的结果集。 这适用于调度程序和对 clearCache 方法的初始调用,但即使在先前调用中清除缓存后也无法再次访问数据库。
@GetMapping(value = "/getCustomers" , produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Customers> getCustomers(HttpServletRequest request,
@RequestParam(name = "clearCache", required = false) boolean clearCache) {
logger.info("Entered getCustomers() clearCache {}", clearCache);
ResponseEntity response = new ResponseEntity(new ErrorResponse("Exception while retrieving customers data"),
new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
try{
List<CustomersInfo> customers = custService.getCustomers(clearCache);
response = getCustomersResponse(customers);
}catch (Exception e) {
logger.error("Exception in getCustomers()"+ e);
}
return response;
}
@Autowired
private CacheService cacheService;
@Cacheable("customers")
public List<CusteomrsInfo> getCustomers(boolean clearCache) {
logger.info("Entered getCustomers() cacheClear {} ", clearCache);
List<LocationInfo> localCompanies = new ArrayList<>();
if (clearCache) {
logger.info("............... requested to clear the cache...............");
cacheService.evictCache();
}
getAllCustomers();
}
@Service
public class CacheService {
private static final Logger logger = LoggerFactory.getLogger(MyService.class);
@Autowired
CacheManager cacheManager;
public void evictCache() {
logger.info("Clearing cache...............");
cacheManager.getCache("customers").clear();
}
@Scheduled(fixedRate = 240000)
public void evictCacheAtIntervals() {
evictCache();
}
我有自动和按需清除缓存机制。
当我调用以下端点时,它最初可以工作(命中数据库) http://localhost:8265/myApp/customers/customersInfo?clearCache=true
但后来调用 http://localhost:8265/myApp/customers/customersInfo 没有命中数据库,它仍然从缓存中获取数据,即使它在之前的 clearCache 调用中被清除了。
请指导我,提前谢谢
【问题讨论】:
标签: java spring-boot spring-cache