最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java緩存ehcache的使用步驟

 更新時(shí)間:2021年05月10日 10:50:50   作者:彩虹咖啡  
這篇文章主要介紹了Java緩存ehcache的使用步驟,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下

一、pom.xml

<dependency>
		    <groupId>net.sf.ehcache</groupId>
		    <artifactId>ehcache</artifactId>
		    <version>2.10.4</version>
</dependency>

二、編寫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">
 
	<cacheManagerPeerProviderFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
		properties="peerDiscovery=automatic, multicastGroupAddress=198.1.1.1,
         multicastGroupPort=10001,
         timeToLive=1" />
 
	<cacheManagerPeerListenerFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
		properties="port=10001,socketTimeoutMillis=60000" />
 
	<!-- 磁盤緩存位置 -->
	<diskStore path="java.io.tmpdir/anywhere" />
 
	
	<cache name="oneCache" maxElementsInMemory="1500" eternal="false"
		timeToIdleSeconds="900" timeToLiveSeconds="900" overflowToDisk="false"
		memoryStoreEvictionPolicy="LRU">
		<cacheEventListenerFactory
			class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateRemovals=false"/>
		<bootstrapCacheLoaderFactory
			class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
	</cache>
	
</ehcache>

三、參數(shù)簡介

maxElementsInMemory 緩存中允許創(chuàng)建的最大對(duì)象數(shù)
eternal 緩存中對(duì)象是否為永久的,如果是,超時(shí)設(shè)置將被忽略,對(duì)象從不過期。
timeToIdleSeconds 緩存數(shù)據(jù)空閑的最大時(shí)間,也就是說如果有一個(gè)緩存有多久沒有被訪問就會(huì)被銷毀,
如果該值是 0 就意味著元素可以停頓無窮長的時(shí)間。
timeToLiveSeconds 緩存數(shù)據(jù)存活的時(shí)間,緩存對(duì)象最大的的存活時(shí)間,超過這個(gè)時(shí)間就會(huì)被銷毀,
這只能在元素不是永久駐留時(shí)有效,如果該值是0就意味著元素可以停頓無窮長的時(shí)間。
overflowToDisk 內(nèi)存不足時(shí),是否啟用磁盤緩存。
memoryStoreEvictionPolicy 緩存滿了之后的淘汰算法。
peerDiscovery 方式:atutomatic 為自動(dòng) ;manual 手動(dòng)
mulicastGroupAddress 廣播組地址:192.1.1.1
mulicastGroupPort 廣播組端口:10001;
timeToLive 是指搜索范圍:0是同一臺(tái)服務(wù)器,1是同一個(gè)子網(wǎng),32是指同一站點(diǎn),64是指同一塊地域,128是同一塊大陸;
hostName 主機(jī)名或者ip,用來接受或者發(fā)送信息的接口

四、Ehcache的緩存數(shù)據(jù)淘汰策略

FIFO:先進(jìn)先出

LFU:最少被使用,緩存的元素有一個(gè)hit屬性,hit值最小的將會(huì)被清出緩存。

LRU:最近最少使用,緩存的元素有一個(gè)時(shí)間戳,當(dāng)緩存容量滿了,而又需要騰出地方來緩存新的元素的時(shí)候,那么現(xiàn)有緩存元素中時(shí)間戳離當(dāng)前時(shí)間最遠(yuǎn)的元素將被清出緩存

五、編寫spring-ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <description>ehcache</description>
  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
  </bean>
  <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:/ehcache.xml"/>
  </bean>
</beans>

六、與Spring整合,導(dǎo)入到spring配置文件

<import resource="classpath:/spring-ehcache.xml"/>

七、Java Source code

使用類導(dǎo)入:
    @Resource
    private org.springframework.cache.ehcacheEhCacheCacheManager cacheManager;

從獲取cache
    Cache cache = cacheManager.getCache(“oneCache”);
存入cache
    cache.put(“key”, “value”);
從cache中獲取
    ValueWrapper val = cache.get(“key”);
    String tempVal = (String)val.get();

到此這篇關(guān)于Java緩存ehcache的使用步驟的文章就介紹到這了,更多相關(guān)ehcache緩存的使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring框架下websocket的搭建

    spring框架下websocket的搭建

    本篇文章主要介紹了spring框架下websocket的搭建,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-03-03
  • 最新評(píng)論

    进贤县| 卫辉市| 邯郸县| 南投县| 江川县| 忻城县| 浦江县| 收藏| 纳雍县| 武隆县| 胶州市| 翁牛特旗| 太仆寺旗| 紫阳县| 开原市| 隆回县| 民勤县| 凌海市| 郯城县| 大邑县| 沁水县| 长丰县| 贵港市| 柞水县| 秦安县| 崇文区| 响水县| 田阳县| 犍为县| 盐源县| 理塘县| 江达县| 合水县| 平遥县| 阳原县| 突泉县| 安国市| 盈江县| 黄龙县| 武功县| 封开县|