Spring整合配置:SpringSSM+Ehcache缓存集成

一. 引入jar包,ssm框架jar包不详述了,这里说下ehcache引入的ehcache-2.10.2.jar;

下载可以到maven仓库下载,地址:http://mvnrepository.com/,搜索ehcache即可;

二.新建ehcache.xml文件,配置缓存机制各项参数。

代码:

<?xml version="1.0" encoding="UTF-8"?>  
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  
    monitoring="autodetect" dynamicConfig="true">  
    <diskStore path="java.io.tmpdir"/>  
   <!-- <diskStore path="java.io.tmpdir" /> -->  
    <defaultCache maxElementsInMemory="10000" eternal="false"  
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"  
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"  
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"  
        memoryStoreEvictionPolicy="LRU" />  
 
    <cache name="baseCache" maxElementsInMemory="10000" eternal="false"  
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"  
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"  
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"  
        memoryStoreEvictionPolicy="LFU" />  

</ehcache>

解析:

  1. <!-- 1.必须要有的属性: name: cache的名字,用来识别不同的cache,必须惟一。 maxElementsInMemory: 内存管理的缓存元素数量最大限值。   
  2.     maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。 eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。   
  3.     overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。 2.下面是一些可选属性: timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。   
  4.     timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。 diskPersistent:   
  5.     设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。 diskExpiryThreadIntervalSeconds:   
  6.     访问磁盘线程活动时间。 diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。 memoryStoreEvictionPolicy:   
  7.     元素逐出缓存规则。共有三种,Recently Used (LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less   
  8.     Frequently Used(specified as LFU)最少使用 --> 

三.新建applicationContext-ehcache.xml文件,开启缓存,注意调用缓存配置路径。

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd  
            http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 开启spring缓存 -->
    <cache:annotation-driven cache-manager="cacheManager" />

    <bean id="ehcacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
    </bean>

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcacheManager" />
        <property name="transactionAware" value="true" />
    </bean>
</beans> 

四.将如下引用代码加入spring MVC的xml文件中

<!-- 加载Ehcache缓存 -->

    <import resource="applicationContext-ehcache.xml" />

五.在业务层或dao层中,方法添加注释


六.启动项目,查看数据后,到数据库修改数据,页面查询缓存两分钟,(设置为120秒)。

Ehcache注解用法:

@Cacheable (value = "myCache" ,key = "'get'+#userNo" )

@CacheEvict (value ="myCache" ,key ="'get'+#userNo") 更新操作根绝用户编号更新缓存中的数据 

@CacheEvict (value = “myCache” ,allEntries = true ) 清除缓存中所有数据


spring配置文件中,扫描注解,引入ehcache配置xml文件

业务层方法,注解指定缓存




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不许赖zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值