Search code examples
javahibernatejakarta-eecachingoracle-coherence

Coherence distributed cache expiry per Namedcache


How can we set eviction policy/expiry per cache entry for any specific cache(NamedCache). For example, I may have two cache's configured, however want to set eviction policy based on time only for one of the cache.

As per the below config file, I require to set eviction only for "TEST2CACHE" cache for say, expiry after 1 day. How to do that?

Any pointers would be helpful. Thanks.

Here, is a sample of my coherence config xml file-

    <cache-mapping>
        <cache-name>TEST1CACHE</cache-name>
        <scheme-name>MyDistributedCache</scheme-name>
    </cache-mapping>

    <!-- Application Configuration Cache -->
    <cache-mapping>
        <cache-name>TEST2CACHE</cache-name>
        <scheme-name>MyDistributedCache</scheme-name>
    </cache-mapping>

</caching-scheme-mapping>

<caching-schemes>
    <distributed-scheme>
        <scheme-name>MyDistributedCache</scheme-name>
        <service-name>MyDistributedCache</service-name>
        <lease-granularity>member</lease-granularity>
        <backing-map-scheme>
            <read-write-backing-map-scheme>
                <internal-cache-scheme>
                    <local-scheme>
                        <unit-calculator>BINARY</unit-calculator>
                    </local-scheme>
                </internal-cache-scheme>
                <cachestore-scheme>
                    <class-scheme>
                        <class-name>spring-bean:myCacheStore</class-name>
                        <init-params>
                            <init-param>
                                <param-name>setEntityName</param-name>
                                <param-value>{cache-name}</param-value>
                            </init-param>
                        </init-params>
                    </class-scheme>
                </cachestore-scheme>
            </read-write-backing-map-scheme>
        </backing-map-scheme>
        <autostart>true</autostart>
    </distributed-scheme>
</caching-schemes>

Solution

  • You are using the same 'MyDistributedCache' schema for both caches, so if you'll add eviction policy it will affect both of them:

    example:

    <local-scheme>
      <unit-calculator>BINARY</unit-calculator>
      <expiry-delay>10d</expiry-delay>
      <flush-delay>1d</flush-delay>
    </local-scheme>
    

    I'll bet you don't want to create two almost identical schemes, so the solution could be to use Scheme Inheritance Link to Coherence Documentation

    at least you would be able to reuse parts of the cache.

    Another option worth to check is to use expiration in the API, but I'm not sure if Coherence will actually clean the cache.

    NamedCache.put(Object oKey,
               Object oValue,
               long cMillis)
    

    see JavaDoc

    I suggest to use different cache schemes.