欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Redis的List實(shí)現(xiàn)特價(jià)商品列表功能

 更新時(shí)間:2021年08月30日 10:35:26   作者:南宮拾壹  
本文通過(guò)場(chǎng)景分析給大家介紹了基于Redis的List實(shí)現(xiàn)特價(jià)商品列表,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

 1、場(chǎng)景分析

淘寶京東的特價(jià)商品列表,

商品特點(diǎn):

  • 商品有限,并發(fā)量非常的大。
  • 考慮分頁(yè)

傳統(tǒng)解決方案:數(shù)據(jù)庫(kù)db,

但是在如此大的并發(fā)量的情況下,不可取。

一般會(huì)采用redis來(lái)處理。這些特價(jià)商品的數(shù)據(jù)不多,而且redis的list本身也支持分頁(yè)。是天然處理這種列表的最佳選擇解決方案。

2、分析

采用list數(shù)據(jù),因?yàn)閘ist數(shù)據(jù)結(jié)構(gòu)有:lrange key 0 -1 可以進(jìn)行數(shù)據(jù)的分頁(yè)。

127.0.0.1:6379> lpush products p1 p2 p3 p4 p5 p6 p7 p8 p9 p10
(integer) 10
127.0.0.1:6379> lrange products 0 1
1) "p10"
2) "p9"
127.0.0.1:6379> lrange products 2 3
1) "p8"
2) "p7"
127.0.0.1:6379> lrange products 4 5
1) "p6"
2) "p5"

3 、具體實(shí)現(xiàn)

淘寶,京東的熱門(mén)商品在雙11的時(shí)候,可能有100多w需要搞活動(dòng):程序需要5分鐘對(duì)特價(jià)商品進(jìn)行刷新。

3.1 ProductListService類(lèi)

  •  初始化的活動(dòng)的商品信息100個(gè)(從數(shù)據(jù)庫(kù)去查詢(xún))

@PostContrcut使用

  •  查詢(xún)產(chǎn)品列表信息

換算的分頁(yè)的起始位置和結(jié)束位置

package com.example.service;

import com.example.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Auther: 長(zhǎng)頸鹿
 * @Date: 2021/08/29/18:00
 * @Description:
 */
@Service
@Slf4j
public class ProductListService {

    @Autowired
    private RedisTemplate redisTemplate;

    // 數(shù)據(jù)熱加載
    @PostConstruct
    public void initData(){
        log.info("啟動(dòng)定時(shí)加載特價(jià)商品到redis的list中...");
        new Thread(() -> runCourse()).start();
    }

    public void runCourse() {
        while (true) {
            // 從數(shù)據(jù)庫(kù)中查詢(xún)出特價(jià)商品
            List<Product> productList = this.findProductsDB();
            // 刪除原來(lái)的特價(jià)商品
            this.redisTemplate.delete("product:hot:list");
            // 把特價(jià)商品添加到集合中
            this.redisTemplate.opsForList().leftPushAll("product:hot:list", productList);
            try {
                // 每隔一分鐘執(zhí)行一次
                Thread.sleep(1000 * 60);
                log.info("定時(shí)刷新特價(jià)商品....");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 數(shù)據(jù)庫(kù)中查詢(xún)特價(jià)商品
     *
     * @return
     */
    public List<Product> findProductsDB() {
        //List<Product> productList = productMapper.selectListHot();
        List<Product> productList = new ArrayList<>();
        for (long i = 1; i <= 100; i++) {
            Product product = new Product();
            product.setId((long) new Random().nextInt(1000));
            product.setPrice((double) i);
            product.setTitle("特價(jià)商品" + (i));
            productList.add(product);
        }
        return productList;
    }

}

3.2 商品的數(shù)據(jù)接口的定義和展示及分頁(yè)

package com.example.controller;

import com.example.entity.Product;
import com.example.service.ProductListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: 長(zhǎng)頸鹿
 * @Date: 2021/08/29/18:04
 * @Description:
 */
@RestController
public class ProductListController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ProductListService productListService;

    @GetMapping("/findProducts")
    public List<Product> findProducts(int pageNo, int pageSize) {

        // 從那個(gè)集合去查詢(xún)
        String key = "product:hot:list";
        // 分頁(yè)的開(kāi)始結(jié)束的換算
        if (pageNo <= 0) pageNo = 1;
        int start = (pageNo - 1) * pageSize;
        // 計(jì)算分頁(yè)的結(jié)束頁(yè)
        int end = start + pageSize - 1;

        // 根據(jù)redis的api去處理分頁(yè)查詢(xún)對(duì)應(yīng)的結(jié)果
        try {
            List<Product> productList = this.redisTemplate.opsForList().range(key, start, end);
            if (CollectionUtils.isEmpty(productList)) {
                //todo: 查詢(xún)數(shù)據(jù)庫(kù),存在緩存擊穿的情況,大量的并發(fā)請(qǐng)求進(jìn)來(lái),可能把數(shù)據(jù)庫(kù)沖
                productList = productListService.findProductsDB();
            }
            return productList;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

}

3.3 定時(shí)任務(wù)

@Configuration      // 主要用于標(biāo)記配置類(lèi),兼?zhèn)銫omponent的效果。
@EnableScheduling   // 開(kāi)啟定時(shí)任務(wù)
public class SaticScheduleTask {
    // 添加定時(shí)任務(wù)
    @Scheduled(cron = "* 0/5 * * * ?")
    // 或直接指定時(shí)間間隔,例如:5秒
    // @Scheduled(fixedRate=5000)
    private void configureTasks() {
        System.err.println("執(zhí)行靜態(tài)定時(shí)任務(wù)時(shí)間: " + LocalDateTime.now());
    }
}

4、解決商品列表存在的緩存擊穿問(wèn)題

 4.1 如何引起的緩存擊穿的情況

public void runCourse() {
        while (true) {
            // 從數(shù)據(jù)庫(kù)中查詢(xún)出特價(jià)商品
            List<Product> productList = this.findProductsDB();
            // 刪除原來(lái)的特價(jià)商品
            this.redisTemplate.delete("product:hot:list");
            // 把特價(jià)商品添加到集合中 需要時(shí)間
            this.redisTemplate.opsForList().leftPushAll("product:hot:list", productList);
            try {
                // 每隔一分鐘執(zhí)行一遍
                Thread.sleep(1000 * 60);
                log.info("定時(shí)刷新特價(jià)商品....");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

出現(xiàn)原因:

  • 特價(jià)商品的數(shù)據(jù)更換需要時(shí)間,剛好特價(jià)商品還沒(méi)有放入到redis緩存中。
  • 查詢(xún)特價(jià)商品的并發(fā)量非常大,可能程序還正在寫(xiě)入特價(jià)商品到緩存中,這時(shí)查詢(xún)緩存根本沒(méi)有數(shù)據(jù),就會(huì)直接沖入數(shù)據(jù)庫(kù)中去查詢(xún)特價(jià)商品??赡茉斐蓴?shù)據(jù)庫(kù)沖垮。這個(gè)就叫做:緩存擊穿

4.2 解決方案

主從輪詢(xún)

可以開(kāi)辟兩塊redis的集合空間A和B。定時(shí)器在更新緩存的時(shí)候,先更新B緩存,然后再更新A緩存。

一定要按照特定順序來(lái)處理。

package com.example.service;

import com.example.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Auther: 長(zhǎng)頸鹿
 * @Date: 2021/08/29/18:00
 * @Description:
 */
@Service
@Slf4j
public class ProductListService {

    @Autowired
    private RedisTemplate redisTemplate;

    // 數(shù)據(jù)熱加載
    @PostConstruct
    public void initData(){
        log.info("啟動(dòng)定時(shí)加載特價(jià)商品到redis的list中...");
        new Thread(() -> runCourse()).start();
    }

    public void runCourse() {
        while (true) {
            // 從數(shù)據(jù)庫(kù)中查詢(xún)出特價(jià)商品
            List<Product> productList = this.findProductsDB();

            // 刪除原來(lái)的特價(jià)商品
            this.redisTemplate.delete("product:hot:slave:list");
            // 把特價(jià)商品添加到集合中
            this.redisTemplate.opsForList().leftPushAll("product:hot:slave:list", productList);// 刪除原來(lái)的特價(jià)商品

            this.redisTemplate.delete("product:hot:master:list");
            // 把特價(jià)商品添加到集合中
            this.redisTemplate.opsForList().leftPushAll("product:hot:master:list", productList);

//            // 刪除原來(lái)的特價(jià)商品
//            this.redisTemplate.delete("product:hot:list");
//            // 把特價(jià)商品添加到集合中
//            this.redisTemplate.opsForList().leftPushAll("product:hot:list", productList);
            try {
                // 每隔一分鐘執(zhí)行一次
                Thread.sleep(1000 * 60);
                log.info("定時(shí)刷新特價(jià)商品....");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 數(shù)據(jù)庫(kù)中查詢(xún)特價(jià)商品
     *
     * @return
     */
    public List<Product> findProductsDB() {
        //List<Product> productList = productMapper.selectListHot();
        List<Product> productList = new ArrayList<>();
        for (long i = 1; i <= 100; i++) {
            Product product = new Product();
            product.setId((long) new Random().nextInt(1000));
            product.setPrice((double) i);
            product.setTitle("特價(jià)商品" + (i));
            productList.add(product);
        }
        return productList;
    }

}
package com.example.controller;

import com.example.entity.Product;
import com.example.service.ProductListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: 長(zhǎng)頸鹿
 * @Date: 2021/08/29/18:04
 * @Description:
 */
@RestController
public class ProductListController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ProductListService productListService;

    @GetMapping("/findProducts")
    public List<Product> findProducts(int pageNo, int pageSize) {

        // 從那個(gè)集合去查詢(xún)

        String master_key = "product:hot:master:list";
        String slave_key = "product:hot:slave:list";

        String key = "product:hot:list";
        // 分頁(yè)的開(kāi)始結(jié)束的換算
        if (pageNo <= 0) pageNo = 1;
        int start = (pageNo - 1) * pageSize;
        // 計(jì)算分頁(yè)的結(jié)束頁(yè)
        int end = start + pageSize - 1;

        // 根據(jù)redis的api去處理分頁(yè)查詢(xún)對(duì)應(yīng)的結(jié)果
        try {

            List<Product> productList = this.redisTemplate.opsForList().range(master_key, start, end);

//            List<Product> productList = this.redisTemplate.opsForList().range(key, start, end);
            if (CollectionUtils.isEmpty(productList)) {
                // todo: 查詢(xún)數(shù)據(jù)庫(kù),存在緩存擊穿的情況,大量的并發(fā)請(qǐng)求進(jìn)來(lái),可能把數(shù)據(jù)庫(kù)沖

                productList = this.redisTemplate.opsForList().range(slave_key, start, end);

//                productList = productListService.findProductsDB();
            }
            return productList;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

}

到此這篇關(guān)于基于Redis的List實(shí)現(xiàn)特價(jià)商品列表的文章就介紹到這了,更多相關(guān)redis list商品列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis基礎(chǔ)學(xué)習(xí)之管道機(jī)制詳析

    Redis基礎(chǔ)學(xué)習(xí)之管道機(jī)制詳析

    這篇文章主要給大家介紹了關(guān)于Redis基礎(chǔ)學(xué)習(xí)之管道機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • 搭建單機(jī)Redis緩存服務(wù)的實(shí)現(xiàn)

    搭建單機(jī)Redis緩存服務(wù)的實(shí)現(xiàn)

    本文主要介紹了搭建單機(jī)Redis緩存服務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 利用Redis的有序集合實(shí)現(xiàn)排行榜功能實(shí)例代碼

    利用Redis的有序集合實(shí)現(xiàn)排行榜功能實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于如何利用Redis的有序集合實(shí)現(xiàn)排行榜功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Redis中的zset類(lèi)型詳解

    Redis中的zset類(lèi)型詳解

    有序集合zset保留了set集合不能有重復(fù)成員的特點(diǎn),但與set集合不同的是,zset的每個(gè)member都有一個(gè)唯一的浮點(diǎn)數(shù)類(lèi)型的分?jǐn)?shù)score與之關(guān)聯(lián),這篇文章主要介紹了Redis的zset類(lèi)型,需要的朋友可以參考下
    2023-08-08
  • 詳解Redis緩存預(yù)熱的實(shí)現(xiàn)方法

    詳解Redis緩存預(yù)熱的實(shí)現(xiàn)方法

    緩存預(yù)熱是一種在程序啟動(dòng)或緩存失效之后,主動(dòng)將熱點(diǎn)數(shù)據(jù)加載到緩存中的策略,本文將給大家分享一下如何實(shí)現(xiàn)Redis的緩存預(yù)熱,文中有詳細(xì)的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2023-10-10
  • Redis 實(shí)現(xiàn)分布式鎖時(shí)需要考慮的問(wèn)題解決方案

    Redis 實(shí)現(xiàn)分布式鎖時(shí)需要考慮的問(wèn)題解決方案

    本文詳細(xì)探討了使用Redis實(shí)現(xiàn)分布式鎖時(shí)需要考慮的問(wèn)題,包括鎖的競(jìng)爭(zhēng)、鎖的釋放、超時(shí)管理、網(wǎng)絡(luò)分區(qū)等,并提供了相應(yīng)的解決方案和代碼實(shí)例,有助于開(kāi)發(fā)者正確且安全地使用Redis實(shí)現(xiàn)分布式鎖
    2024-09-09
  • windows 64位下redis安裝教程

    windows 64位下redis安裝教程

    這篇文章主要為大家詳細(xì)介紹了windows 64位下redis安裝教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 關(guān)于Redis數(shù)據(jù)庫(kù)入門(mén)詳細(xì)介紹

    關(guān)于Redis數(shù)據(jù)庫(kù)入門(mén)詳細(xì)介紹

    大家好,本篇文章主要講的是關(guān)于Redis數(shù)據(jù)庫(kù)入門(mén)詳細(xì)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽
    2021-12-12
  • 如何利用Redis鎖解決高并發(fā)問(wèn)題詳解

    如何利用Redis鎖解決高并發(fā)問(wèn)題詳解

    redis鎖處理高并發(fā)問(wèn)題十分常見(jiàn),下面這篇文章主要給大家介紹了關(guān)于如何使用Redis鎖解決高并發(fā)問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Windows下搭建Redis哨兵集群模式的方法步驟

    Windows下搭建Redis哨兵集群模式的方法步驟

    哨兵模式,是基于主從復(fù)制模式,主從復(fù)制的優(yōu)點(diǎn)全都擁有,并且主從可以實(shí)現(xiàn)自動(dòng)切換,故障轉(zhuǎn)移等功能,本文主要介紹了Windows下搭建Redis哨兵集群模式的方法步驟,文中通過(guò)圖文介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下,需要的朋友可以參考下
    2023-09-09

最新評(píng)論