Spring Boot缓存在后续的按需清除缓存调用上不起作用。

huangapple go评论86阅读模式
英文:

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 = &quot;/getCustomers&quot; , produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity&lt;Customers&gt; getCustomers(HttpServletRequest request,
@RequestParam(name = &quot;clearCache&quot;, required = false) boolean clearCache) {
logger.info(&quot;Entered getCustomers() clearCache {}&quot;, clearCache);
ResponseEntity response = new ResponseEntity(new ErrorResponse(&quot;Exception while retrieving customers data&quot;),
new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
try{
List&lt;CustomersInfo&gt; customers = custService.getCustomers(clearCache);
response = getCustomersResponse(customers);
}catch (Exception e) {
logger.error(&quot;Exception in getCustomers()&quot;+ e);
}
return response;
}
@Autowired
private CacheService cacheService;
@Cacheable(&quot;customers&quot;)
public List&lt;CusteomrsInfo&gt; getCustomers(boolean clearCache) {
logger.info(&quot;Entered getCustomers() cacheClear {} &quot;, clearCache);
List&lt;LocationInfo&gt; localCompanies = new ArrayList&lt;&gt;();
if (clearCache) {
logger.info(&quot;............... requested to clear the cache...............&quot;);
cacheService.evictCache();
}
getAllCustomers();
}
@Service
public class CacheService {
private static final Logger logger = LoggerFactory.getLogger(MyService.class);
@Autowired
CacheManager cacheManager;
public void evictCache() {
logger.info(&quot;Clearing cache...............&quot;);
cacheManager.getCache(&quot;customers&quot;).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()

huangapple
  • 本文由 发表于 2020年8月28日 05:49:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63624576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定