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);
阅读全文
0 0
- springboot中ehcache的使用
- SpringBoot-EhCache使用
- springboot Ehcache
- SpringBoot中aop的使用
- SpringBoot中Redis的使用
- Mybatis中使用ehcache
- hibernate中使用Ehcache
- ehcache的配置使用
- EhCache的使用详解
- ehcache缓存的使用
- Ehcache的使用
- ehcache的使用
- EHCache的使用
- EHCache的使用
- EHCache的使用
- EHCache的使用
- EHCache的使用
- ehcache 的使用
- hibernate_day03_13_多对多操作(维护第三张表)
- Centos Apache服务安装与配置
- 三极管+电容 实现的心形灯电路
- Linux进程间通信——管道 pipe 详解
- BZOJ 1601 [Usaco2008 Oct]灌水(最小生成树)
- springboot中ehcache的使用
- yarn —— Nodejs包新管理工具
- 1.15使用装饰器改变函数行为
- 4.day11_包和权限修饰符-1
- java的关系运算符
- 杭电2017新生赛1009-最大收益
- 谷歌浏览器开发调式工具devTools最新教程(完整版)
- java的逻辑运算符
- 数据保护 data protection