spring boot 调用ehcache3.x(jsr107接口)原生api

来源:互联网 发布:深圳sem优化 编辑:程序博客网 时间:2024/06/05 17:39

有时候,spring cache的注解可能没有满足我们的需求,这时候就要考虑使用原生API了!其实原理很简单,就是把cache交给spring容器托管就好。废话少说上代码:(注意:当前的该文是基于spring boot spring cache ehcache3.x整合)

@Configuration@EnableCachingpublic class CacheConfig {    /**     * 托管在配置文件ehcache.xml定义的Cache     * @param cacheManager     * @return     */    @Bean    public Cache<String,String> cityCache(CacheManager cacheManager) {        return cacheManager.getCache("city");    }    /**     * 托管在配置文件ehcache.xml定义的Cache     * @param cacheManager     * @return     */    @Bean    public Cache<String,String> userCache(CacheManager cacheManager) {        return cacheManager.getCache("user");    }}

使用例子:

import javax.cache.Cache;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;public class StartupRunner implements CommandLineRunner {    @Autowired    private Cache<String,String> cityCache;    @Autowired    private Cache<String,String> userCache;    @Override    public void run(String... strings) throws Exception {        cityCache.put("userName","liangrh");        userCache.put("userName","liangrh");    }}

Over…

1 0
原创粉丝点击