最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot使用@Cacheable注解實現(xiàn)緩存功能流程詳解

 更新時間:2023年01月12日 11:07:36   作者:哪 吒  
最近一直再學Spring Boot,在學習的過程中也有過很多疑問。為了解答自己的疑惑,也在網(wǎng)上查了一些資料,以下是對@Cacheable注解的一些理解

一、Spring從3.1開始支持Cache

Spring 從 3.1 開始就引入了對 Cache 的支持。定義了 org.springframework.cache.Cacheorg.springframework.cache.CacheManager接口來統(tǒng)一不同的緩存技術(shù)。并支持使用 JCache(JSR-107)注解簡化我們的開發(fā)。

其使用方法和原理都類似于 Spring 對事務(wù)管理的支持,Spring Cache 是作用在方法上的,其核心思想是,當我們在調(diào)用一個緩存方法時會把該方法參數(shù)和返回結(jié)果作為一個鍵值對存在緩存中。

SpringBoot中提供的緩存注解@Cacheable,@Cacheable會將方法的返回值作為緩存的value,默認將方法的參數(shù)值作為緩存的key。@Cacheable可以標記在某個具體的方法上,也可以標記帶一個類上,表示該類的所有方法都支持緩存。

每次調(diào)用需要緩存功能的方法時,Spring 會檢查指定參數(shù)的指定目標方法是否已經(jīng)被調(diào)用過,如果有就直接從緩存中獲取方法調(diào)用后的結(jié)果,如果沒有就調(diào)用方法并緩存結(jié)果后返回給用戶,下次調(diào)用直接從緩存中獲取。

二、@Cacheable常用屬性

  • @Cacheable添加緩存,相同條件的查詢不再查詢數(shù)據(jù)庫,而是從緩存中查詢;
  • @CachePut每次都會訪問數(shù)據(jù)庫,并更新緩存;
  • @CacheEvict清除緩存;

1、value/cacheNames

如上圖所示,這兩個屬性代表的意義相同,這兩個屬性都是用來指定緩存組件的名稱,即將方法的返回結(jié)果放在哪個緩存中,屬性定義為數(shù)組,可以指定多個緩存;

2、key

可以通過 key 屬性來指定緩存數(shù)據(jù)所使用的的 key,默認使用的是方法調(diào)用傳過來的參數(shù)作為 key。最終緩存中存儲的內(nèi)容格式為:Entry<key,value> 形式。

3、condition

觸發(fā)條件。這個參數(shù)是規(guī)定這個緩存觸發(fā)的條件拼接,例如 condition="#area.id != 1",就是在id不為1的時候觸發(fā)緩存。

4、unless

排除條件。這個參數(shù)是規(guī)定這個緩存在何時不觸發(fā),例如 unless="#result == null",不對查詢結(jié)果為NULL的結(jié)果進行緩存。

5、keyGenerator

自定義的Key策略,比如緩存的Key值就是方法名。

@Configuration
public class CacheConfig{
	@Bean
	public KeyGenerator myKeyGenerator(){
		return (target,method,params)->method.getName();
	} 
}

KeyGenerator 是Spring提供的一個函數(shù)式接口。

package org.springframework.cache.interceptor;
import java.lang.reflect.Method;
/**
 * Cache key generator. Used for creating a key based on the given method
 * (used as context) and its parameters.
 *
 * @author Costin Leau
 * @author Chris Beams
 * @author Phillip Webb
 * @since 3.1
 */
@FunctionalInterface
public interface KeyGenerator {
	/**
	 * Generate a key for the given method and its parameters.
	 * @param target the target instance
	 * @param method the method being called
	 * @param params the method parameters (with any var-args expanded)
	 * @return a generated key
	 */
	Object generate(Object target, Method method, Object... params);
}

通過@Cacheable(keyGenerator=“myKeyGenerator”)指定Key自定義的生成策略。

6、sync

是否使用異步模式,默認是方法執(zhí)行完,以同步的方式將方法返回的結(jié)果存在緩存中。

7、cacheManager

可以用來指定緩存管理器,從哪個緩存管理器里面獲取緩存。

三、整合步驟

1、加入pom

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

2、啟動類加@EnableCaching注解

3、controller或service加@Cacheable注解即可 四、代碼實例

package com.nezha.controller;
import com.nezha.entity.Area;
import com.nezha.service.area.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;
	// @Cacheable添加緩存,相同條件的查詢不再查詢數(shù)據(jù)庫,而是從緩存中查詢
    @Cacheable(cacheNames="area",key = "#id")
    @GetMapping("getAreaById")
    public Area getAreaById(Integer id) {
        return areaService.getAreaById(id);
    }
	// @CachePut每次都會訪問數(shù)據(jù)庫,并更新緩存
    @CachePut(cacheNames="area",key = "#id")
    @PostMapping("updateAreaById")
    public void updateAreaById(Integer id) {
        areaService.updateAreaById(id);
    }
	// @CacheEvict清除緩存
    @CacheEvict(cacheNames="area",key = "#id")
    @PostMapping("deleteAreaById")
    public void deleteAreaById(Integer id) {
        areaService.deleteAreaById(id);
    }
}

通過postman訪問127.0.0.1:8080/NettyProject/area/getAreaById?id=1,第一次時控制臺輸出sql日志,第二次請求時,控制臺無日志輸出,證明未進行sql查詢,查詢的是@Cacheable(cacheNames="area",key = "#id")緩存的數(shù)據(jù)。

到此這篇關(guān)于SpringBoot使用@Cacheable注解實現(xiàn)緩存功能流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot @Cacheable內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

嵊泗县| 平安县| 新和县| 绍兴县| 肇东市| 公主岭市| 张家界市| 黄浦区| 南京市| 靖宇县| 岫岩| 腾冲县| 家居| 林州市| 贺兰县| 平南县| 张北县| 招远市| 雷波县| 安宁市| 苍南县| 全南县| 万宁市| 临漳县| 兴隆县| 芜湖县| 鹿泉市| 巴林右旗| 马尔康县| 桓仁| 博白县| 蒙城县| 青川县| 天峻县| 三原县| 修文县| 福清市| 石柱| 金阳县| 公主岭市| 周宁县|