Java 缓存系统 spring

Java caching system with spring

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

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

我的目标是找到一个像上面那样工作的缓存管理器class,因此,能够使用注释@cacheble、@key 等

谢谢!!

您是否考虑过 Infinispan?它提供分布式功能,并且具有良好的 spring 集成。它还支持 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"));
    }
}