时间:2021-05-19
废话少说,直接上代码:
ehcache.xml 文件
<?xml version="1.0" encoding="UTF-8"?><ehcache dynamicConfig="false" monitoring="off" updateCheck="false" xmlns:xsi="http://.sf.ehcache.Element;/** * 操作缓存类 * * @author jiangadam */public class EhcacheUtils { private static final String path = "/config/ehcache.xml"; // EHCache 的配置文件地址 private CacheManager manager; private static EhcacheUtils ehCache; private EhcacheUtils(String path) { manager = CacheManager.create(getClass().getResource(path)); } public static EhcacheUtils getInstance() { if (ehCache == null) { ehCache = new EhcacheUtils(path); } return ehCache; } /** * 缓存一个对象 * * @param cacheName * 缓存的名字 * @param key * 缓存的KEY * @param value * 缓存的值 */ public void put(String cacheName, String key, Object value) { Cache cache = manager.getCache(cacheName); Element element = new Element(key, value); cache.put(element); } /** * 获取一个缓存的对象,没有返回NULL * * @param cacheName * @param key * @return */ public Object get(String cacheName, String key) { Cache cache = manager.getCache(cacheName); Element element = cache.get(key); return element == null ? null : element.getObjectValue(); } public Cache get(String cacheName) { return manager.getCache(cacheName); } public void remove(String cacheName, String key) { Cache cache = manager.getCache(cacheName); cache.remove(key); }}PUT 写入缓存
GET 获取缓存的数据
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
方案实施1、spring和ehcache集成主要获取ehcache作为操作ehcache的对象。spring.xml中注入ehcacheManager和ehCa
使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存下载地址:http://code.google.com/p/ehcach
本文将介绍如何在springboot中集成ehcache作为hibernate的二级缓存。各个框架版本如下springboot:1.4.3.RELEASEspr
Spring框架七大模块简单介绍Spring中MVC模块代码详解ORM模块对Hibernate、JDO、TopLinkiBatis等ORM框架提供支持ORM模块
EhCache是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。ehcache提供了多种缓存策略,