Search code examples
javakotlinignite

Apache Ignite Cache Not Expiring


The cache created by the below method is not getting expired. Note that Cache is created from the java(kotlin) client

    fun <K, V> getOrCreateCache(cacheName: String): IgniteCache<K, V> {
        val cacheCfg =
            CacheConfiguration<K, V>(cacheName)
        cacheCfg.isEagerTtl = true
        cacheCfg.atomicityMode = CacheAtomicityMode.ATOMIC
        cacheCfg.setExpiryPolicyFactory(
            CreatedExpiryPolicy.factoryOf(
                Duration(
                    TimeUnit.SECONDS,
                    60
                )
            )
        )
        return ignite.getOrCreateCache<K, V>(cacheCfg)
    }

I am created the cache by calling above method like below

var jobCache: IgniteCache<String, Job> = getOrCreateCache<String, Job>(Constants.JOB_CACHE)

The cache is getting created, I am able to put entries to the "Job" cache. But after 60 seconds of adding cache entry, added cache entry is not expiring.

Is it OK to set the cache expiry while creating the cache from ignite client? Please help me to debug why cache expiry is not happening.


Solution

  • Your code is fully working, I assume you experimented with the cache settings but did not restart the Apache Ignite node and the previous settings were in effect. This is exactly what I encountered when I first ran ignite.getOrCreateCache("test") and then ran your code and found that the data was not being cleared from the cache.

    Java validation code:

    var cacheCfg = new CacheConfiguration<String, String>("test");
    cacheCfg.setEagerTtl(true);
    cacheCfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    cacheCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new javax.cache.expiry.Duration(TimeUnit.SECONDS, 3)));
    
    ignite.getOrCreateCache(cacheCfg).put("1", "1");
    System.out.println("After insert: " + getIgnite().cache("test").get("1"));
    Thread.sleep(2000);
    System.out.println("After 2s: " + ignite.cache("test").get("1"));
    Thread.sleep(2000);
    System.out.println("After 4s: " + ignite.cache("test").get("1"));
    

    Result:

    After insert: 1
    After 2s: 1
    After 4s: null