Spring3.1 +Ehcache 注解形式配置
1.Spring配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"
default-lazy-init="false" default-autowire="byName">
<context:annotation-config />
<context:component-scan base-package="com.miao" />
<!-- 启用缓存注解功能 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- cacheManager工厂类 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml" />
<!-- 声明cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory" />
</beans>
2.ehcache文件
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!--
diskStore :指定数据存储位置,可指定磁盘中的文件夹位置
defaultCache : 默认的管理策略
以下属性是必须的:
name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。maxElementsInMemory:在内存中缓存的element的最大数目。
maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。
以下属性是可选的:
timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。
timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。
diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
diskExpiryThreadIntervalSeconds: 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
diskSpoolBufferSizeMB: DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
memoryStoreEvictionPolicy: 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
缓存的3 种清空策略 :
FIFO ,first in first out (先进先出).
LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="520"
timeToLiveSeconds="520"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="testCache"
maxElementsInMemory="10000"
maxElementsOnDisk="1000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
3.测试类的代码
package com.miao.service.user;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.miao.dao.user.UserDao;
@Service("userService")
public class UserServiceImp implements UserService{
@Resource
private UserDao userDao;
/**
* Cacheable 参数意义
* value:缓存位置名称,不能为空,对应ehcache.xml中声明的cache的name
* key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL
* condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL
*
*/
@Override
@Cacheable(value="testCache") //指定cache
public List getAllUsers() {
return userDao.getAllUsers();
}
@Override
//@Cacheable(value="testCache",key="#id +'getUserById'")
@Cacheable(value="testCache",key="'getUserById('+#id+')'") //指定cache, 设置key
public List getUserById(String id) {
return userDao.getUserById(id);
}
@Override
//@CacheEvict(value="testCache",key="'getUserById('+#id+')'") //清除指定缓存 (业务逻辑修改了某个User,所以要将这个User的缓存清除;其实也要将获取所有User的缓存清除)
//@CacheEvict(value="testCache",allEntries=true) //清除所有的缓存
@CacheEvict(value="testCache",allEntries=true,condition="#id=='lisi'") //当id==“lisi”时,清除所有的缓存
public boolean modifyUserById(String id) {
return true;
}
}
4.调用方法进行测试即可
分享到:
相关推荐
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!
NULL 博文链接:https://greemranqq.iteye.com/blog/2002473
spring3整合EhCache注解实例
本例子主要讲解ehcache的配置使用。采用了java配置和xml配置两种方式。主要用于学习。 使用java配置时将SpringTestCase.java 文件中的@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) ...
本例子主要讲解ehcache的配置使用。采用了java配置和xml配置两种方式。主要用于学习。 使用java配置时将SpringTestCase.java 文件中的@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) ...
NULL 博文链接:https://liuyunlong1229.iteye.com/blog/2081421
spring mvc注解方式学习搭建的项目,供大家参考
整合S2SH+Freemarker+oscache,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
以前都是用SSH,第一次使用spring+springMVC+mybatis自己做的测试练习,主要是配置注解的使用,加了稍微详细的注释说明。另外在测试ehcache的时候也继承进去了,紧做练习参考. 环境:tomcat使用7.x,JDK1.7开发工具...
NULL 博文链接:https://cuisuqiang.iteye.com/blog/2050675
Ehcache、xmemcached、redis 整合spring注解demo
代码内含BoneCp连接池和注解缓存。没有提供Spring的jar包。只要加入Spring的包就可以使用。或者只需要BoneCP和注解缓存的可以直接到lib里拿jar包。
ehcache全解析 利用Spring和EHCache缓存结果 Hibernate+ehcache二级缓存技术 Spring基于注解的缓存配置--web应用实例 http://zjava.org.ru/
ehcache :添加依赖 pom.xml 2、添加配置文件ehcache.xml 3、添加注解@EnableCaching @Cacheable 4、插入缓存 5 读取缓存 6 设置缓存过期时间ehcache.xml -->timeToLiveSeconds。 redis : 1、添加依赖 pom.xml 2、...
springmvc3.0+hibernate+interceptor+AOP注解+EHcache
NULL 博文链接:https://hanqunfeng.iteye.com/blog/603719
使用springboot 集成ehcache,里面附带数据库建库脚本,controller,service以及对ehcache集成,操作的详细样例,使用spring注解形式。