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

SpringBoot整合Redis實現(xiàn)緩存分頁數(shù)據(jù)查詢功能

 更新時間:2023年06月20日 10:16:19   作者:互聯(lián)網(wǎng)小阿祥  
類似淘寶首頁,這些商品是從數(shù)據(jù)庫中查出來的嗎,答案肯定不是,本文我們就通過一個案例實操一下,首頁熱點數(shù)據(jù)怎么放到Redis中去查詢,感興趣的同學(xué)可以參考一下

正式觀看本文之前,設(shè)想一個問題,高并發(fā)情況下,首頁列表數(shù)據(jù)怎么做?

類似淘寶首頁,這些商品是從數(shù)據(jù)庫中查出來的嗎?答案肯定不是,在高并發(fā)的情況下,數(shù)據(jù)庫是扛不住的,那么我們要怎么去扛住C端端大并發(fā)量呢,這快我們可以借助Redis,我們知道Redis是一個基于內(nèi)存的NoSQL數(shù)據(jù)庫。學(xué)過操作系統(tǒng)我們都知道,內(nèi)存要比磁盤的效率大的多,那我們Redis就是基于內(nèi)存的,而數(shù)據(jù)庫是基于磁盤的。

我們現(xiàn)在知道要用Redis去做首頁數(shù)據(jù)的分頁,那么我們應(yīng)該用Redis的那種數(shù)據(jù)結(jié)構(gòu)來做呢。

Redis有5種基本的數(shù)據(jù)結(jié)構(gòu),我們這里用list類型做分頁。

在 Redis 中,List(列表)類型是按照元素的插入順序排序的字符串列表。你可以在列表的頭部(左邊)或者尾部(右部)添加新的元素。

ok,那么接下來我們就通過一個案例實操一下,首頁熱點數(shù)據(jù)怎么放到Redis中去查詢。

SpringBoot整合RedisTemplate這里就不做過多介紹啦,大家可以網(wǎng)上找篇博文 整合一下。

<!-- 創(chuàng)建SpringBoot項目加入redis的starter依賴 -->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

編寫ProductService,定于數(shù)據(jù)分頁方法。

public interface ProductService {
    Map<String,Object> productListPage(int current, int size) throws InterruptedException;
}

編寫ProductServiceImpl實現(xiàn)類。

/**
 * @author lixiang
 * @date 2023/6/18 21:01
 */
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    private static final String PRODUCT_LIST_KEY = "product:list";
    private static final List<Product> PRODUCT_LIST;
    //模擬從數(shù)據(jù)庫中查出來的數(shù)據(jù)
    static {
        PRODUCT_LIST = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            Product product = new Product();
            product.setId(UUID.randomUUID().toString().replace("-", ""));
            product.setName("商品名稱:" + i);
            product.setDesc("商品描述:" + i);
            product.setPrice(new BigDecimal(i));
            product.setInventory(2);
            PRODUCT_LIST.add(product);
        }
    }
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Map<String, Object> productListPage(int current, int size) throws InterruptedException {
        //從緩存中拿到分頁數(shù)據(jù)
        List<Product> productList = getProductListByRedis(current, size);
        if (productList == null || productList.size() == 0) {
            log.info("當前緩存中無分頁數(shù)據(jù),當前頁:" + current + ",頁大小:" + size);
            //從數(shù)據(jù)庫中拿到分頁數(shù)據(jù)
            productList = getProductListByDataSource(current, size);
        }
        Map<String, Object> resultMap = new HashMap<>();
        //計算當前總頁數(shù)
        int totalPage = (PRODUCT_LIST.size() + size - 1) / size;
        resultMap.put("total", PRODUCT_LIST.size());
        resultMap.put("data", productList);
        resultMap.put("pages", totalPage);
        return resultMap;
    }
    private List<Product> getProductListByRedis(int current, int size) {
        log.info("從Redis取出商品信息列表,當前頁:" + current + ",頁大小:" + size);
        // 計算總頁數(shù)
        int pages = pages(size);
        // 起始位置
        int start = current <= 0 ? 0 : (current > pages ? (pages - 1) * size : (current - 1) * size);
        // 終止位置
        int end = start+size-1;
        List<Product> list = redisTemplate.opsForList().range(PRODUCT_LIST_KEY, start, end);
        List<Product> productList = list;
        return productList;
    }
    /**
     * 獲取商品信息集合
     *
     * @return
     */
    private List<Product> getProductListByDataSource(int current, int size) throws InterruptedException {
        //模擬從DB查詢需要300ms
        Thread.sleep(300);
        log.info("從數(shù)據(jù)庫取出商品信息列表,當前頁:" + current + ",頁大小:" + size);
        // 計算總頁數(shù)
        int pages = pages(size);
        // 起始位置
        int start = current <= 0 ? 0 : (current > pages ? (pages - 1) * size : (current - 1) * size);
        //數(shù)據(jù)緩存到redis中
        redisTemplate.opsForList().rightPushAll(PRODUCT_LIST_KEY, PRODUCT_LIST);
        //設(shè)置當前key過期時間為1個小時
        redisTemplate.expire(PRODUCT_LIST_KEY,1000*60*60, TimeUnit.MILLISECONDS);
        return PRODUCT_LIST.stream().skip(start).limit(size).collect(Collectors.toList());
    }
    /**
     *  獲取總頁數(shù)
     * @param size
     * @return
     */
    private Integer pages(int size){
        int pages = PRODUCT_LIST.size() % size == 0 ? PRODUCT_LIST.size() / size : PRODUCT_LIST.size() / size + 1;
        return pages;
    }
}

ok,然后編寫controller,進行測試。

@RestController
@RequestMapping("/api/v1/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @GetMapping("/page")
    public Map<String,Object> page(@RequestParam("current") int current,@RequestParam("size") int size){
        Map<String, Object> stringObjectMap;
        try {
            stringObjectMap = productService.productListPage(current, size);
        } catch (InterruptedException e) {
            stringObjectMap = new HashMap<>();
        }
        return stringObjectMap;
    }
}

當?shù)谝淮卧L問的時候,先去Redis中查詢,發(fā)現(xiàn)沒有,然后就去查DB,將要緩存的數(shù)據(jù)頁放到Redis中。

第二次訪問的時候。就直接訪問Redis啦

通過Redis和DB查詢的對比,我們發(fā)現(xiàn)從Redis中拿出來只用了18ms,從公DB中需要300ms,由此可見Redis的一個強大之處。

那么我們觀察一下查詢邏輯,會不會有什么問題。

    public Map<String, Object> productListPage(int current, int size) throws InterruptedException {
        //從緩存中拿到分頁數(shù)據(jù)
        List<Product> productList = getProductListByRedis(current, size);
        if (productList == null || productList.size() == 0) {
            log.info("當前緩存中無分頁數(shù)據(jù),當前頁:" + current + ",頁大小:" + size);
            //從數(shù)據(jù)庫中拿到分頁數(shù)據(jù)
            productList = getProductListByDataSource(current, size);
        }
    }

設(shè)想,假如某一時刻,Redis中的緩存失效啦,大量的請求,全部查到DB上,也會帶來一個災(zāi)難。所以這快又涉及到一個緩存擊穿的問題。

解決緩存擊穿

  • 方案一:永不過期
    • 提前把熱點數(shù)據(jù)不設(shè)置過期時間,后臺異步更新緩存。
  • 方案二:加互斥鎖或隊列
    • 其實我理解緩存擊穿和緩存穿透差不多,所以加一個互斥鎖,讓一個線程正常請求數(shù)據(jù)庫,其他線程等待即可(這里可以使用線程池來處理),都創(chuàng)建完緩存,讓其他線程請求緩存即可。

到此這篇關(guān)于SpringBoot整合Redis實現(xiàn)緩存分頁數(shù)據(jù)查詢功能的文章就介紹到這了,更多相關(guān)SpringBoot Redis數(shù)據(jù)查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

花莲县| 大连市| 和平区| 沁阳市| 元江| 大姚县| 沙湾县| 托克逊县| 乌苏市| 华宁县| 华蓥市| 新干县| 隆尧县| 盐源县| 安徽省| 屏东市| 独山县| 泰和县| 武威市| 仙游县| 红河县| 子洲县| 建湖县| 徐水县| 安泽县| 九寨沟县| 绥芬河市| 右玉县| 获嘉县| 绥滨县| 通城县| 安多县| 兰坪| 高青县| 股票| 即墨市| 建平县| 成武县| 哈密市| 余江县| 湘乡市|