Service 代碼:
@Service
public class GameareaServiceImpl extends BaseServiceImpl<Gamearea> implements IGameareaService{
@Resource
private GameareaMapper gameareaMapper;
@Override
@Cacheable(value="myCache")
public Gamearea find(int id) {
return gameareaMapper.find(id);
}
@Override
@Cacheable(value="myCache")
public HandlerResult list(Map map) {
HandlerResult rs = new HandlerResult();
rs.setResultObj(gameareaMapper.list(map));
return rs;
}
@Override
@Cacheable(value="myCache")
public Gamearea findByParameter(Gamearea gamearea) {
return gameareaMapper.findByParameter(gamearea);
}
}
web.xml;
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
applicationContext.xml:
<context:component-scan base-package="com.jadyer"/>
<!-- 緩存配置 -->
<!-- 啟用緩存注解功能(請將其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap實(shí)現的緩存管理器(該功能是從Spring3.1開(kāi)始提供的) -->
<!--
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
-->
<!-- 若只想使用Spring自身提供的緩存器,則注釋掉下面的兩個(gè)關(guān)于Ehcache配置的bean,并啟用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基于的Ehcache實(shí)現的緩存管理器 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
</beans>
ehcache.xml
<!-- Ehcache2.x的變化(取自https://github.com/springside/springside4/wiki/Ehcache) -->
<!-- 1)最好在ehcache.xml中聲明不進(jìn)行updateCheck -->
<!-- 2)為了配合BigMemory和Size Limit,原來(lái)的屬性最好改名 -->
<!-- maxElementsInMemory->maxEntriesLocalHeap -->
<!-- maxElementsOnDisk->maxEntriesLocalDisk -->
<ehcache mlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" >
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"/>
<cache name="myCache"
maxElementsOnDisk="20000"
maxElementsInMemory="2000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"/>
</ehcache>
<!--
<diskStore>==========當內存緩存中對象數量超過(guò)maxElementsInMemory時(shí),將緩存對象寫(xiě)到磁盤(pán)緩存中(需對象實(shí)現序列化接口)
<diskStore path="">==用來(lái)配置磁盤(pán)緩存使用的物理路徑,Ehcache磁盤(pán)緩存使用的文件后綴名是*.data和*.index
name=================緩存名稱(chēng),cache的唯一標識(ehcache會(huì )把這個(gè)cache放到HashMap里)
maxElementsOnDisk====磁盤(pán)緩存中最多可以存放的元素數量,0表示無(wú)窮大
maxElementsInMemory==內存緩存中最多可以存放的元素數量,若放入Cache中的元素超過(guò)這個(gè)數值,則有以下兩種情況
1)若overflowToDisk=true,則會(huì )將Cache中多出的元素放入磁盤(pán)文件中
2)若overflowToDisk=false,則根據memoryStoreEvictionPolicy策略替換Cache中原有的元素
eternal==============緩存中對象是否永久有效,即是否永駐內存,true時(shí)將忽略timeToIdleSeconds和timeToLiveSeconds
timeToIdleSeconds====緩存數據在失效前的允許閑置時(shí)間(單位:秒),僅當eternal=false時(shí)使用,默認值是0表示可閑置時(shí)間無(wú)窮大,此為可選屬性
即訪(fǎng)問(wèn)這個(gè)cache中元素的最大間隔時(shí)間,若超過(guò)這個(gè)時(shí)間沒(méi)有訪(fǎng)問(wèn)此Cache中的某個(gè)元素,那么此元素將被從Cache中清除
timeToLiveSeconds====緩存數據在失效前的允許存活時(shí)間(單位:秒),僅當eternal=false時(shí)使用,默認值是0表示可存活時(shí)間無(wú)窮大
即Cache中的某元素從創(chuàng )建到清楚的生存時(shí)間,也就是說(shuō)從創(chuàng )建開(kāi)始計時(shí),當超過(guò)這個(gè)時(shí)間時(shí),此元素將從Cache中清除
overflowToDisk=======內存不足時(shí),是否啟用磁盤(pán)緩存(即內存中對象數量達到maxElementsInMemory時(shí),Ehcache會(huì )將對象寫(xiě)到磁盤(pán)中)
會(huì )根據標簽中path值查找對應的屬性值,寫(xiě)入磁盤(pán)的文件會(huì )放在path文件夾下,文件的名稱(chēng)是cache的名稱(chēng),后綴名是data
diskPersistent=======是否持久化磁盤(pán)緩存,當這個(gè)屬性的值為true時(shí),系統在初始化時(shí)會(huì )在磁盤(pán)中查找文件名為cache名稱(chēng),后綴名為index的文件
這個(gè)文件中存放了已經(jīng)持久化在磁盤(pán)中的cache的index,找到后會(huì )把cache加載到內存
要想把cache真正持久化到磁盤(pán),寫(xiě)程序時(shí)注意執行net.sf.ehcache.Cache.put(Element element)后要調用flush()方法
diskExpiryThreadIntervalSeconds==磁盤(pán)緩存的清理線(xiàn)程運行間隔,默認是120秒
diskSpoolBufferSizeMB============設置DiskStore(磁盤(pán)緩存)的緩存區大小,默認是30MB
memoryStoreEvictionPolicy========內存存儲與釋放策略,即達到maxElementsInMemory限制時(shí),Ehcache會(huì )根據指定策略清理內存
共有三種策略,分別為L(cháng)RU(最近最少使用)、LFU(最常用的)、FIFO(先進(jìn)先出)
-->
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。