cache implementation on DAO with custom refresh and evictions java

In my application, I have a scenario where I have to refresh cache each 24hrs. I'm expecting database downtime so I need to implement a use case to refresh cache after 24hrs only if the database is up running.

I'm using spring-ehache and I did implement simple cache to refresh for each 24 hrs, but unable to get my head around to make the retention possible on database downtime .

Answers


Conceptually you could split the scheduling and cache eviction into two modules and only clear your cache if certain condition (in this case, database's healthcheck returns true) is met:

SomeCachedService.java:

class SomeCachedService {
  @Autowired
  private YourDao dao;

  @Cacheable("your-cache")
  public YourData getData() {
    return dao.queryForData();
  }

  @CacheEvict("your-cache")
  public void evictCache() {
    // no body needed
  }
}

CacheMonitor.java

class CacheMonitor {
  @Autowired
  private SomeCachedService service;

  @Autowired
  private YourDao dao;

  @Scheduled(fixedDelay = TimeUnit.DAYS.toMillis(1))
  public conditionallyClearCache() {
    if (dao.isDatabaseUp()) {
      service.evictCache();
    }  
  }
}

Ehcache also allows you to create a custom eviction algorithm but the documentation doesn't seem too helpful in this case.


Need Your Help

How do I get rid of sticky iPad splash screens?

iphone ios ipad

Initially I developed my app as Universal. Then I designed splash screens separately for iPad. Recently I decided to deploy my app in iPhone device mode only. My app has no fancy graphics and it just

Section titles in the Sphinx sidebar

python documentation python-sphinx restructuredtext

Is there any way to add section titles in the left menu of Sphinx documentation?