参考博客
http://jinnianshilongnian.iteye.com/blog/2001040/
1.导入spring的jar包...
下面这个是ehcache缓存的
<!-- 缓存 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.2</version>
</dependency>
2.配置文件
spring_cache.xml spring的缓存管理配置文件
(初始化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:oxm="http://www.springframework.org/schema/oxm"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 支持缓存注解 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- 缓存 属性-->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<!-- ehcache的配置文件-->
<property name="configLocation" value="classpath:spring/ehcache.xml"/>
<property name="shared" value="true"/>
</bean>
<!-- 默认是cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
<!-- 开启事务 -->
<property name="transactionAware" value="true"/>
</bean>
</beans>
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"/>
<!-- 相当于cache的模板参数-->
<defaultCache eternal="false"
maxEntriesLocalHeap="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"/>
<!-- 名字为baseCache的cache-->
<cache name="baseCache"
eternal="false"
maxEntriesLocalHeap="200"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
statistics="true"
timeToLiveSeconds="0"/>
<!-- 名字为test的cache-->
<cache name="test"
eternal="false"
maxEntriesLocalHeap="200"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
statistics="true"
timeToLiveSeconds="0"/>
<!--
eternal="false" // 元素是否永恒,如果是就永不过期(必须设置)
maxElementsInMemory="1000" // 缓存容量的内存最大值(必须设置)
overflowToDisk="false" // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)
diskPersistent="false" // 磁盘缓存在VM重新启动时是否保持(默认为false)
timeToIdleSeconds="0" // 导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0
timeToLiveSeconds="600" // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期
memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU
-->
</ehcache>
3.如果是手动使用缓存
1.注入bean
@Autowired
private EhCacheCacheManager cacheManager ;
下面的代码是controller里的使用我们配置的缓存
// Cache cache = cacheManager.getCache("baseCache") ;
// Cache cache1 = cacheManager.getCache("test") ;
// cache.put("name", "zhshch");
// System.out.println(cache.getName() + "_" + cache.get("name").get());
// System.out.println(cache.getName() + "_" + cache.get("name",String.class));
4.注解方式使用缓存
package com.zhshch.service.impl;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.zhshch.po.Person;
import com.zhshch.service.ICacheService;
@Service("cacheService")
//这里cacheconfig指明我们要用 名字为test和baseCache的缓存
@CacheConfig(cacheNames={"test","baseCache"})public class CacheService implements ICacheService {
//代替数据库
private static Map<String,Object> map = new HashMap<String,Object>();
static{
Person p = new Person();
p.setId("test333");
p.setName("周星驰");
p.setAge(22);
map.put("test333", p);
}
@CachePut(key="#id")
public Person testCachePut(String id, Person person) {
return person ;
}
@CacheEvict(key="#person.id")
public String testCacheEvict(String id, Person person) {
map.remove(id) ;
return "success" ;
}
@Cacheable(key="#p0")
public Person testCacheable(String id, Person person) {
System.out.println("缓存不存在,查询数据库");
return (Person)map.get(id) ;
}
}
key的生成策略,如果未指定,会找默认的
key自己指定的使用方法,
譬如(String id, Person person)
1.#id,#person.id
2.#p0,#p1.id(p0,,p1代表第0个参数,第一个参数等)
@CachePut 把返回对象放入缓存
@CacheEvict清除缓存中为key的对象
@Cacheable如果缓存中存在key的对象,则直接使用缓存,不执行(Person)map.get(id) ;的方法了
如果缓存不存在,则会把方法返回结果按照@CachePut的规则放到缓存中
其中我cacheconfig指定了两个,所以删除放入对象,清除对象都是对两个cache都操作的,一起新增,一起清除
Cacheable不知道查找的策略,是不是一个缓存找到了就立即返回
还有我测试的时候是个Map模拟数据库,执行CacheEvict(key="test333")清除的时候,会把缓存中引用的对象给清空
所以CacheEvict之后再使用Map就得不到数据了(为null),因为mao.get("test333")那个数据已经不存在了
暂时记录这么多,更多请参考http://jinnianshilongnian.iteye.com/blog/2001040/
或者百度自行查找需要的