|
@@ -0,0 +1,37 @@
|
|
|
|
|
+#!/usr/bin/env python3
|
|
|
|
|
+# -*- coding:utf-8 -*-
|
|
|
|
|
+import redis
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class Redis(object):
|
|
|
|
|
+ def __init__(self):
|
|
|
|
|
+ self.redis = redis.StrictRedis(host='r-t4nb4n9i8je7u6ogk1pd.redis.singapore.rds.aliyuncs.com',
|
|
|
|
|
+ port=5000,
|
|
|
|
|
+ password='gHmNkVBd88sZybj',
|
|
|
|
|
+ db=10,
|
|
|
|
|
+ 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}")
|