带有弹簧的Java缓存系统

时间:2017-11-14 14:27:09

标签: java spring caching jcs

我已经有一个系统使用spring的缓存抽象+ EhCache实现来缓存解决方案。但现在,我需要切换EhCache以获得其他解决方案,从而为我提供分布式功能。我发现JCS(java Caching System)适合我的问题。但是,我还没有找到一种方法来使用JCS的spring缓存抽象。你们中的任何人都知道如何在JCS中使用spring缓存抽象吗?如果是这样,我该怎么做?

@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);
}

我的目标是找到一个与上面类似的CacheManager类,因此,能够使用@cacheble,@ key等的anottations。

谢谢!

1 个答案:

答案 0 :(得分:0)

您是否考虑过 Infinispan ?它提供分布式功能,并具有良好的弹簧集成。它还支持JSR 107 api。

从官方网站摘录的一个例子:

/**
 * 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"));
    }
}