基于Redis實(shí)現(xiàn)抽獎(jiǎng)功能及問(wèn)題小結(jié)
1、分析
- 公司年底要做年會(huì)所有的員工都要參與抽獎(jiǎng)的環(huán)節(jié)
- 平臺(tái)的產(chǎn)品要進(jìn)行抽獎(jiǎng)活動(dòng)
這個(gè)時(shí)候我們可以利用redis中的set集合中的spop來(lái)實(shí)現(xiàn)。
特征:抽獎(jiǎng)成功的人會(huì)自動(dòng)從集合中刪除,即獲取到獎(jiǎng)品的人不再繼續(xù)參與抽獎(jiǎng)。
spop命令:隨機(jī)返回元素,元素從集合中刪除該元素
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: 長(zhǎng)頸鹿 * @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("初始化獎(jiǎng)品等級(jí)信息..."); // 判斷集合是否已經(jīng)存在 boolean flag = this.redisTemplate.hasKey(SPOP_USER_SETS); // 防止作弊 if (!flag) { // 獲取所有員工的信息 List<Integer> initDataList = initDataList(); // 把員工信息寫(xiě)入到redis中 sadd key data initDataList.forEach(data -> this.redisTemplate.opsForSet().add(SPOP_USER_SETS, data)); } } // 模擬100用戶(hù)抽獎(jiǎng) private List<Integer> initDataList() { // todo : 從數(shù)據(jù)庫(kù)里面來(lái),把公司里面所有的員工從數(shù)據(jù)表中全部查詢(xún)出來(lái) List<Integer> listData = new ArrayList<>(); for (int i = 0; i < 100; i++) { listData.add(i + 1); } return listData; } }
3、具體抽獎(jiǎng)方法
// 隨機(jī)抽取用戶(hù) public int start(){ return (int)redisTemplate.opsForSet().pop(SPOP_USER_SETS); }
4、抽獎(jiǎng)接口測(cè)試
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: 長(zhǎng)頸鹿 * @Date: 2021/08/21/14:13 * @Description: 抽獎(jiǎng)接口測(cè)試 */ @RestController public class SpopRandomSetController { @Autowired private SpopRandomSetService spopRandomSetService; @PostMapping("/sPop/random/user") public int start() { return spopRandomSetService.start(); } }
5、小結(jié)
# 查詢(xún)集合成員 smembers pop:user:Set # 查詢(xún)集合的長(zhǎng)度變化 scard pop:user:Set
spop:隨機(jī)從集合取出一個(gè)元素返回,并且從集合中刪除該元素。
到此這篇關(guān)于基于Redis實(shí)現(xiàn)抽獎(jiǎng)功能的文章就介紹到這了,更多相關(guān)Redis實(shí)現(xiàn)抽獎(jiǎng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis教程(二):String數(shù)據(jù)類(lèi)型
這篇文章主要介紹了Redis教程(二):String數(shù)據(jù)類(lèi)型,本文講解了String數(shù)據(jù)類(lèi)型概述、相關(guān)命令列表、命令使用示例三部分內(nèi)容,需要的朋友可以參考下2015-04-04Redis批量生成數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了Redis批量生成數(shù)據(jù)的實(shí)現(xiàn),主要介紹了兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06redis stream 實(shí)現(xiàn)消息隊(duì)列的實(shí)踐
本文主要介紹了redis stream 實(shí)現(xiàn)消息隊(duì)列的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08基于Redis?zSet實(shí)現(xiàn)滑動(dòng)窗口對(duì)短信進(jìn)行防刷限流的問(wèn)題
這篇文章主要介紹了基于Redis?zSet實(shí)現(xiàn)滑動(dòng)窗口對(duì)短信進(jìn)行防刷限流,主要針對(duì)目前線上短信被腳本惡意盜刷的情況,用Redis實(shí)現(xiàn)滑動(dòng)窗口限流,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-02-02Redis偶發(fā)連接失敗案例實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于Redis偶發(fā)連接失敗的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使Redis具有一定的參考學(xué)習(xí)價(jià)值,用需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10redis數(shù)據(jù)的兩種持久化方式對(duì)比
Redis是我們開(kāi)發(fā)中常用的數(shù)據(jù)庫(kù),今天和大家分享的就是redis持久化的2種方式:RDB(Redis DataBase)和AOF(Apend Only File),希望對(duì)大家學(xué)習(xí)redis有幫助,一起來(lái)看看吧。2017-08-08淺析Redis中String數(shù)據(jù)類(lèi)型及其底層編碼
這篇文章主要介紹?Redis?中?String?數(shù)據(jù)類(lèi)型及其底層編碼,文中有詳細(xì)的代碼示例,對(duì)大家的工作及學(xué)習(xí)有一定的幫助,需要的朋友可以參考下2023-05-05