欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java Ehcache緩存框架入門級(jí)使用實(shí)例

 更新時(shí)間:2017年08月11日 10:47:19   作者:liangzzz  
這篇文章主要介紹了Java Ehcache緩存框架入門級(jí)使用實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

前言

JAVA緩存實(shí)現(xiàn)方案有很多,最基本的自己使用Map去構(gòu)建緩存,或者使用memcached或Redis,但是上述兩種緩存框架都要搭建服務(wù)器,而Map自行構(gòu)建的緩存可能沒有很高的使用效率,那么我們可以嘗試一下使用Ehcache緩存框架。

Ehcache主要基于內(nèi)存緩存,磁盤緩存為輔的,使用起來方便。下面介紹如何在項(xiàng)目中使用Ehcache

入門使用教程

1.maven引用

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

2.在classpath下建立一個(gè)ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--timeToIdleSeconds 當(dāng)緩存閑置n秒后銷毀 --> 
<!--timeToLiveSeconds 當(dāng)緩存存活n秒后銷毀 --> 
<!-- 
緩存配置 
    name:緩存名稱。 
    maxElementsInMemory:緩存最大個(gè)數(shù)。 
    eternal:對(duì)象是否永久有效,一但設(shè)置了,timeout將不起作用。 
    timeToIdleSeconds:設(shè)置對(duì)象在失效前的允許閑置時(shí)間(單位:秒)。僅當(dāng)eternal=false對(duì)象不是永久有效時(shí)使用,可選屬性,默認(rèn)值是0,也就是可閑置時(shí)間無窮大。 
    timeToLiveSeconds:設(shè)置對(duì)象在失效前允許存活時(shí)間(單位:秒)。最大時(shí)間介于創(chuàng)建時(shí)間和失效時(shí)間之間。僅當(dāng)eternal=false對(duì)象不是永久有效時(shí)使用,默認(rèn)是0.,也就是對(duì)象存活時(shí)間無窮大。 
    overflowToDisk:當(dāng)內(nèi)存中對(duì)象數(shù)量達(dá)到maxElementsInMemory時(shí),Ehcache將會(huì)對(duì)象寫到磁盤中。 
    diskSpoolBufferSizeMB:這個(gè)參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認(rèn)是30MB。每個(gè)Cache都應(yīng)該有自己的一個(gè)緩沖區(qū)。 
    maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)。 
    diskPersistent:是否緩存虛擬機(jī)重啟期數(shù)據(jù) Whether the disk store persists between restarts of the Virtual Machine. The default value is false. 
    diskExpiryThreadIntervalSeconds:磁盤失效線程運(yùn)行時(shí)間間隔,默認(rèn)是120秒。 
    memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時(shí),Ehcache將會(huì)根據(jù)指定的策略去清理內(nèi)存。默認(rèn)策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進(jìn)先出)或是LFU(較少使用)。 
    clearOnFlush:內(nèi)存數(shù)量最大時(shí)是否清除。 
-->
   <!-- 磁盤緩存位置 -->
  <diskStore path="java.io.tmpdir/easylink-mall-web/ehcache"/>
  <!-- 默認(rèn)緩存 -->
  <defaultCache
      maxElementsInMemory="10000"
      eternal="false"
      timeToIdleSeconds="120"
      timeToLiveSeconds="120"
      maxElementsOnDisk="10000000"
      diskExpiryThreadIntervalSeconds="120"
      memoryStoreEvictionPolicy="LRU">
    <persistence strategy="localTempSwap"/>
  </defaultCache>
  <!-- 商戶申請(qǐng)數(shù)據(jù)緩存 數(shù)據(jù)緩存40分鐘 -->
  <cache
      name="merchant-apply-cache"
      eternal="false"
      timeToIdleSeconds="2400"
      timeToLiveSeconds="2400"
      maxEntriesLocalHeap="10000"
      maxEntriesLocalDisk="10000000"
      diskExpiryThreadIntervalSeconds="120"
      overflowToDisk="false"
      memoryStoreEvictionPolicy="LRU">
  </cache>
</ehcache>

3.與spring的cacheManager結(jié)合使用

<?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:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache.xsd">

  <!-- 支持緩存注解 -->
  <cache:annotation-driven cache-manager="cacheManager" />

  <!-- 默認(rèn)是cacheManager -->
  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="cacheManagerFactory"/>
  </bean>

  <!-- cache管理器配置 -->
  <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
    <property name="shared" value="true" />
  </bean>

</beans>

4.代碼使用

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.easylink.mall.entity.Merchant;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/spring.xml")
public class EhcacheTest {

  @Autowired
  private CacheManager cacheManager;

  @Test
  public void execute() {
    // 獲取商戶申請(qǐng)緩存容器
    Cache cache = cacheManager.getCache("merchant-apply-cache");
    Merchant merchant = new Merchant();
    Long id = IdWorker.getId();
    merchant.setId(id);
    merchant.setName("緩存測(cè)試");
    // 將商戶申請(qǐng)數(shù)據(jù)添加至緩存中 // key : id value : object
    cache.put(id, merchant);
    // 獲取商戶申請(qǐng)數(shù)據(jù)
    // 方法1
    Merchant cacheMerchant1 = (Merchant) cache.get(id).get();
    System.out.println(cacheMerchant1.getName());
    // 方法2
    Merchant cacheMerchant2 = cache.get(id, Merchant.class);
    System.out.println(cacheMerchant2.getName());
    // 將商戶申請(qǐng)數(shù)據(jù)從緩存中移除
    cache.evict(id);
  }

}

5.注意事項(xiàng)

cache.get(key) 和cache.get(key, class);方法,由于不知道你存入的key是什么類型,所以get的時(shí)候不會(huì)做key的類型檢查,如上述例子中

Long id = IdWorker.getId();
cache.put(id, merchant);
Merchant cacheMerchant2 = cache.get(id, Merchant.class);

put進(jìn)去時(shí)的key是Long類型的,get的時(shí)候也只能傳入對(duì)應(yīng)Long類型的key才能獲取到對(duì)應(yīng)的value,如果傳入的是String類型的key,即使兩個(gè)key的值是一致的,也會(huì)導(dǎo)致無法獲取到對(duì)應(yīng)的value。這個(gè)情況很容易發(fā)生在對(duì)request請(qǐng)求的參數(shù),由于是String字符串類型,但是忘了做類型轉(zhuǎn)換就直接把這個(gè)String當(dāng)做key去獲取對(duì)應(yīng)的value。導(dǎo)致獲取不到,請(qǐng)同學(xué)們要注意,親身經(jīng)歷,血與淚的教訓(xùn)。

總結(jié)

以上就是我自己總結(jié)的Ehcache入門級(jí)用法,Ehcache是個(gè)不錯(cuò)的內(nèi)存緩存框架,如果沒使用過的話,可以嘗試使用。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論