Ehcache緩存框架如何在Java項(xiàng)目中使用-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)Ehcache緩存框架如何在Java項(xiàng)目中使用 ,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

德安網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,德安網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為德安上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的德安做網(wǎng)站的公司定做!

前言

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

Ehcache主要基于內(nèi)存緩存,磁盤緩存為輔的,使用起來(lá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í)間無(wú)窮大。 
    timeToLiveSeconds:設(shè)置對(duì)象在失效前允許存活時(shí)間(單位:秒)。大時(shí)間介于創(chuàng)建時(shí)間和失效時(shí)間之間。僅當(dāng)eternal=false對(duì)象不是永久有效時(shí)使用,默認(rèn)是0.,也就是對(duì)象存活時(shí)間無(wú)窮大。 
    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)致無(wú)法獲取到對(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)。

看完上述內(nèi)容,你們對(duì)Ehcache緩存框架如何在Java項(xiàng)目中使用 有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站標(biāo)題:Ehcache緩存框架如何在Java項(xiàng)目中使用-創(chuàng)新互聯(lián)
文章源于:http://bm7419.com/article0/dseooo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、域名注冊(cè)自適應(yīng)網(wǎng)站、網(wǎng)站制作、外貿(mào)建站關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司