ehcache / ehcache-jcache

The Ehcache 2.x implementation of JSR107 (JCACHE)
Other
91 stars 48 forks source link

Expiration configured in ehcache.xml in not honoured #26

Closed jonekdahl closed 9 years ago

jonekdahl commented 10 years ago

Cache element expiry does not seem to work properly when the JCache API is used with a cache that is configured through ehcache.xml.

ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">
    <cache name="cache123"
           maxEntriesLocalHeap="123"
           eternal="false"
           overflowToDisk="false"
           timeToIdleSeconds="1"
           timeToLiveSeconds="1"
           memoryStoreEvictionPolicy="LRU">
    </cache>
</ehcache>

My understanding is that the above config file creates an in-memory cache with 123 elements, and that elements time out after 1 second. I believe the following test case verifies my assumptions, note that the second test case uses the unwrapped ehcache instance from the JCache API:

EhCache test case:

package org.example;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.ehcache.jcache.JCache;
import org.junit.Assert;
import org.junit.Test;

import javax.cache.Caching;

public class EhCacheTest {

    private final String cacheName = "cache123";

    private void checkCache(Cache cache) throws InterruptedException {
        Assert.assertNull("Cache should initially be empty", cache.get("test"));
        cache.put(new Element("test", new Object()));
        Assert.assertNotNull("Should not have timed out", cache.get("test"));
        Thread.sleep(2000);
        Assert.assertNull("Should have timed out", cache.get("test"));
    }

    @Test
    public void testEhCacheDirect() throws InterruptedException {
        CacheManager manager = CacheManager.newInstance();
        Cache cache = manager.getCache(cacheName);
        checkCache(cache);
    }

    @Test
    public void testEhCacheFromJCache() throws InterruptedException {
        JCache jcache = Caching.getCachingProvider().getCacheManager().getCache(cacheName).unwrap(JCache.class);
        Cache cache = (Cache) jcache.unwrap(Cache.class);
        checkCache(cache);
    }
}

The above tests are both successful. When accessing the "same" cache through the JCache API however, the elements does not seem to expire. The test testJCacheXmlConfigured below consistently fails on the third assert ("Should have timed out by now").

JCache test case:

package org.example;

import org.junit.Assert;
import org.junit.Test;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ModifiedExpiryPolicy;
import javax.cache.spi.CachingProvider;
import java.util.concurrent.TimeUnit;

public class JCacheTest {

    CachingProvider provider = Caching.getCachingProvider();
    CacheManager manager = provider.getCacheManager();

    protected void checkCache(Cache cache) throws InterruptedException {
        final String key = "test";
        Assert.assertNull("Cache should initially be empty", cache.get(key));
        cache.put(key, new Object());
        Assert.assertNotNull("Should not have timed out yet", cache.get(key));
        Thread.sleep(2000);
        Assert.assertNull("Should have timed out by now", cache.get(key));
    }

    @Test
    public void testJCacheManuallyConfigured() throws InterruptedException {
        CachingProvider provider = Caching.getCachingProvider();
        CacheManager manager = provider.getCacheManager();
        Cache cache = manager.createCache("myManuallyConfiguredCache",
                new MutableConfiguration()
                        .setStoreByValue(false)
                        .setExpiryPolicyFactory(
                                ModifiedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, 1000))
                        ));
        checkCache(cache);
    }

    @Test
    public void testJCacheXmlConfigured() throws InterruptedException {
        final String cacheName = "cache123";
        Cache cache = manager.getCache(cacheName);
        checkCache(cache);
    }
}
alexsnaps commented 10 years ago

Hey Jon, Thanks for the detailed report. I'll look into this today and will update you here... Alex

alexsnaps commented 10 years ago

There is a bug indeed. It's identified... PR coming up

jonekdahl commented 10 years ago

Great, for a while there I was really wondering what I was doing wrong.

alexsnaps commented 10 years ago

I'm to blame for this, sorry about that. But thanks again for this detailed description! Really helps helping you quickly :). I'm proposing we cut a 1.0.1 release early November, but you probably may want to check the -SNAPSHOT as soon as this is merged in!

jonekdahl commented 9 years ago

@alexsnaps, what is the plan for the 1.0.1 release?

alexsnaps commented 9 years ago

Working on getting this out asap, hopefully today!

gchaitu795 commented 6 years ago

timeToIdleSeconds and timeToLiveSeconds are not working as expected when I'm using ehcache with spring boot by configuring through xml.

My sample ehcache configuration looks like below: <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">

<diskStore path="java.io.tmpdir" />

<cache name="usersCache" 
    maxEntriesLocalHeap="10000"
    maxEntriesLocalDisk="1000" 
    eternal="false" 
    diskSpoolBufferSizeMB="20"
    timeToIdleSeconds="3" 
    timeToLiveSeconds="3"
    memoryStoreEvictionPolicy="LFU" 
    transactionalMode="off">
    <persistence strategy="localTempSwap" />
</cache>

Even after 3 seconds, element is not flushed or evicted from cache. Please let me know, if my configuration is wrong or missed anything.

I'm using Ehcache 3.5.2 version.

henri-tremblay commented 6 years ago

@gchaitu795 No you are not. This is an Ehcache 2 configuration. And ehcache-jcache is for Ehcache 2. This project is not maintained anymore.

If you have user support questions, please use the ehcache mailing list. Picking randomly an issue that seems to be related to your question is not really considered good manners.