麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Java > 正文

淺談Spring boot cache使用和原理

2024-07-14 08:42:16
字體:
來源:轉載
供稿:網友

緩存要解決的問題:一個程序的瓶頸在于數據庫,我們也知道內存的速度是大大快于硬盤的速度的。當我們需要重復地獲取相同的數據的時候,我們一次又一次的請求數據庫或者遠程服務,導致大量的時間耗費在數據庫查詢或者遠程方法調用上,導致程序性能的惡化,這便是數據緩存要解決的問題。

類似的緩存技術有:Redis、EhCache、Guava等,現在一般常用的為Redis。

Spring 3.1 引入了激動人心的基于注釋(annotation)的緩存(cache)技術,它本質上不是一個具體的緩存實現方案(例如EHCache 或者 OSCache),而是一個對緩存使用的抽象,通過在既有代碼中添加少量它定義的各種 annotation,即能夠達到緩存方法的返回對象的效果。 

Spring 的緩存技術還具備相當的靈活性,不僅能夠使用 SpEL(Spring Expression Language)來定義緩存的 key 和各種 condition,還提供開箱即用的緩存臨時存儲方案,也支持和主流的專業緩存例如 EHCache 集成。 

其特點總結如下: 

1. 通過少量的配置 annotation 注釋即可使得既有代碼支持緩存 
2. 支持開箱即用 Out-Of-The-Box,即不用安裝和部署額外第三方組件即可使用緩存 
3. 支持 Spring Express Language,能使用對象的任何屬性或者方法來定義緩存的 key 和 condition 
4. 支持 AspectJ,并通過其實現任何方法的緩存支持 
5. 支持自定義 key 和自定義緩存管理者,具有相當的靈活性和擴展性

一、Spring boot cache原理

第一步、自動配置類;

自動啟動類:CacheAutoConfiguration

屬性配置:CacheProperties

主啟動類添加:@EnableCaching注解

cache POM添加:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-cache</artifactId></dependency>

第二步、從緩存的配置類 中獲取 多個cache

CacheConfigurationImportSelector.selectImports()方法獲取

static class CacheConfigurationImportSelector implements ImportSelector {?    @Override    public String[] selectImports(AnnotationMetadata importingClassMetadata) {      CacheType[] types = CacheType.values();      String[] imports = new String[types.length];      for (int i = 0; i < types.length; i++) {        imports[i] = CacheConfigurations.getConfigurationClass(types[i]);      }      return imports;    }?}

獲取結果:SimpleCacheConfiguration 默認cache

 org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration  org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration  org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration  org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration  org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration  org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration  org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration  org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration  org.springframework.boot.autoconfigure.cache.GuavaCacheConfiguration  org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默認】  org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration

第三步:SimpleCacheConfiguration.cacheManager()

此方法中給容器中注冊了一個CacheManager組件:類型為ConcurrentMapCacheManager

@Beanpublic ConcurrentMapCacheManager cacheManager() {  ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();  List<String> cacheNames = this.cacheProperties.getCacheNames();  if (!cacheNames.isEmpty()) {   cacheManager.setCacheNames(cacheNames);  }  return this.customizerInvoker.customize(cacheManager);}

第四步:查看獲取緩存方法getCache()

ConcurrentMapCacheManager 類里,數據都存儲到為ConcurrentMap 中

public Cache getCache(String name) {  Cache cache = this.cacheMap.get(name); //cacheMap 為ConcurrentMap 類型,獲取一個cache組件  if (cache == null && this.dynamic) {   synchronized (this.cacheMap) {     cache = this.cacheMap.get(name); //cahceMap不為空獲取     if (cache == null) {      //可以獲取或者創建ConcurrentMapCache類型的緩存組件;他的作用將數據保存在ConcurrentMap中;      cache = createConcurrentMapCache(name);        this.cacheMap.put(name, cache); //ConcurrentMapCache.lookup();     }   }  }  return cache;}

二、Cacheable運行流程:

? @Cacheable: 1、方法運行之前,先去查詢Cache(緩存組件),按照cacheNames指定的名字獲取; (CacheManager先獲取相應的緩存),第一次獲取緩存如果沒有Cache組件會自動創建。 2、去Cache中查找緩存的內容(ConcurrentMapCache.lookup()方法中去查找),使用一個key,默認就是方法的參數; key是按照某種策略生成的;默認是使用keyGenerator生成的,默認使用SimpleKeyGenerator生成key; SimpleKeyGenerator生成key的默認策略; 如果沒有參數;key=new SimpleKey(); 如果有一個參數:key=參數的值 如果有多個參數:key=new SimpleKey(params);

 //這個方法 SimpleKeyGenerator.generateKey()  方法生成keypublic static Object generateKey(Object... params) {  if (params.length == 0) {   return SimpleKey.EMPTY;  }  if (params.length == 1) { //如果只有一個參數,直接返回這個參數為key   Object param = params[0];   if (param != null && !param.getClass().isArray()) {     return param;   }  }  return new SimpleKey(params);}

3、沒有查到緩存就調用目標方法; 4、將目標方法返回的結果,放進緩存中ConcurrentMapCache.put();

@Cacheable標注的方法執行之前先來檢查緩存中有沒有這個數據,默認按照參數的值作為key去查詢緩存, 如果沒有就運行方法并將結果放入緩存;以后再來調用就可以直接使用緩存中的數據;

? 核心: 1)、使用CacheManager【ConcurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】組件 2)、key使用keyGenerator生成的,默認是SimpleKeyGenerator

詳細執行流程:ConcurrentMapCache.lookup()上斷點查看,執行過程

//第一步CacheAspectSupport 中execute()private Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) //第二步 CacheAspectSupportprivate Cache.ValueWrapper findCachedItem(Collection<CacheOperationContext> contexts) {  Object result = CacheOperationExpressionEvaluator.NO_RESULT;  for (CacheOperationContext context : contexts) {    if (isConditionPassing(context, result)) {      Object key = generateKey(context, result); //獲取key      Cache.ValueWrapper cached = findInCaches(context, key);      if (cached != null) {        return cached;      }      else {        if (logger.isTraceEnabled()) {          logger.trace("No cache entry for key '" + key + "' in cache(s) " + context.getCacheNames());        }      }    }  }  return null;}//第三步:CacheAspectSupport.findInCaches()//第四步:AbstractCacheInvoker.doGet()//第五步:AbstractValueAdaptingCache.get();@Overridepublic ValueWrapper get(Object key) {    Object value = lookup(key);    return toValueWrapper(value);}// 第六步:ConcurrentMapCache.lookup(); 從ConcurrentMap 中根據key獲取值@Overrideprotected Object lookup(Object key) {    return this.store.get(key);}

三、Cacheable 注解的幾個屬性:

1、cacheNames/value:指定緩存組件的名字;將方法的返回結果放在哪個緩存中,是數組的方式,可以指定 多個緩存;

2、key:緩存數據使用的key;可以用它來指定。默認是使用方法參數的值 1-方法的返回值

? 編寫SpEL; #i d;參數id的值 #a0 #p0 #root.args[0]

? getEmp[2]

3、keyGenerator:key的生成器;可以自己指定key的生成器的組件id

? key/keyGenerator:二選一使用;

4、cacheManager:指定緩存管理器;或者cacheResolver指定獲取解析器

5、condition:指定符合條件的情況下才緩存;

? ,condition = "#id>0"

? condition = "#a0>1":第一個參數的值》1的時候才進行緩存

6、unless:否定緩存;當unless指定的條件為true,方法的返回值就不會被緩存;可以獲取到結果進行判斷

? unless = "#result == null"

? unless = "#a0==2":如果第一個參數的值是2,結果不緩存;

7、sync:是否使用異步模式;異步模式的情況下unless不支持

四、Cache使用:

1.Cacheable的使用

@Cacheable(value = {"emp"}/*,keyGenerator = "myKeyGenerator",condition = "#a0>1",unless = "#a0==2"*/)public Employee getEmp(Integer id){  System.out.println("查詢"+id+"號員工");  Employee emp = employeeMapper.getEmpById(id);  return emp;}

2.自定義keyGenerator:

@Bean("myKeyGenerator")public KeyGenerator keyGenerator(){  return new KeyGenerator(){?    @Override    public Object generate(Object target, Method method, Object... params) {      return method.getName()+"["+ Arrays.asList(params).toString()+"]";    }  };}

3.CachePut的使用:更新緩存

/**   * @CachePut:既調用方法,又更新緩存數據;同步更新緩存   * 修改了數據庫的某個數據,同時更新緩存;   * 運行時機:   * 1、先調用目標方法   * 2、將目標方法的結果緩存起來   *   * 測試步驟:   * 1、查詢1號員工;查到的結果會放在緩存中;   *     key:1 value:lastName:張三   * 2、以后查詢還是之前的結果   * 3、更新1號員工;【lastName:zhangsan;gender:0】   *     將方法的返回值也放進緩存了;   *     key:傳入的employee對象 值:返回的employee對象;   * 4、查詢1號員工?   *   應該是更新后的員工;   *     key = "#employee.id":使用傳入的參數的員工id;   *     key = "#result.id":使用返回后的id   *       @Cacheable的key是不能用#result   *   為什么是沒更新前的?【1號員工沒有在緩存中更新】   *   */  @CachePut(value = "emp",key = "#result.id")  public Employee updateEmp(Employee employee){    System.out.println("updateEmp:"+employee);    employeeMapper.updateEmp(employee);    return employee;  }

4.CacheEvict 緩存清除

/** * @CacheEvict:緩存清除 * key:指定要清除的數據 * allEntries = true:指定清除這個緩存中所有的數據 * beforeInvocation = false:緩存的清除是否在方法之前執行 *   默認代表緩存清除操作是在方法執行之后執行;如果出現異常緩存就不會清除 * * beforeInvocation = true: *   代表清除緩存操作是在方法運行之前執行,無論方法是否出現異常,緩存都清除 * * */@CacheEvict(value="emp",beforeInvocation = true,key = "#id")public void deleteEmp(Integer id){  System.out.println("deleteEmp:"+id);  //employeeMapper.deleteEmpById(id);  int i = 10/0;}

5.Caching 復雜配置

// @Caching 定義復雜的緩存規則@Caching(   cacheable = {     @Cacheable(/*value="emp",*/key = "#lastName")   },   put = {     @CachePut(/*value="emp",*/key = "#result.id"),     @CachePut(/*value="emp",*/key = "#result.email")   })public Employee getEmpByLastName(String lastName){  return employeeMapper.getEmpByLastName(lastName);}

6.CacheConfig緩存清除

@CacheConfig(cacheNames="emp",cacheManager = "employeeCacheManager") //抽取緩存的公共配置@Servicepublic class EmployeeService {

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩av电影在线观看 | 国产精品18久久久久久久久 | 久久成年网| 国产1区在线观看 | 国产va在线观看 | 蜜桃网站在线观看 | 欧美视屏一区二区 | 一区二区三区日韩精品 | 国产成人精品一区在线播放 | 免费小毛片 | 视频h在线 | 久久精品com | 国产一级一区二区 | 亚洲第九十九页 | 四虎久草| 久久久久中精品中文字幕19 | 国产精品久久久久久久久久久久午夜 | 噜噜噜在线 | 91 成人| 国产在线观看91一区二区三区 | 91av久久| 国产九色91 | 日日操操| 欧美性生活免费视频 | 久久精品久久精品国产大片 | 日本不卡一区二区三区在线观看 | 国产成人高清在线 | 99精品视频在线免费观看 | 在线免费91 | 日韩av电影在线免费观看 | 久久久久在线观看 | 91 成人| 欧美日韩免费在线观看视频 | 毛片免费观看完整版 | 久草在线资源观看 | 久久国产精品久久久久久电车 | 视频一区二区三区视频 | 国产午夜精品一区二区三区四区 | 亚洲乱操 | 久久久久久久久久久av | 天天草夜夜骑 |