springboot中ehcache的使用

来源:互联网 发布:交通数据开放平台 编辑:程序博客网 时间:2024/05/29 12:38

1.pom.xml中添加

<dependency>   <groupId>net.sf.ehcache</groupId>   <artifactId>ehcache-core</artifactId>   <version>2.4.6</version></dependency><!--开启 cache 缓存--><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-cache</artifactId></dependency><!-- ehcache 缓存 --><dependency>   <groupId>net.sf.ehcache</groupId>   <artifactId>ehcache</artifactId></dependency>
2.ehcache.xml中内容

<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"         updateCheck="false">    <defaultCache            eternal="false"            maxElementsInMemory="10000"            overflowToDisk="false"            diskPersistent="false"            timeToIdleSeconds="86400"            timeToLiveSeconds="86400"            memoryStoreEvictionPolicy="LRU" />    <!--    配置自定义缓存    maxElementsInMemory:缓存中允许创建的最大对象数    eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。    timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,                    两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,                    如果该值是 0 就意味着元素可以停顿无穷长的时间。    timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,                                      这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。    overflowToDisk:内存不足时,是否启用磁盘缓存。    memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。    -->    <cache            name="getInfoCache"            eternal="false"            maxElementsInMemory="10000"            overflowToDisk="false"            diskPersistent="false"            timeToIdleSeconds="259200"            timeToLiveSeconds="259200"            memoryStoreEvictionPolicy="LRU" /></ehcache>
3.添加bean的配置文件

package com.eltyl.config;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.ehcache.EhCacheCacheManager;import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;@Configuration// 标注启动了缓存@EnableCachingpublic class CacheConfiguration {    /*     * 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地.     */    @Bean    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();        cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml"));        cacheManagerFactoryBean.setShared (true);        return cacheManagerFactoryBean;    }    /** ehcache 主要的管理器*/    @Bean(name = "appEhCacheCacheManager")    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){        return new EhCacheCacheManager (bean.getObject ());    }}
4.增加工具类

package com.eltyl.util;import net.sf.ehcache.Cache;import net.sf.ehcache.Element;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.ehcache.EhCacheCacheManager;import org.springframework.stereotype.Component;@Componentpublic class EhcacheUtil {    @Autowired    EhCacheCacheManager appEhCacheCacheManager;    public void put(String cacheName, String key, Object value) {        Cache cache = appEhCacheCacheManager.getCacheManager().getCache(cacheName);        Element element = new Element(key, value);        cache.put(element);    }    public Object get(String cacheName, String key) {        Cache cache = appEhCacheCacheManager.getCacheManager().getCache(cacheName);        Element element = cache.get(key);        return element == null ? null : element.getObjectValue();    }    public Cache get(String cacheName) {        return appEhCacheCacheManager.getCacheManager().getCache(cacheName);    }    public void remove(String cacheName, String key) {        Cache cache = appEhCacheCacheManager.getCacheManager().getCache(cacheName);        cache.remove(key);    }}5.使用
获取缓存 cacheData=(JSONObject)ehcacheUtil.get("cacheName",key);设置缓存 ehcacheUtil.put("cacheName",key,value);


原创粉丝点击