0

I have a javax.cache.Cache instance, where after 10 mins of inactivity the entries are being automatically removed. Without going into much depth, is there a configuration parameter which automatically removes key value pair from the cache?

Heres the code snippet

@Produces
@FileRicAdapterService
public EventCacheFile getNamedEventsCache(InjectionPoint injectionPoint) {
    Cache<String, CachedRecord> cache = getPersistentCacheManager()
            .getCache("filetoric");

    return new EventCacheFileImpl(cache);
}

@Produces
public CacheManager getPersistentCacheManager() {
    return new CacheManagerAdapter(
            getCacheContainer(Constants.PERSISTENT_CACHE));
}

private EmbeddedCacheManager getCacheContainer(String name) {
    String jndiName = "java:jboss/infinispan/container/" + name;
    try {
        return (EmbeddedCacheManager) new InitialContext().lookup(jndiName);
    } catch (NamingException e) {
        logger.fatal("Cache container not found: %s", name);
        throw new UnsatisfiedResolutionException(
                "Cache container not found " + name);
    }
}

1 Answers1

1

It might not be an answer, but it's too long for a comment.

You really should have included the cache JNDI configuration in your question. It's hard to guess what you are doing. However, based on the superficial details you provided, I believe you can try using the following elements to control the cache eviction and expiration, as explained in Infinispan subsystem documentation:

<eviction>

This child element configures the eviction behaviour of the cache.

  • strategy: This attribute configures the cache eviction strategy. Available options are UNORDERED, FIFO, LRU, LIRS and NONE (to disable eviction).

  • max-entries: This attribute configures the maximum number of entries in a cache instance. If selected value is not a power of two the actual value will default to the least power of two larger than selected value. -1 means no limit.

<expiration>

This child element configures the expiration behaviour of the cache.

  • max-idle: This attribute configures the maximum idle time a cache entry will be maintained in the cache, in milliseconds. If the idle time is exceeded, the entry will be expired cluster-wide. -1 means the entries never expire.

  • lifespan: This attribute configures the maximum lifespan of a cache entry, after which the entry is expired cluster-wide, in milliseconds. -1 means the entries never expire.

  • interval: This attribute specifies the interval (in ms) between subsequent runs to purge expired entries from memory and any cache stores. If you wish to disable the periodic eviction process altogether, set wakeupInterval to -1.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • is it by default set to -1 (Never expire)? I believe we havent change any default config parameter. If by default it is -1, then i have to assume there is some other place where the cache is being modified. – Syed Ahmed May 05 '16 at 16:24
  • @SyedAhmed Do you really need JNDI? – cassiomolin May 05 '16 at 16:29
  • what will be the other alternative? I am just thining of replacing Cache with HashMap – Syed Ahmed May 05 '16 at 16:31
  • @SyedAhmed Create the cache programmatically. It's something like `CacheManager cacheManager = new DefaultCacheManager(); Cache cache = cacheManager.getCache();`. I'll update the answer with more details as soon as I have the chance. – cassiomolin May 05 '16 at 16:41
  • I have debugged the code several points on the inifinispan Cache and the lifespan is always -1. But still for some reason, its automatically expiring inactive records after 10 minute! I m so confused – Syed Ahmed May 06 '16 at 01:08