濮阳杆衣贸易有限公司

主頁 > 知識庫 > 基于Redis實現(xiàn)抽獎功能及問題小結

基于Redis實現(xiàn)抽獎功能及問題小結

熱門標簽:山東外呼銷售系統(tǒng)招商 日本中國地圖標注 鄭州人工智能電銷機器人系統(tǒng) 魔獸2青云地圖標注 北京400電話辦理收費標準 十堰營銷電銷機器人哪家便宜 宿遷便宜外呼系統(tǒng)平臺 超呼電話機器人 貴州電銷卡外呼系統(tǒng)

1、分析

  • 公司年底要做年會所有的員工都要參與抽獎的環(huán)節(jié)
  • 平臺的產品要進行抽獎活動

這個時候我們可以利用redis中的set集合中的spop來實現(xiàn)。

特征:抽獎成功的人會自動從集合中刪除,即獲取到獎品的人不再繼續(xù)參與抽獎。

spop命令:隨機返回元素,元素從集合中刪除該元素

2、初始化名單數(shù)據(jù)

package com.example.service;

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;

/**
 * @Auther: 長頸鹿
 * @Date: 2021/08/21/14:09
 * @Description:
 */
@Service
@Slf4j
public class SpopRandomSetService {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String SPOP_USER_SETS = "pop:user:set";

    // 把所有員工全部添加到集合列表中
    @PostConstruct
    public void initData(){
        log.info("初始化獎品等級信息...");
        // 判斷集合是否已經存在
        boolean flag = this.redisTemplate.hasKey(SPOP_USER_SETS);
        // 防止作弊
        if (!flag) {
            // 獲取所有員工的信息
            ListInteger> initDataList = initDataList();
            // 把員工信息寫入到redis中 sadd key data
            initDataList.forEach(data -> this.redisTemplate.opsForSet().add(SPOP_USER_SETS, data));
        }
    }

    // 模擬100用戶抽獎
    private ListInteger> initDataList() {
        // todo : 從數(shù)據(jù)庫里面來,把公司里面所有的員工從數(shù)據(jù)表中全部查詢出來
        ListInteger> listData = new ArrayList>();
        for (int i = 0; i  100; i++) {
            listData.add(i + 1);
        }
        return listData;
    }

}

3、具體抽獎方法

// 隨機抽取用戶
    public int start(){
        return (int)redisTemplate.opsForSet().pop(SPOP_USER_SETS);
    }

4、抽獎接口測試

package com.example.controller;

import com.example.service.SpopRandomSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: 長頸鹿
 * @Date: 2021/08/21/14:13
 * @Description: 抽獎接口測試
 */
@RestController
public class SpopRandomSetController {

    @Autowired
    private SpopRandomSetService spopRandomSetService;

    @PostMapping("/sPop/random/user")
    public int start() {
        return spopRandomSetService.start();
    }

}

5、小結

# 查詢集合成員
smembers pop:user:Set
# 查詢集合的長度變化
scard pop:user:Set

spop:隨機從集合取出一個元素返回,并且從集合中刪除該元素。

到此這篇關于基于Redis實現(xiàn)抽獎功能的文章就介紹到這了,更多相關Redis實現(xiàn)抽獎內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Redis實戰(zhàn)之商城購物車功能的實現(xiàn)代碼
  • java redis 實現(xiàn)簡單的用戶簽到功能
  • 使用redis的increment()方法實現(xiàn)計數(shù)器功能案例
  • Java使用Redis實現(xiàn)秒殺功能
  • 多個SpringBoot項目采用redis實現(xiàn)Session共享功能
  • 使用Redis實現(xiàn)微信步數(shù)排行榜功能

標簽:果洛 楊凌 江蘇 臺州 大慶 吉安 朝陽 北京

巨人網絡通訊聲明:本文標題《基于Redis實現(xiàn)抽獎功能及問題小結》,本文關鍵詞  基于,Redis,實現(xiàn),抽獎,功,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《基于Redis實現(xiàn)抽獎功能及問題小結》相關的同類信息!
  • 本頁收集關于基于Redis實現(xiàn)抽獎功能及問題小結的相關信息資訊供網民參考!
  • 推薦文章
    惠安县| 邳州市| 玉门市| 巴楚县| 隆化县| 黎川县| 永济市| 山阴县| 饶平县| 田林县| 祥云县| 象州县| 车致| 城市| 清河县| 邹平县| 阳江市| 阿瓦提县| 太仓市| 龙岩市| 广宁县| 南部县| 昌平区| 巴彦县| 秀山| 武夷山市| 饶平县| 金湖县| 祥云县| 长春市| 邢台市| 隆林| 阿拉善左旗| 镇赉县| 车险| 通化市| 怀安县| 白沙| 夏河县| 祁东县| 临猗县|