| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- #!/usr/bin/env python3
- # -*- coding:utf-8 -*-
- import redis
- from config import load_config
- cfgs = load_config()
- class Redis(object):
- def __init__(self):
- self.redis = redis.StrictRedis(host=cfgs['redis']['host'],
- port=cfgs['redis']['port'],
- password=cfgs['redis']['passwd'],
- db=cfgs['redis']['db'],
- decode_responses=True)
- if __name__ == '__main__':
- import random
- # 连接到 Redis 服务器
- r = Redis().redis
- # 有序集合的键名
- zset_key = 'hotkeys'
- data_list = ['ORDER_FULLORDR_RATE', 'MONTH6_SALE_QTY_YOY', 'MONTH6_SALE_QTY_MOM', 'MONTH6_SALE_QTY']
- # 清空已有的有序集合(可选,若需要全新的集合可执行此操作)
- r.delete(zset_key)
-
- for item in data_list:
- # 生成 80 到 100 之间的随机数,小数点后保留 4 位
- score = round(random.uniform(80, 100), 4)
- # 将元素和对应的分数添加到有序集合中
- r.zadd(zset_key, {item: score})
- # # 从 Redis 中读取有序集合并打印
- # result = r.zrange(zset_key, 0, -1, withscores=True)
- # for item, score in result:
- # print(f"元素: {item}, 分数: {score}")
|