集成spring+mybatis +ehcache

在这里springMVC+mybatis如何集成我暂时不多说,之后有时间会更新。

<ehcache>
 <!--timeToIdleSeconds 当缓存闲置n秒后销毁 -->
 <!--timeToLiveSeconds 当缓存存活n秒后销毁 -->

集成ehcache只要分为四步:

    第一步:  编写ehcache配置文件ehcache.xml 当然名字自己可以随便起,只要保证在后面引用配置文件的时候能够书写正确就OK。  

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

第二步:编写ehcache整合spring配置文件 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>
第三步   将配置文件引入mvc的配置文件中 mvc-servlet.xml 因个人项目而定名字只是在我代码中是这样命名的

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

此时文件都在同一级如果配置文件在项目管理上存在分包分文件夹的情况要特别注意

第四步  使用注解的方式在dao层或者service的实现层中对应的添加  @Cacheable(value="baseCache",key="'Login'")
示例代码:


import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.cy.ssm.mapper.UserMapper;
import com.cy.ssm.beans.UserBean;
import com.cy.ssm.service.ILoginService;
 
@Service
public class LoginServiceImpl implements ILoginService {
 
//    private static Logger logger = LoggerFactory
//            .getLogger(LoginServiceImpl.class);
 
    @Resource
    private UserMapper um;
    
    @Override
    @Cacheable(value="baseCache",key="'Login'")
    public UserBean Login(String username, String password) {
        return um.login(username, password);
    }
 
}

注:ehcache注解用法
//将查询到的数据缓存到myCache中,并使用方法名称加上参数中的userNo作为缓存的关键  
    //通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的密钥,从而不会影响其它缓存值  
    @Cacheable (value = "myCache" ,key = "'get'+#userNo" ) 

@CacheEvict (value ="myCache" ,key ="'get'+#userNo") 更新操作根绝用户编号更新缓存中的数据
@CacheEvict (value = “myCache” ,allEntries = true ) 清除缓存中所有数据
--------------------- 
作者:不努力未来永远是个梦 
来源:CSDN 
原文:https://blog.csdn.net/preferG/article/details/77950221 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值