spring集成ehcache

1.本例环境
   1>. ehcache + spring + mybatis + eclipse Mars + 基于maven的web项目
   2.1>. spring集成mybatis的源码下载地址: https://github.com/zhangbeizhen/spring-mybatis
   2.2>. spring集成mybatis参考博客地址:  https://blog.csdn.net/zhangbeizhen18/article/details/88575287
   3>.本例源码下载地址: https://github.com/zhangbeizhen/spring-ehcache
2.本例说明
  本例是在spring集成mybatis的基础上进行集成ehcache,本例缓存是缓存一个员工信息的缓存
3.集成ehcache
  1>.在resources目录下创建如下目录与文件
  /resources/cache/ehcache.xml
  /resources/common/spring-context-ehcache.xml
  其中,ehcache.xml缓存相关的配置信息,包括缓存的定义,缓存其它参数等
       spring-context-ehcache.xml是在spring中集成ehcache相关配置
  2>.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">

	<!-- 2019-3-18 spring集成ehcache缓存相关配置 -->
	<!-- 设定缓存的默认数据过期策略 -->
	<defaultCache maxElementsInMemory="10000" 
	    eternal="false"
		overflowToDisk="true" 
		timeToIdleSeconds="100" 
		timeToLiveSeconds="200"
		diskPersistent="false" 
		diskExpiryThreadIntervalSeconds="120" />
    <!-- 定义员工信息缓存  -->
	<cache name="empCache" 
	    maxElementsInMemory="1000" 
	    eternal="false"
		overflowToDisk="true" 
		timeToIdleSeconds="60" 
		timeToLiveSeconds="100" />
</ehcache>

 3>.spring-context-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:context="http://www.springframework.org/schema/context"
	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/context
       		http://www.springframework.org/schema/context/spring-context.xsd
       		http://www.springframework.org/schema/cache
       		http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"
	default-lazy-init="true">

	<!-- 2019-3-18 spring集成ehcache缓存相关配置 -->
	<cache:annotation-driven cache-manager="cacheManager" />
	<!-- 装载ehcache缓存管理器 -->
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
		<property name="cacheManager" ref="ehcache"></property>
	</bean>
	<!-- 装载ehcache配置文件 -->
	<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation" value="WEB-INF/classes/cache/ehcache.xml"></property>
	</bean>
</beans>

4.使用缓的存java代码

/**查,第一次调用会调用数据库,第二次开始,只要在缓存有效期时间内,不调用数据库读取数据*/
@Cacheable(value = "empCache", key = "#id")
public Employee getEmployee( Integer id) {
	logger.info("获取员工号为" + id + "的员工信息.");
    Employee employee = employeeMapper.getEmployeeById(id);
    logger.info("员工信息:" + employee.toString());
    return employee;
}

5.测试url
 1>.插入一条数据
  http://127.0.0.1:8080/spring-mybatis/emp/insert?lastName=赵七&email=zhaoqi@163.com&gender=1&cardNumber=20190314002&post=程序猿&departmentId=1
 2>查询数据
 http://127.0.0.1:8080/spring-mybatis/emp/query/1
 观察控制台输出日志,第一次调用会调用数据库,第二次开始,只要在缓存有效期时间内,不调用数据库读取数据
以上,TKS.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值