`
孤星119
  • 浏览: 122746 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring + Ehcache 注解形式配置

 
阅读更多

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.调用方法进行测试即可

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics