0

我已经有一个系统,它使用 spring 的缓存抽象 + EhCache 实现缓存解决方案。但是现在,我需要将 EhCache 切换为其他能够为我提供分布式功能的解决方案。我发现 JCS ( java Caching System ) 适合我的问题。但是,我还没有设法找到一种将 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);
}

我的目标是找到一个像上面那样工作的 CacheManager 类,因此,能够使用 @cacheble、@key 等注释。

谢谢!!

4

1 回答 1

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"));
    }
}
于 2017-11-16T12:43:50.230 回答