EHCache spring

 

 

import:

import org.springframework.cache.annotation.Cacheable;

 

 

注解(我放在service方法上,注意这个注解,如果用SPRING的Cacheable,就跟这一样;如果用GOOGLE的,这几个配置及import都得一致):

@Cacheable(value = "baseCache", key = "'myid_'+#date")

public String myMethod(String date){}

 

spring配置文件:

xmlns:cache="http://www.springframework.org/schema/cache"

 

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache-3.2.xsd



<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
	<cache:annotation-driven cache-manager="ehcacheManager"/>
	<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
	<bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation" value="classpath:ehcache.xml" />
		<property name="shared" value="true"/>
	</bean>
	<!-- 声明cacheManager -->
	<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
		<property name="cacheManager" ref="ehcacheManagerFactory" />
	</bean>



 

 

 

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" />
	<!--
	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
	-->
	<cache name="baseCache"
	eternal="false"
	maxElementsInMemory="100"
	overflowToDisk="false"
	diskPersistent="false"
	timeToIdleSeconds="0"
	timeToLiveSeconds="300"
	memoryStoreEvictionPolicy="LRU" />

</ehcache>



阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring