springmvc和encache集成
虽然目前已经出了 ehcache3.x 了,但是,结合我搜索到的资料,我依旧只能先采用 ehcache2.x 来使用了
首先,在pom 中引入jar
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.4</version> </dependency>
引入 ehcache2 的配置文件
<?xml version="1.0" encoding="UTF-8"?> <ehcache updateCheck="false" name="defaultCache"> <diskStore path="../temp/ehcache" /> <!-- <diskStore path="java.io.tmpdir" /> --> <!-- 默认缓存配置. --> <defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" maxEntriesLocalDisk="100000" /> <!-- 系统缓存 --> <cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/> </ehcache>
在spring 的配置文件中 spring-context 中引入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd"> <!-- 当前这个配置文件,是没有用到的,保留是为了方便查阅 --> <!-- 缓存配置 --> <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) --> <!-- <cache:annotation-driven cache-manager="cacheManager"/> --> <!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) --> <!-- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/> </set> </property> </bean> --> <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 --> <!-- Spring提供的基于的Ehcache实现的缓存管理器 --> <!-- 缓存配置 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <!-- <bean id="shiroCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean> --> <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManager" ref="cacheManager"/> </bean> </beans>
我这里使用了 shiro,为了 shiro 和 ehcache 结合,引入 shiro-ehcache 包
CacheUtils 采用工具类的模式,在需要缓存的地方,手动加入或移除缓存
/** * Copyright © 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. */ package com.thinkgem.jeesite.common.utils; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; /** * Cache工具类 * @author ThinkGem * @version 2013-5-29 */ public class CacheUtils { private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager")); private static final String SYS_CACHE = "sysCache"; /** * 获取SYS_CACHE缓存 * @param key * @return */ public static Object get(String key) { return get(SYS_CACHE, key); } /** * 写入SYS_CACHE缓存 * @param key * @return */ public static void put(String key, Object value) { put(SYS_CACHE, key, value); } /** * 从SYS_CACHE缓存中移除 * @param key * @return */ public static void remove(String key) { remove(SYS_CACHE, key); } /** * 获取缓存 * @param cacheName * @param key * @return */ public static Object get(String cacheName, String key) { Element element = getCache(cacheName).get(key); return element==null?null:element.getObjectValue(); } /** * 写入缓存 * @param cacheName * @param key * @param value */ public static void put(String cacheName, String key, Object value) { Element element = new Element(key, value); getCache(cacheName).put(element); } /** * 从缓存中移除 * @param cacheName * @param key */ public static void remove(String cacheName, String key) { getCache(cacheName).remove(key); } /** * 获得一个Cache,没有则创建一个。 * @param cacheName * @return */ private static Cache getCache(String cacheName){ Cache cache = cacheManager.getCache(cacheName); if (cache == null){ cacheManager.addCache(cacheName); cache = cacheManager.getCache(cacheName); cache.getCacheConfiguration().setEternal(true); } return cache; } public static CacheManager getCacheManager() { return cacheManager; } }
手动加入和移除呢,还是太麻烦。
其实还有 spring 与 ehcache 结合的包,可以在方法上加注解的方式。参考博客:http://blog.csdn.net/u011068702/article/details/47780033
/** * ehcache-spring-annotations简介 * @see ---------------------------------------------------------------------------------------------------------------- * @see 关于Spring与Ehcache的集成,googlecode上有一个经Apache认证的开源项目叫做ehcache-spring-annotations * @see 目前已经到了1.2.0版本,它只是简化了Spring和Ehcache集成的复杂性(事实上我觉得完全没必要,因为它俩集成并不复杂) * @see 尽管如此还是要提一下,它的项目主页为https://code.google.com/p/ehcache-spring-annotations/ * @see 这篇文章中描述了其用法http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/ * @see ---------------------------------------------------------------------------------------------------------------- * @see 1)使用时在项目中引入ehcache-spring-annotations-1.2.0.jar和guava-r09.jar两个jar文件 * @see 2)然后在applicationContext.xml按照如下配置 * @see <beans xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" * @see xsi:schemaLocation="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring * @see http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd"> * @see <ehcache:annotation-driven/> * @see <ehcache:config cache-manager="cacheManager"> * @see <ehcache:evict-expired-elements interval="60"/> * @see </ehcache:config> * @see <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> * @see <property name="configLocation" value="classpath:ehcache.xml"/> * @see </bean> * @see 3)最后在需要缓存的方法上使用@Cacheable和@TriggersRemove注解即可 * @see 经我亲测,@TriggersRemove(cacheName="..", when="..", removeAll=true)可移除缓存中的全部对象 * @see 但若写成@TriggersRemove(cacheName="..", when="..")则不会移除缓存中的单一的或所有的对象,即缓存中的对象无变化 * @see ---------------------------------------------------------------------------------------------------------------- * @create Oct 3, 2013 4:56:35 PM * @author 玄玉<http://blog.csdn.net/jadyer> */
另外针对每一个方法上写一遍缓存也是够繁琐的,是否有法子采用AOP 的方式来注入缓存呢
比如 方法名为 getCacheXXX 或者 findCacheXXX 则需要缓存
update 就移除缓存 之类
等我哪天有心情研究下
-----------
Spring4整合Ehcache2.10 :http://blog.csdn.net/frankcheng5143/article/details/50776542
spring+EnCache缓存示例 :http://blog.csdn.net/zjt1388/article/details/51810270
http://blog.csdn.net/frankcheng5143/article/details/50791757
作者:panie
出处:http://www.cnblogs.com/panie2015/
如果您希望与我交流互动,欢迎加我微信
本文内容为作者辛苦整理书写,欢迎转载,但请保留文章出处
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?