通过python将商品信息导入到redis,并进行秒杀系统的模拟id,品类,数量,描述 1,汽水,5,饮料 2,薯片,4,零食 3,汽水,2,饮料 4,薯片,1,零食 5,薯片,3,零食 6,汽水,5,饮料 7,橙汁,7,饮料 8,汽水,8,饮料 9,橙汁,9,饮料 10,蛋糕,7,零食 11,汽水,8,饮料 12,蛋糕,8,零食 13,蛋糕,8,零食 14,蛋糕,8,零食 15,奶茶,6,饮料 16,奶茶,3,饮料 17,汽水,10,饮料 18,奶茶,5,饮料 19,汽水,8,饮料 20,奶茶,9,饮料
时间: 2023-08-22 11:10:11 浏览: 44
好的,下面是将商品信息导入到 Redis 并进行秒杀系统的 Python 代码:
```python
import redis
# 连接 Redis
r = redis.Redis(host='localhost', port=6379, db=0)
# 商品信息列表
goods_list = [
{'id': 1, 'name': '汽水', 'quantity': 5, 'category': '饮料'},
{'id': 2, 'name': '薯片', 'quantity': 4, 'category': '零食'},
{'id': 3, 'name': '汽水', 'quantity': 2, 'category': '饮料'},
{'id': 4, 'name': '薯片', 'quantity': 1, 'category': '零食'},
{'id': 5, 'name': '薯片', 'quantity': 3, 'category': '零食'},
{'id': 6, 'name': '汽水', 'quantity': 5, 'category': '饮料'},
{'id': 7, 'name': '橙汁', 'quantity': 7, 'category': '饮料'},
{'id': 8, 'name': '汽水', 'quantity': 8, 'category': '饮料'},
{'id': 9, 'name': '橙汁', 'quantity': 9, 'category': '饮料'},
{'id': 10, 'name': '蛋糕', 'quantity': 7, 'category': '零食'},
{'id': 11, 'name': '汽水', 'quantity': 8, 'category': '饮料'},
{'id': 12, 'name': '蛋糕', 'quantity': 8, 'category': '零食'},
{'id': 13, 'name': '蛋糕', 'quantity': 8, 'category': '零食'},
{'id': 14, 'name': '蛋糕', 'quantity': 8, 'category': '零食'},
{'id': 15, 'name': '奶茶', 'quantity': 6, 'category': '饮料'},
{'id': 16, 'name': '奶茶', 'quantity': 3, 'category': '饮料'},
{'id': 17, 'name': '汽水', 'quantity': 10, 'category': '饮料'},
{'id': 18, 'name': '奶茶', 'quantity': 5, 'category': '饮料'},
{'id': 19, 'name': '汽水', 'quantity': 8, 'category': '饮料'},
{'id': 20, 'name': '奶茶', 'quantity': 9, 'category': '饮料'}
]
# 将商品信息导入 Redis
for goods in goods_list:
r.hmset(goods['id'], goods)
# 模拟秒杀系统
def seckill(goods_id, quantity):
# 获取商品信息
goods_info = r.hgetall(goods_id)
# 判断商品是否存在
if not goods_info:
print('商品不存在!')
return False
# 判断商品数量是否足够
if int(goods_info[b'quantity']) < quantity:
print('商品数量不足!')
return False
# 减少商品数量
r.hincrby(goods_id, 'quantity', -quantity)
print('秒杀成功!')
return True
# 调用秒杀函数进行测试
seckill(1, 2) # 秒杀成功!
seckill(1, 6) # 商品数量不足!
seckill(21, 2) # 商品不存在!
```
以上代码中,通过 Redis 的 `hmset` 命令将商品信息导入 Redis,每个商品使用一个哈希表来存储,哈希表的键名为商品的 ID,键值为商品的信息。在模拟秒杀系统中,使用 Redis 的 `hgetall` 命令获取商品信息,然后使用 `hincrby` 命令减少商品数量。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)