javaspringcachingjcs

Java caching system with spring


I already have a system which uses spring's caching abstraction + EhCache Implementation for caching solution. But now, I need to switch EhCache for other solution that gives me distributed features. I found JCS ( java Caching System ) suitable for my problem. However, I haven't managed to find a way to use spring caching abstraction with JCS. Does anyone of you know how if it's possible to use spring caching abstraction with JCS? If so, how do I do it?

@Bean(destroyMethod = "shutdown")
public net.sf.ehcache.CacheManager ehCacheManager() {
    net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();

    DiskStoreConfiguration store = new DiskStoreConfiguration();
    store.setPath(
            diskDirectory);
    config.addDiskStore(store);
    CacheConfiguration cacheConfiguration = new CacheConfiguration();
    cacheConfiguration.setName("disk");
    cacheConfiguration.maxEntriesLocalHeap(1);
    cacheConfiguration.setTimeToLiveSeconds(Integer.parseInt(cacheTime));
    cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");


    cacheConfiguration.maxBytesLocalDisk(Long.parseLong(diskSize), MemoryUnit.GIGABYTES);
    PersistenceConfiguration perCache = new PersistenceConfiguration();
    perCache.strategy(Strategy.LOCALTEMPSWAP);

    cacheConfiguration.addPersistence(perCache);
    config.addCache(cacheConfiguration);



    return net.sf.ehcache.CacheManager.newInstance(config);
}

My goal is to find a CacheManager class that works like the one above, and Therefore, be able to use the anottations @cacheble, @key and etc.

Thanks!!


Solution

  • Have you considered Infinispan? It provides distributed features and it has a good spring integration. It also supports JSR 107 api.

    An example extracted from the official site:

    /**
     * This example shows how to configure Spring's {@link CacheManager} with 
      Infinispan implementation.
     */
    public class SpringAnnotationConfiguration {
    
        @Configuration
        public static class ApplicationConfiguration {
    
            @Bean
            public SpringEmbeddedCacheManagerFactoryBean springCache() {
                return new SpringEmbeddedCacheManagerFactoryBean();
            }
    
            @Bean
            public CachePlayground playground() {
                return new CachePlayground();
            }
        }
    
        public static class CachePlayground {
    
            @Autowired
            private CacheManager cacheManager;
    
            public void add(String key, String value) {
                cacheManager.getCache("default").put(key, value);
            }
    
            public String getContent(String key) {
                return cacheManager.getCache("default").get(key).get().toString();
            }
        }
    
        public static void main(String[] args) {
            ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
    
            CachePlayground cachePlayground = applicationContext.getBean(CachePlayground.class);
    
            cachePlayground.add("Infinispan", "Is cool!");
            System.out.println(cachePlayground.getContent("Infinispan"));
        }
    }