2

In this cacheManager does not auto-refreshed when time limit exceed with getTimeToLive()

private void setCacheInstance() {
    cacheManager
            .createCache("rateDataCached", CacheConfigurationBuilder
                    .newCacheConfigurationBuilder(
                            Integer.class, PostageServicesResultHolder.class,
                            ResourcePoolsBuilder.heap(100))
                    .withExpiry(Expirations.timeToLiveExpiration(Duration.of(getTimeToLive(), TimeUnit.MINUTES))).build());
}

private long getTimeToLive() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH.mm");
    Float currentTime = Float.parseFloat(sdf.format(cal.getTime()));
    return (long) ((24.00-currentTime) * 60);
}
SGG
  • 135
  • 1
  • 3
  • 15
  • @Anant666 yes we can try that, but in my case, i have to clear cache without using any job or scheduler. – SGG Jun 21 '17 at 09:42

2 Answers2

2

You can configure a loader. It will load the new value as soon as the cached value expires.

Then, a value is tested for expiration when accessed. So it is currently impossible to expire it in background and prefetch it using only Ehcache if that's was you wanted to do. In general, it is unnecessary anyway.

If you really wanted to do it, you need indeed you own scheduler that will get the value periodically to expire it and prefetch it (by using a loader or putting the new value).

In case you wonder, if Ehcache was doing it internally, we would have scavenger and prefetcher tasks scheduled. So whoever is doing it needs scheduled jobs.

The answer the comment. In order to expire all entries at midnight, you can use a custom expiry which will calculate the time until midnight for each entry. Here is a sample code for that:

Expiry expiry = new Expiry() {
  @Override
  public Duration getExpiryForCreation(Object key, Object value) {
    long msUntilMidnight = LocalTime.now().until(LocalTime.MAX, ChronoUnit.MILLIS);
    return Duration.of(msUntilMidnight, TimeUnit.MILLISECONDS);
  }

  @Override
  public Duration getExpiryForAccess(Object key, ValueSupplier value) {
    return null;
  }

  @Override
  public Duration getExpiryForUpdate(Object key, ValueSupplier oldValue, Object newValue) {
    return null;
  }
};

try(CacheManager cm = CacheManagerBuilder.newCacheManagerBuilder()
  .withCache("cache",
    CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .heap(10))
  .withExpiry(expiry))
      .build(true)) {

  // ...
}
Henri
  • 5,551
  • 1
  • 22
  • 29
  • In my case in its getting timeToLiveExpiration correctly, But when i use this line to check the current time left rateShopCache.getRateShopCacheFromCacheManager().containsKey(identifier) it store the static value & never refresh it whenever time get expired – SGG Jun 17 '17 at 07:21
  • Is there any method, in which I can set time of 12:00 am & cache refreshed automatically @Henri – SGG Jun 17 '17 at 07:24
  • The easiest way to do it is to have a scheduler clearing the cache à 12:00 am. Which can optionally load the new value right away (using `put` or `get` and a loader). The other solution is to use a custom expiry that calculate each entry to expire on midnight. I've update my answer with a code sample. – Henri Jun 19 '17 at 01:57
0

I have tried using calendar instance by using Expiry class,

public class CustomExpiry implements Expiry {

@Override
public Duration getExpiryForCreation(Object key, Object value) {
    return Duration.of(getTimeToLive(), TimeUnit.MILLISECONDS);
}

@Override
public Duration getExpiryForAccess(Object key, ValueSupplier value) {
    return null;
}

@Override
public Duration getExpiryForUpdate(Object key, ValueSupplier oldValue, Object newValue) {
    return null;
}

private long getTimeToLive() {
    Calendar now = Calendar.getInstance();
    Calendar midnight = Calendar.getInstance();

    midnight.set(Calendar.HOUR_OF_DAY,0);
    midnight.set(Calendar.MINUTE,0);
    midnight.set(Calendar.SECOND,0);
    midnight.set(Calendar.MILLISECOND,0);
    midnight.add(Calendar.DATE, 1);

    long difference = midnight.getTimeInMillis() - now.getTimeInMillis();
    System.out.println("caching time : "+  difference);
    return difference;

}

}

SGG
  • 135
  • 1
  • 3
  • 15