We have answer of your question!

100% solved queries, no empty question

Question: EhCache 3.1.3 persistence issue


0

Advertisement


I'm having issues with persistence in ehcache. The cache itself works fine when first started but it does not remember the old values when I restart my tomcat servlet. Here's my code for initializing ehcache. I'm not using any xml.

import org.ehcache.Cache;
import org.ehcache.PersistentCacheManager;
import org.ehcache.Status;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;

PersistentCacheManager persistentCacheManager =
    CacheManagerBuilder.newCacheManagerBuilder()
            .with(CacheManagerBuilder.persistence("/cache"))
            .withCache("test-cache",
                    CacheConfigurationBuilder.newCacheConfigurationBuilder(
                            Integer.class, String.class,
                            ResourcePoolsBuilder.newResourcePoolsBuilder()
                                    .heap(1, EntryUnit.ENTRIES)
                                    .offheap(1, MemoryUnit.MB)
                                    .disk(20, MemoryUnit.MB)
                    )
            ).build(true);
Cache cache = persistentCacheManager.getCache("test-cache", Integer.class, String.class);

log.info("Cache get 1 before putting: " + cache.get(1));
for(int i = 0; i< 1000; i++) {
    cache.put(i,"Hello");
}
log.info("Cache get 1 after putting: " + cache.get(1));

Result of the "before" log statement is always null after a restart.

Question author Atopcu | Source

Answer


1


Advertisement


You need to declare your disk ressource pool as persistent. It is not by default.

.disk(20, MemoryUnit.MB, true)

Then your test works perfectly.

The complete code including closing the cache manager:

import org.ehcache.Cache;
import org.ehcache.PersistentCacheManager;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.ehcache.config.builders.CacheConfigurationBuilder.*;
import static org.ehcache.config.builders.CacheManagerBuilder.*;
import static org.ehcache.config.builders.ResourcePoolsBuilder.*;

public class Main {

  private static final Logger log = LoggerFactory.getLogger(Main.class);

  public static void main(String[] args) {
    try(PersistentCacheManager persistentCacheManager =
        newCacheManagerBuilder()
            .with(persistence("cache"))
            .withCache("test-cache",
                newCacheConfigurationBuilder(
                    Integer.class, String.class,
                    newResourcePoolsBuilder()
                        .heap(1, EntryUnit.ENTRIES)
                        .offheap(1, MemoryUnit.MB)
                        .disk(20, MemoryUnit.MB, true)
                )
            ).build(true)) {
      Cache cache = persistentCacheManager.getCache("test-cache", Integer.class, String.class);

      log.info("Cache get 1 before putting: " + cache.get(1));
      for (int i = 0; i < 1000; i++) {
        cache.put(i, "Hello");
      }
      log.info("Cache get 1 after putting: " + cache.get(1));
    }
  }
}
Answer author Henri

Advertisement


Tickanswer.com is providing the only single recommended solution of the question EhCache 3.1.3 persistence issue under the categories i.e java , persistence , ehcache , disk , . Our team of experts filter the best solution for you.

Related Search Queries:

ehcache 3 configuration , ehcache 3.0 example , ehcache 3 example , ehcache 3 vs ehcache 2 , ehcache 3.1 example , ehcache 3 eviction policy , ehcache 3 statistics , ehcache 3 xml ,

You may also add your answer!