日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【SpringBoot】在SpringBoot中使用Ehcache

發布時間:2024/4/14 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【SpringBoot】在SpringBoot中使用Ehcache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SpringBoot提供了對緩存的支持,通過在啟動類中添加@EnableCaching注解自動化配置合適的緩存管理器(CacheManager),Spring Boot根據下面的順序去偵測緩存提供者:

  • Generic
  • JCache (JSR-107)
  • EhCache 2.x
  • Hazelcast
  • Infinispan
  • Redis
  • Guava
  • Simple

SpringBoot的緩存機制:
SpringBoot緩存是依賴于由org.springframework.cache.Cache和org.springframework.cache.CacheManager接口實現的抽象高速緩存,不提供實際的存儲,SpringBoot會根據實現自動配置合適的CacheManager,只要緩存支持通過@EnableCaching注釋啟用即可。基于Ehcache API實現的緩存操作工具類

SpringBoot 配置 EhCache 2.x
一、在pom文件中引入Ehcache依賴

二、在resources根目錄下引入配置文件 ehcache.xml,增加需要的緩存配置

Ehcache.xml 文件配置詳解:

  • diskStore:為緩存路徑,ehcache分為內存和磁盤兩級,此屬性定義磁盤的緩存位置。
  • defaultCache:默認緩存策略,當ehcache找不到定義的緩存時,則使用這個緩存策略。只能定義一個。 name:緩存名稱。
  • maxElementsInMemory:緩存最大數目 maxElementsOnDisk:硬盤最大緩存個數。
  • eternal:對象是否永久有效,一但設置了,timeout將不起作用。 overflowToDisk:是否保存到磁盤,當系統當機時
  • timeToIdleSeconds:設置對象在失效前的允許閑置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大。
  • timeToLiveSeconds:設置對象在失效前允許存活時間(單位:秒)。最大時間介于創建時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。
  • diskPersistent:是否緩存虛擬機重啟期數據 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
  • diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩沖區。
  • diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。
  • memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置為FIFO(先進先出)或是LFU(較少使用)。
  • clearOnFlush:內存數量最大時是否清除。
  • memoryStoreEvictionPolicy:可選策略有:LRU(最近最少使用,默認策略)、FIFO(先進先出)、LFU(最少訪問次數)。
  • FIFO,first in first out,先進先出。 LFU, Less Frequently
  • Used,一直以來最少被使用的。如上面所講,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。 LRU,Least Recently Used,最近最少使用的,緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現有緩存元素中時間戳離當前時間最遠的元素將被清出緩存。
  • 三、在SpringBoot啟動類中添加開啟緩存的注解@EnableCaching

    一般情況下,我們在Sercive層對緩存進行操作。 Ehcache 在 Spring 中的注解如下:

  • @Cacheable
    Spring在每次執行前都會檢查Cache中是否存在相同key的緩存元素,如果存在就不再執行該方法,而是直接從緩存中獲取結果進行返回,否則才會執行并將返回結果存入指定的緩存中。
  • @CacheEvict
    清除緩存。
  • @CachePut
    @CachePut也可以聲明一個方法支持緩存功能。使用@CachePut標注的方法在執行前不會去檢查緩存中是否存在之前執行過的結果,而是每次都會執行該方法,并將執行結果以鍵值對的形式存入指定的緩存中。
  • 參數解釋:

  • value:緩存位置名稱,不能為空,如果使用EHCache,就是ehcache.xml中聲明的cache的name
  • key:緩存的key,默認為空,既表示使用方法的參數類型及參數值作為key,支持SpEL(后面會有詳細的講解)
  • condition:觸發條件,只有滿足條件的情況才會加入緩存,默認為空,既表示全部都加入緩存,支持SpEL
  • allEntries:CacheEvict參數,true表示清除value中的全部緩存,默認為false
  • 具體的Demo示例參看本人的Github:ehcache工具類以及在springboot中使用ehcache

    附錄:最近在面試中一個面試官問到@Cacheable 的key生成的問題,在這里總結一下:

    key屬性是用來指定Spring緩存方法的返回結果時對應的key的。該屬性支持SpringEL表達式。當我們沒有指定該屬性時,Spring將使用默認策略生成key。

    自定義策略是指我們可以通過Spring的EL表達式來指定我們的key。這里的EL表達式可以使用方法參數及它們對應的屬性。使用方法參數時我們可以直接使用“#參數名”或者“#p參數index”。下面是幾個使用參數作為key的示例。

    1、用“#參數名”或者“#p參數index”

    // key 當前類,緩存對象為返回值list 15 @Cacheable(value = {"lt.ecache"}, key = "#root.targetClass") 16 public List<Employee> getEmployees() { 17 System.out.println("*** getEmployees() 已經調用 ***"); 18 List<Employee> list = new ArrayList<Employee>(5); 19 list.add(new Employee(1, "Ben", "Architect")); 20 list.add(new Employee(2, "Harley", "Programmer")); 21 list.add(new Employee(3, "Peter", "BusinessAnalyst")); 22 list.add(new Employee(4, "Sasi", "Manager")); 23 list.add(new Employee(5, "Abhi", "Designer")); 24 return list; 25 } 26 27 // key id,緩存數據為返回值Employee對象 28 @Cacheable(value = "lt.ecache", key = "#id") 29 public Employee getEmployee(int id, List<Employee> employees) { 30 System.out.println("*** getEmployee(): " + id + " ***"); 31 Employee emp = null; 32 for (Employee employee : employees) { 33 if (employee.getId() == id) { 34 emp = employee; 35 } 36 } 37 return emp; 38 } 39 40 // @CachePut會去替換緩存中的Employee對象為當前id對應的對象 41 @CachePut(value = "lt.ecache", key = "#id") 42 public Employee updateEmployee(int id, String designation, List<Employee> employees) { 43 System.out.println("*** updateEmployee() " + id + " ***"); 44 Employee emp = null; 45 int i = 0; 46 for (Employee employee : employees) { 47 if (employee.getId() == id) { 48 employee.setDesignation(designation); 49 emp = employee; 50 } 51 } 52 System.out.println(emp); 53 return emp; 54 } 55 56 //key為參數中Employee對象的id,緩存指定id對應的Employee對象 57 @Cacheable(value = "lt.ecache", key = "#employee.id") 58 public Employee addEmployee(Employee employee, List<Employee> employees) { 59 System.out.println("*** addEmployee() : " + employee.getId() + " ***"); 60 employees.add(employee); 61 System.out.println(employee); 62 return employee; 63 } 64 65 //key為參數中的id,移除緩存,移除指定id對應的Employee對象 66 @CacheEvict(value = "lt.ecache", key = "#id") 67 public Employee removeEmployee(int id, List<Employee> employees) { 68 System.out.println("*** removeEmployee() : " + id + " ***"); 69 Employee emp = null; 70 int i = 0; 71 for (Employee employee : employees) { 72 if (employee.getId() == id) { 73 emp = employee; 74 } else { 75 i++; 76 } 77 } 78 employees.remove(i); 79 return emp; 80 } 81 82 //key為當前類,移除緩存,移除employees列表對象 83 @CacheEvict(value = "lt.ecache", key = "#root.targetClass") 84 public List<Employee> removeAllEmployee(List<Employee> employees) { 85 System.out.println("*** removeAllEmployee() : ***"); 86 employees.clear(); 87 System.out.println(employees.size()); 88 return employees; 89 }

    當我們要使用root對象的屬性作為key時我們也可以將“#root”省略,因為Spring默認使用的就是root對象的屬性。

    2、如果要調用當前類里面的方法(方法必須是public):

    @Override@Cacheable(value={"TeacherAnalysis_public_chart"}, key="#root.target.getDictTableName() + '_' + #root.target.getFieldName()")public List<Map<String, Object>> getChartList(Map<String, Object> paramMap) {}public String getDictTableName(){return "";}public String getFieldName(){return "";} 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

    總結

    以上是生活随笔為你收集整理的【SpringBoot】在SpringBoot中使用Ehcache的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。