SGG
SGG

Reputation: 165

Does ehCache cache have any option of auto refreshing? without any scheduler job?

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);
}

Upvotes: 3

Views: 2563

Answers (2)

SGG
SGG

Reputation: 165

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;

}

}

Upvotes: 0

Henri
Henri

Reputation: 5721

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)) {

  // ...
}

Upvotes: 3

Related Questions