javascript
【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 文件配置詳解:
三、在SpringBoot啟動類中添加開啟緩存的注解@EnableCaching
一般情況下,我們在Sercive層對緩存進行操作。 Ehcache 在 Spring 中的注解如下:
Spring在每次執行前都會檢查Cache中是否存在相同key的緩存元素,如果存在就不再執行該方法,而是直接從緩存中獲取結果進行返回,否則才會執行并將返回結果存入指定的緩存中。
清除緩存。
@CachePut也可以聲明一個方法支持緩存功能。使用@CachePut標注的方法在執行前不會去檢查緩存中是否存在之前執行過的結果,而是每次都會執行該方法,并將執行結果以鍵值對的形式存入指定的緩存中。
參數解釋:
具體的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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【zTree】基于zTree动态生成树节
- 下一篇: 【SpringBoot】SpingBoo