`

Spring4 Cache + Ehcache配置

阅读更多

Spring4 Cache + Ehcache配置

 

从Spring 3.1之后 引入了基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使用的抽象,通过在既有代码中添加少量它定义的各种 annotation,即能够达到缓存方法的返回对象的效果来定义缓存的 key 和各种 condition,还提供开箱即用的缓存临时存储方案,也支持和主流的专业缓存

例如 EHCache 集成其特点总结如下:

通过少量的配置 annotation 注释即可使得既有代码支持缓存支持开箱即用 Out-Of-The-Box,即不用安装和部署额外第三方组件即可使用缓存

支持 SpringEL,能使用对象的任何属性或者方法来定义缓存的 key 和 condition支持 AspectJ,并通过其实现任何方法的缓存支持支持自定义 key 和自定义缓存管理者,具有相当的灵活性和扩展性

 

首先我们需要配置ehcache.xml

配置如下:

<ehcache>
	<diskStore path="java.io.tmpdir"/>
    <!-- 
                    配置自定义缓存
        maxElementsInMemory:缓存中允许创建的最大对象数
        eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
        timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,
                    两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,
                    如果该值是 0 就意味着元素可以停顿无穷长的时间。
        timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,
                    这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
        overflowToDisk:内存不足时,是否启用磁盘缓存。
        memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
    -->
    <defaultCache  
            maxElementsInMemory="5000"  
            eternal="true"  
            overflowToDisk="false"  
            memoryStoreEvictionPolicy="LFU"/>  
    <!-- 用户cache -->
    <cache name="user_Cache"
            maxElementsInMemory="10000"  
            eternal="false"
            timeToIdleSeconds="1000"
            timeToLiveSeconds="18000"
            overflowToDisk="false"  
            memoryStoreEvictionPolicy="LFU"/>
</ehcache>  

 然后我们需要在sping配置文件中增加cacheManager配置

 

 

<beans xmlns="http://www.springframework.org/schema/beans"
 	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">  
    <!--配置spring自动拦截带cache注解的注解方法-->
    <cache:annotation-driven />
    <!--配置cache管理工厂-->
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:resources/ehcache.xml" p:shared="false"/>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="cacheManagerFactory"/>
    <!-- generic cache manager
    	使用spring自带的多线程map集合,如果不适用ecache等缓存软件的,可以适用这个
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
        <property name="caches">  
            <set>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="user_Cache"/>  
             </set>  
        </property>  
    </bean>
     -->
</beans>  

 

    接着,我们就可以在serviceImpl中需要的方法上添加@CacheEvict @Cacheable  @Caching

其中

@CacheEvict 是在调用方法时删除某一个缓存,可以指定 beforeInvocation = true,在方法调用前删除,默认为调用后删除

参数: value 指定缓存的cache名字,可以指定多个,key是缓存中key的名字,beforeInvocation 是否在执行前删除缓存,默认为false,allEntries 为是否清楚cacha中所有的缓存

@Cacheable  是指定当前方法需要缓存结果

参数: value 指定缓存的cacha名字,可以指定多个, key为缓存的key名字,默认为hash,不过在我使用过程中发现有时候不指定,返回的cache有时候会是其他缓存的结果,我一直都设置.也比较简单.

key执行springEL.设置如下:

比如下面方法

@Cacheable(value="user_Cache",key=" 'findForList' + #userid+ '_' + #pwd ")

findForList(String userid,String pwd)

 如果参数为对象,可以按照如下设置,比如TmUser中有userid

@Cacheable(value="user_Cache",key=" 'findForList' + #user.userid")

findForList(TmUser user)

如果我们想使用Hash来做key,首先需要自己做一个方法比如:com.common.util.HashUtil,里面有一个方法hash(Object obj)

@Cacheable(value="user_Cache",key=" 'findForList'+T(com.common.util.HashUtil).hash(#user)")

findForList(TmUser user)

@Caching 此方法可以一次指定多个cache和evict,即,在调用方法时缓存多个对象和除去多个对象.

配置如下:

   @Caching(cacheable={@Cacheable(value="a_Cache"),@Cacheable(value="b_Cache")},evict={@CacheEvict(value="a_cache",key="a"),@CacheEvict(value="b_cache",key="b")})

将spring的配置文件在web.xml中加载,添加ehcache相关jar包,即可发现缓存已生效

注:如果在Action或Controller中控制cache,可以通过注入EhCacheCacheManager对象,id为前面标红的ID

通过 cacheManager.getCache(cacheName)获得对应Cache

通过 cache.get(缓存的key, 缓存的对象类型)得到对应的对象.

通过 cache.put(要缓存的key,要缓存的对象)来缓存对象

分享到:
评论

相关推荐

    hibernate4+spring4+springmvc+ehcache+自己写的cache系统

    hibernate4+spring4+springmvc+ehcache+自己写的cache系统

    Spring+MyBatis/Hibernate+Ehcache+Maven的Demo Web项目(稳定版)

    1.标题所提及技术的整合,Spring包括mvc、aop、ioc等。个人属于强迫症类型,技术水平怎么样再说,代码必须好看 2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache...

    Spring AOP+ehCache简单缓存系统解决方案

    需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法...

    Spring+MyBatis/Hibernate+Ehcache+Maven的Demo Web项目

    1.标题所提及技术的整合,Spring包括mvc、aop、ioc等。个人属于强迫症类型,技术水平怎么样再说,代码必须好看 2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache...

    Spring AOP+ehCache简单缓存系统解决方案.doc

    缘起需求:需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或者DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/...

    Spring 与Ehcache实现基于方法的缓存

    NULL 博文链接:https://liuyunlong1229.iteye.com/blog/2081421

    Spring来实现一个Cache简单的解决方案

    spring中使用缓存的方案,根据需求,使用Spring AOP + ehCache可实现些功能,使用EHCACHE能方便地结合SPRING来实现缓存。

    spring-cache.xsd+spring-encache.xsd

    里面有两个xsd文件,springmodules-ehcache和springmodules-cache.xsd。需求:因为有时候在你在xml中用某个标签时,提示错误,有时候就是少了xsd的引入

    AOP Cache源代码

    使用Spring AOP技术 + ehcache做的一个缓存。此代码为转载,供大家学习使用。

    JAVA编程之spring cache本机缓存应用

    JAVA编程之spring cache本机缓存应用 spring cache简单实用,简介: ...它不是具体的缓存实现,它只提供一整套的接口和代码规范、配置、注解等,用于整合各种缓存方案,比如Caffeine、Guava Cache、Ehcache。

    Ehcache整合Spring使用页面、对象缓存

    NULL 博文链接:https://bijian1013.iteye.com/blog/2263270

    Spring整合Ecache

    本实例的环境 eclipse + maven + spring + ehcache + junit EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。Ehcache是一种广泛使用的开 源Java分布式缓存。主要...

    spring ehcache

    该项目使用了spring cache第三方插件ehcache ,是一个完整的项目其中涉及一张表,需要用户自己根据mybatis的配置xml自行在数据库中添加一张表,使用的是spring3 xml配置方式,希望对你们有帮助,有问题,我可以邮箱...

    spring AOP实现查询缓存

    本代码通过使用spring aop+ehcache的技术,实现了方法级别的查询缓存,主要原理是 方法的完整路径+方法参数值,作为key,放入cache中,下次访问时先判断cache中是否有该key.

    EHCache 配置说明

    Ehcache is a widely used java distributed cache for general purpose caching, Java EE and light-weight containers. It features memory and disk stores, replicate by copy and invalidate, listeners, cache...

    Spring Boot 2.x的EhCache缓存的使用问题详解.docx

    除了按顺序侦测外,我们也可以通过配置属性spring.cache.type来强制指定。我们也可以通过debug调试查看cacheManager对象的实例来判断当前使用了什么缓存。在上一篇中,我们也展示了如何去查看当前使用情况。

    Spring Boot的EhCache缓存使用.docx

    当我们不指定具体其他第三方实现的时候,Spring Boot的Cache模块会使用ConcurrentHashMap来存储。而实际生产使用的时候,因为我们可能需要更多其他特性,往往就会采用其他缓存框架,所以接下来我们会分几篇分别介绍...

    Spring基于注解的缓存配置--EHCache AND OSCache

    NULL 博文链接:https://hanqunfeng.iteye.com/blog/603719

    spring支持ehcache

    使用spring对缓存ehcache进行管理

    Spring-Boot-Example:Spring Boot集成演示(jpa,rest,cache,redis,ehcache,log4j2,mybatis,jms,mq ...)

    Spring-Boot-Example Spring Boot 功能特性和组件系列的整合, 详解与使用。下面所有Demo都是基于 Spring Boot 2.0.x, 2.1.x, 2.2.x 版本。...4. spring-boot-cache-ehcache2 添加 ehcache2 依赖和 ehcache.

Global site tag (gtag.js) - Google Analytics