• springmvc缓存和mybatis缓存


    首先要有一个搭建好的ssm框架,笔者使用的是基于maven搭建的ssm框架。

    加入springmvc缓存:

    1. 导入相关依赖包:

     1 <dependency>
     2    <groupId>org.springframework</groupId>
     3    <artifactId>spring-context-support</artifactId>
     4    <version>4.3.7.RELEASE</version>
     5 </dependency>
     6 <dependency>
     7     <groupId>net.sf.ehcache</groupId>
     8     <artifactId>ehcache</artifactId>
     9     <version>1.6.2</version>
    10 </dependency>

    2. 加入ehcache的配置文件ehcache.xml:(注意文件的位置!)

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     3     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
     4     updateCheck="false">  
     5       
     6     <diskStore path="java.io.tmpdir" />  
     7       
     8     <defaultCache eternal="false"   
     9         maxElementsInMemory="1000"  
    10         overflowToDisk="false"   
    11         diskPersistent="false"   
    12         timeToIdleSeconds="0"  
    13         timeToLiveSeconds="600"   
    14         memoryStoreEvictionPolicy="LFU" />  
    15   
    16     <cache name="myCache"   
    17         eternal="false"   
    18         maxElementsInMemory="500"  
    19         overflowToDisk="false"   
    20         diskPersistent="false"   
    21         timeToIdleSeconds="0"  
    22         timeToLiveSeconds="300"   
    23         memoryStoreEvictionPolicy="LFU" />  
    24   
    25 </ehcache>

    3. 在springmvc的配置文件中开启缓存功能:(注意引入命名空间,不然会报错!)

    4. 开启缓存注解

    1 <!-- 启用缓存注解功能 -->
    2 <cache:annotation-driven cache-manager="cacheManager" />
    3 <!-- spring提供的基于的ehcache实现的缓存管理器 -->
    4 <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    5     <property name="configLocation" value="classpath:ehcache.xml" />
    6 </bean>
    7 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    8     <property name="cacheManager" ref="cacheManagerFactory" />
    9 </bean>

    5. 将缓存注解写在了service层:(控制层代码省略,就是调用一下service层)

    1 @Cacheable(value = "myCache", key = "'UserService.findById'")
    2 @Override
    3 public List<User> findById(int id) {
    4     System.out.println("*************************************************我是缓存方法*************************************************");
    5     List<User> list = userMapper.findById(id);
    6     return list;
    7 }

    value对应的是ehcache.xml文件里的name,相当于一个缓存空间。key最好在全局是唯一的,这里使用的类名+方法名,因为后面可能会根据这个值对特定的缓存进行清理。

    6. 测试:(jsp代码省略,就是调用一下controller层,然后controller层调用上面加过缓存注解的service层方法)

    第一次调用,进入该方法进行了相关程序:

    第二次调用,没有进入该方法,直接从缓存中输出了结果:

    在更新的时候,需要将该缓存清除掉:

    1 @CacheEvict(value = "myCache", key = "'UserService.findById'")
    2 @Override
    3 public void removeCache() {
    4     System.out.println("*************************************************移除了缓存*************************************************");
    5 }

    再次查询的候就会重新进入该方法进行查询。

    加入MyBatis缓存:

    mybatis的一级缓存是默认开启的,二级缓存有一个最简单的开启方法,在每个Mapper.xml文件里加入一个<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />即可(注意要放到mapper标签里)

    测试:(此处测试时先屏蔽掉了springmvc的缓存)

    1 @Override
    2 public List<User> findById(int id) {
    3     System.out.println("*************************************************我是缓存方法*************************************************");
    4     List<User> list = userMapper.findById(id);
    5     return list;
    6 }

    第一次访问,对数据库进行了查询:

    第二次访问,没有对数据库进行查询,直接使用了缓存:

    源码下载:http://files.cnblogs.com/files/ImNemo/ssm-cache.rar

    作者:Oven
    个人网站:http://www.cloveaire.com
    个性签名:大亨以正,莫退初心!
    如果觉得这篇文章对你有帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
  • 相关阅读:
    图像,script,link 空地址带来的困惑
    localStorage变更事件当前页响应新解-awen
    opencv在ios上的开发教程
    torch-ios框架XCODE使用备忘
    重拾老本行_图像处理
    dialogic d300语音卡驱动重装后启动报错问题解决方法
    Winpcap构建用户级网桥
    winpcap usb山寨网卡识别
    最近这段时间
    银行IT入门深似海
  • 原文地址:https://www.cnblogs.com/Oven5217/p/6699420.html
Copyright © 2020-2023  润新知