英文:
Springboot cache not working on subsequent on demand clear cache calls
问题
我有一个 Spring Boot 应用程序,我在一个方法上应用了缓存来缓存我的结果集。
这个方法在使用调度程序和初始调用 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 调用中已经清除了缓存。
请指导我,提前感谢。
英文:
I have a springboot application for which I applied cache on a method to cache my result sets.
This works fine with Scheduler and initial calls to clearCache method but fails to hit the db again even after cache clear in the previous calls.
@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();
}
I have both automatically as well as on demand clear cache mechanism.
It works initially (hits the db) when I call the below endpoint
http://localhost:8265/myApp/customers/customersInfo?clearCache=true
But later calls on
http://localhost:8265/myApp/customers/customersInfo
does not hit the db, it still gets the data from the cache even though its been cleared in the previous clearCache call.
Please guide me, thanks in advance
答案1
得分: 1
尝试在 getCustomers() 方法后添加 "if (clearCache)",即在当前会话的使用之后。
英文:
Try adding "if (clearCache) " after getCustomers() methods.
I mean after your use of current session.
答案2
得分: 0
我解决了这个问题,问题是evictCache()被放置在带有@Cacheable注解的方法下面。
在调用custService.getCustomers()之前,我将其放置在了方法之前。
英文:
I resolved this, the issue was evictCache() was placed under the method with @Cacheable.
I placed this before invoking custService.getCustomers()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论