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

spring-boot整合ehcache實(shí)現(xiàn)緩存機(jī)制的方法

 更新時(shí)間:2018年01月18日 09:07:57   作者:大招無限  
spring-boot是一個(gè)快速的集成框架,其設(shè)計(jì)目的是用來簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了spring-boot整合ehcache實(shí)現(xiàn)緩存機(jī)制,需要的朋友可以參考下

EhCache 是一個(gè)純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn),是Hibernate中默認(rèn)的CacheProvider。

  ehcache提供了多種緩存策略,主要分為內(nèi)存和磁盤兩級(jí),所以無需擔(dān)心容量問題。

  spring-boot是一個(gè)快速的集成框架,其設(shè)計(jì)目的是用來簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進(jìn)行配置,從而使開發(fā)人員不再需要定義樣板化的配置。

  由于spring-boot無需任何樣板化的配置文件,所以spring-boot集成一些其他框架時(shí)會(huì)有略微的不同。

  1.spring-boot是一個(gè)通過maven管理的jar包的框架,集成ehcache需要的依賴如下

 <dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
   <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache</artifactId>
   <version>2.8.3</version>
</dependency> 

    具體pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.lclc.boot</groupId>
 <artifactId>boot-cache</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <!-- Inherit defaults from Spring Boot -->
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.1.3.RELEASE</version>
 </parent>
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>17.0</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
  </dependency>
  <dependency>
   <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache</artifactId>
   <version>2.8.3</version>
  </dependency>
 </dependencies>
 <dependencyManagement>
  <dependencies>
  </dependencies>
 </dependencyManagement>
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
 <repositories>
  <repository>
   <id>spring-snapshots</id>
   <url>http://repo.spring.io/snapshot</url>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </repository>
  <repository>
   <id>spring-milestones</id>
   <url>http://repo.spring.io/milestone</url>
  </repository>
 </repositories>
 <pluginRepositories>
  <pluginRepository>
   <id>spring-snapshots</id>
   <url>http://repo.spring.io/snapshot</url>
  </pluginRepository>
  <pluginRepository>
   <id>spring-milestones</id>
   <url>http://repo.spring.io/milestone</url>
  </pluginRepository>
 </pluginRepositories>
</project>

  2.使用ehcache,我們需要一個(gè)ehcache.xml來定義一些cache的屬性。

<?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/Tmp_EhCache" />
   <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
 timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
   <cache name="demo" eternal="false" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
 timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" />
</ehcache>

  解釋下這個(gè)xml文件中的標(biāo)簽。

  (1).diskStore: 為緩存路徑,ehcache分為內(nèi)存和磁盤兩級(jí),此屬性定義磁盤的緩存位置。參數(shù)解釋如下:    

    user.home – 用戶主目錄
     user.dir – 用戶當(dāng)前工作目錄
     java.io.tmpdir – 默認(rèn)臨時(shí)文件路徑

  (2).defaultCache:默認(rèn)緩存策略,當(dāng)ehcache找不到定義的緩存時(shí),則使用這個(gè)緩存策略。只能定義一個(gè)。

(3).cache:自定緩存策略,為自定義的緩存策略。參數(shù)解釋如下:

    cache元素的屬性:

name:緩存名稱
maxElementsInMemory:內(nèi)存中最大緩存對(duì)象數(shù)
maxElementsOnDisk:硬盤中最大緩存對(duì)象數(shù),若是0表示無窮大
eternal:true表示對(duì)象永不過期,此時(shí)會(huì)忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認(rèn)為false
overflowToDisk:true表示當(dāng)內(nèi)存緩存的對(duì)象數(shù)目達(dá)到了maxElementsInMemory界限后,會(huì)把溢出的對(duì)象寫到硬盤緩存中。注意:如果緩存的對(duì)象要寫入到硬盤中的話,則該對(duì)象必須實(shí)現(xiàn)了Serializable接口才行。
diskSpoolBufferSizeMB:磁盤緩存區(qū)大小,默認(rèn)為30MB。每個(gè)Cache都應(yīng)該有自己的一個(gè)緩存區(qū)。
diskPersistent:是否緩存虛擬機(jī)重啟期數(shù)據(jù)
diskExpiryThreadIntervalSeconds:磁盤失效線程運(yùn)行時(shí)間間隔,默認(rèn)為120秒
timeToIdleSeconds: 設(shè)定允許對(duì)象處于空閑狀態(tài)的最長(zhǎng)時(shí)間,以秒為單位。當(dāng)對(duì)象自從最近一次被訪問后,如果處于空閑狀態(tài)的時(shí)間超過了timeToIdleSeconds屬性值,這個(gè)對(duì)象就會(huì)過期,EHCache將把它從緩存中清空。只有當(dāng)eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對(duì)象可以無限期地處于空閑狀態(tài)
timeToLiveSeconds:設(shè)定對(duì)象允許存在于緩存中的最長(zhǎng)時(shí)間,以秒為單位。當(dāng)對(duì)象自從被存放到緩存中后,如果處于緩存中的時(shí)間超過了 timeToLiveSeconds屬性值,這個(gè)對(duì)象就會(huì)過期,EHCache將把它從緩存中清除。只有當(dāng)eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對(duì)象可以無限期地存在于緩存中。timeToLiveSeconds必須大于timeToIdleSeconds屬性,才有意義
memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時(shí),Ehcache將會(huì)根據(jù)指定的策略去清理內(nèi)存??蛇x策略有:LRU(最近最少使用,默認(rèn)策略)、FIFO(先進(jìn)先出)、LFU(最少訪問次數(shù))。

  3.將ehcache的管理器暴露給spring的上下文容器,

@Configuration
// 標(biāo)注啟動(dòng)了緩存
@EnableCaching
public class CacheConfiguration {
 /*
  * ehcache 主要的管理器
  */
 @Bean(name = "appEhCacheCacheManager")
 public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
  return new EhCacheCacheManager (bean.getObject ());
 }

 /*
  * 據(jù)shared與否的設(shè)置,Spring分別通過CacheManager.create()或new CacheManager()方式來創(chuàng)建一個(gè)ehcache基地.
  */
 @Bean
 public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
  EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
  cacheManagerFactoryBean.setConfigLocation (new ClassPathResource ("conf/ehcache-app.xml"));
  cacheManagerFactoryBean.setShared (true);
  return cacheManagerFactoryBean;
 }
}

    @Configuration:為spring-boot注解,主要標(biāo)注此為配置類,優(yōu)先掃描。

    @Bean:向spring容器中加入bean。

  至此所有的配置都做好了,通過spring-boot進(jìn)行集成框架就是這么簡(jiǎn)單。

  4.使用ehcache

    使用ehcache主要通過spring的緩存機(jī)制,上面我們將spring的緩存機(jī)制使用了ehcache進(jìn)行實(shí)現(xiàn),所以使用方面就完全使用spring緩存機(jī)制就行了。
    具體牽扯到幾個(gè)注解:

    @Cacheable:負(fù)責(zé)將方法的返回值加入到緩存中,參數(shù)3
    @CacheEvict:負(fù)責(zé)清除緩存,參數(shù)4

     參數(shù)解釋:

    value:緩存位置名稱,不能為空,如果使用EHCache,就是ehcache.xml中聲明的cache的name
    key:緩存的key,默認(rèn)為空,既表示使用方法的參數(shù)類型及參數(shù)值作為key,支持SpEL
    condition:觸發(fā)條件,只有滿足條件的情況才會(huì)加入緩存,默認(rèn)為空,既表示全部都加入緩存,支持SpEL

    allEntries:CacheEvict參數(shù),true表示清除value中的全部緩存,默認(rèn)為false

  不多說,直接上代碼:

@Service
public class CacheDemoServiceImpl implements CacheDemoService {
 /**
  * 緩存的key
  */
 public static final String THING_ALL_KEY = "\"thing_all\"";
 /**
  * value屬性表示使用哪個(gè)緩存策略,緩存策略在ehcache.xml
  */
 public static final String DEMO_CACHE_NAME = "demo";
 @CacheEvict(value = DEMO_CACHE_NAME,key = THING_ALL_KEY)
 @Override
 public void create(Thing thing){
  Long id = getNextId ();
  thing.setId (id);
  data.put (id, thing);
 } 
  @Cacheable(value = DEMO_CACHE_NAME,key = "#thing.getId()+'thing'")
 @Override
 public Thing findById(Long id){
  System.err.println ("沒有走緩存!" + id);
  return data.get (id);
 }
  @Cacheable(value = DEMO_CACHE_NAME,key = THING_ALL_KEY)
 @Override
 public List<Thing> findAll(){
  return Lists.newArrayList (data.values ());
 }
  @Override
 @CachePut(value = DEMO_CACHE_NAME,key = "#thing.getId()+'thing'")
 @CacheEvict(value = DEMO_CACHE_NAME,key = THING_ALL_KEY)
 public Thing update(Thing thing){
  System.out.println (thing);
  data.put (thing.getId (), thing);
  return thing;
 }
 @CacheEvict(value = DEMO_CACHE_NAME)
 @Override
 public void delete(Long id){
  data.remove (id);
 }
}

    5.只需要通過注解在service層方法上打注解便可以使用緩存,在find**上存入緩存,在delete**,update**上清除緩存。

總結(jié)

以上所述是小編給大家介紹的spring-boot整合ehcache實(shí)現(xiàn)緩存機(jī)制的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • springboot整合druid連接池的步驟

    springboot整合druid連接池的步驟

    這篇文章主要介紹了springboot整合druid連接池的步驟,幫助大家更好的理解和學(xué)習(xí)springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準(zhǔn)備

    Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準(zhǔn)備

    這篇文章主要為大家介紹了Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準(zhǔn)備學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Java代碼規(guī)范與質(zhì)量檢測(cè)插件SonarLint的使用

    Java代碼規(guī)范與質(zhì)量檢測(cè)插件SonarLint的使用

    本文主要介紹了Java代碼規(guī)范與質(zhì)量檢測(cè)插件SonarLint的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • POS機(jī)如何與Java交互的方式探討

    POS機(jī)如何與Java交互的方式探討

    本文深入探討POS機(jī)與Java語言的交互機(jī)制,詳細(xì)介紹了通過RESTfulAPI、Socket編程和消息隊(duì)列等方式實(shí)現(xiàn)數(shù)據(jù)交換和功能調(diào)用,文章還包含了代碼示例、狀態(tài)圖與關(guān)系圖,幫助開發(fā)者理解和實(shí)現(xiàn)POS機(jī)與Java之間的高效交互
    2024-09-09
  • SpringBoot項(xiàng)目整合Log4j2實(shí)現(xiàn)自定義日志打印失效問題解決

    SpringBoot項(xiàng)目整合Log4j2實(shí)現(xiàn)自定義日志打印失效問題解決

    這篇文章主要介紹了SpringBoot項(xiàng)目整合Log4j2實(shí)現(xiàn)自定義日志打印失效問題解決,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • java多線程文件下載器的實(shí)現(xiàn)

    java多線程文件下載器的實(shí)現(xiàn)

    本文主要介紹了java多線程文件下載器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • Spring中的集合注入代碼實(shí)例

    Spring中的集合注入代碼實(shí)例

    這篇文章主要介紹了Spring中的集合注入代碼實(shí)例,集合注入是指在Spring框架中,通過配置文件或注解的方式將集合類型的數(shù)據(jù)注入到Bean中,集合類型包括List、Set、Map和Properties等,需要的朋友可以參考下
    2023-11-11
  • Java并發(fā)編程回環(huán)屏障CyclicBarrier

    Java并發(fā)編程回環(huán)屏障CyclicBarrier

    這篇文章主要介紹了Java并發(fā)編程回環(huán)屏障CyclicBarrier,文章繼續(xù)上文所介紹的Java并發(fā)編程同步器CountDownLatch展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-04-04
  • Java設(shè)計(jì)模式之策略模式詳解

    Java設(shè)計(jì)模式之策略模式詳解

    這篇文章主要為大家詳細(xì)介紹了Java設(shè)計(jì)模式之策略模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 詳解Java目錄操作與文件操作教程

    詳解Java目錄操作與文件操作教程

    本章具體介紹了目錄操作、文件操作的基本使用方法和常用函數(shù),圖解穿插代碼實(shí)現(xiàn),感興趣的朋友來看看吧
    2022-03-03

最新評(píng)論