当前位置:网站首页>ThinkPHP+Redis实现简单抽奖
ThinkPHP+Redis实现简单抽奖
2022-07-03 00:56:00 【生命不止、战斗不息】
Redis集合简介
Redis的集合数据类型,功能非常强大。说到集合的话,你可能想到高中数学上的集合。其实他们本就是一个意思。Redis集合里面可以存放很多字符串(元素),Redis最多支持2的32次方减1个元素,但是集合里的元素都是唯一的,不会有重复。和数学里集合一样,Redis也支持交集,并集以及差集。
用它可以完成许多有意思的功能。最常见的就是标签功能了,可能A用户的标签有“动漫”、”运动“、”二次元“,B用户的标签有”运动“、”旅游“、”篮球“。那么,使用集合的并集,就能知道他们的共同标签是什么。另外,当系统知道用户的标签后,就可以给他们推荐相关的广告或产品。除此之外,还要许多有意思的功能它也能实现。今天,我们来看看如何使用Reids实现抽奖功能。
sRandMember、sPop
这两个命令功能非常相似,都是从集合中返回一个元素值。不同的是,sRandMember不会从集合中删除返回的元素,但是sPop会删除。这两个命令可以分别实现不同的抽奖算法。
比如,集合中有100个元素,值从数字1到数字100.我们定义抽到的是数字1的话,即表示中奖。
使用sRandMember的话,不管之前抽过多少次,下次抽中的概率都是1%。而使用sPop的话,则每次抽中的概率都不一样。第一个人抽中概率是1%,当第一个人没抽中的话,第二个人抽中概率就是1/99,以此类推。
抽奖功能实现
抽奖功能实现其实就只有两步,首先设置抽奖概率,即往集合中添加元素,然后就是开始抽奖了。
设置抽奖概率,伪代码如下:
//抽奖活动,获取一个兑换码 定义1-10中奖
public function create($key = 'draw', $type = 'nil')
{
//tp6加载redis
$redis = Cache::store('redis')->handler();
//中奖逻辑
if ($type == 'nil') {
// 普通用户概率小 算法 不会产生重复 去生成兑换码
$num = mt_rand(7, 9999);
} else if ($type == 'VIP') {
$num = mt_rand(3, 1000);
} else if ($type == 'VIP8') {
$num = mt_rand(1, 2);
}
$redis->sAdd($key, $num);
return $num;
}
//开奖 只开奖 不写逻辑
public function read($key, $stand = 10)
{
$bool = $this->draw($key, $stand = 10);
//给用户的开奖结果
return $bool ? '恭喜你中奖了,天选之子' : '很遗憾,差亿点就中奖了';
}
//写开奖逻辑
public function draw($key, $stand)
{
$redis = Cache::store('redis')->handler();
//判断集合是否还有没有开奖内容
if ($redis->scard($key) > 0) {
//真正随机开奖
$number = $redis->spop($key);
} else {
echo "抽奖结束";
exit();
}
echo $number . "<hr/>";
return $number < $stand;
}
注意:路由传参
边栏推荐
- How wide does the dual inline for bread board need?
- RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
- (C language) data storage
- Niu Ke swipes questions and clocks in
- First hand evaluation of Reza electronics rz/g2l development board
- 有向图的强连通分量
- [AUTOSAR nine c/s principle Architecture]
- 机器学习术语
- [overview of AUTOSAR three RTE]
- On Fibonacci sequence
猜你喜欢
matlab 多普勒效应产生振动信号和处理
MySQL
[untitled]
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
[love crash] neglected details of gibaro
【C语言】分支和循环语句(上)
[AUTOSAR eight OS]
How to convert Quanzhi a40i/t3 to can through SPI
Assets, vulnerabilities, threats and events of the four elements of safe operation
MySQL basics 03 introduction to MySQL types
随机推荐
Draw love with go+ to express love to her beloved
JS inheritance and prototype chain
按键精灵打怪学习-多线程后台坐标识别
[untitled]
MySQL basics 03 introduction to MySQL types
[introduction to AUTOSAR seven tool chain]
leetcode:701. Insertion in binary search tree [BST insertion]
[overview of AUTOSAR three RTE]
无向图的割点
MySQL基础用法02
【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
MySQL
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
Daily topic: movement of haystack
leetcode 2097 — 合法重新排列数对
[shutter] animation animation (shutter animation type | the core class of shutter animation)
465. DFS backtracking of optimal bill balance
Several cases of recursive processing organization
Makefile中wildcard、patsubst、notdir的含义
Foundations of data science is free to download