springmvc缓存 和 mybatis缓存

1. 导入相关依赖包:

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>4.3.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>1.6.2</version>
</dependency>

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

<?xml version="1.0" encoding="UTF-8"?>  
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
    updateCheck="false">  
      
    <diskStore path="java.io.tmpdir" />  
      
    <defaultCache eternal="false"   
        maxElementsInMemory="1000"  
        overflowToDisk="false"   
        diskPersistent="false"   
        timeToIdleSeconds="0"  
        timeToLiveSeconds="600"   
        memoryStoreEvictionPolicy="LFU" />  
  
    <cache name="myCache"   
        eternal="false"   
        maxElementsInMemory="500"  
        overflowToDisk="false"   
        diskPersistent="false"   
        timeToIdleSeconds="0"  
        timeToLiveSeconds="300"   
        memoryStoreEvictionPolicy="LFU" />  
  
</ehcache>

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

4. 开启缓存注解

<!-- 启用缓存注解功能 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- spring提供的基于的ehcache实现的缓存管理器 -->
<bean id="cacheManagerFactory" 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="cacheManagerFactory" />
</bean>

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

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

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

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

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

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

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

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

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

加入MyBatis缓存:

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

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

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

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

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

猜你喜欢

转载自blog.csdn.net/qq_32352565/article/details/83513260