使用python对redis备份zset
生活随笔
收集整理的這篇文章主要介紹了
使用python对redis备份zset
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 #coding:utf8
2 import redis
3 import json
4
5
6 '''
7 用于備份zset,zset分兩種:score值唯一的zset、score值不唯一的zset
8 使用時(shí)注意:不要在備份的同時(shí)對(duì)old_key執(zhí)行添加和刪除操作
9 '''
10
11 redis1 = redis.StrictRedis.from_url("redis地址")
12 old_key = 'old_zset_name'
13 new_key = 'new_zset_name'
14
15
16 def zset_to_zset():
17 total = redis1.zcard(old_key)
18 start = 0
19 step = 10000
20 while start < total:
21 print start
22 end = start + step - 1
23 if end > total:
24 end = total
25 lis = redis1.zrange(old_key, start, end)
26 if lis:
27 dic = dict().fromkeys(lis, 0)
28 redis1.zadd(new_key, **dic)
29 start += step
30 # redis1.delete(old_key)
31 print 'all finished!'
32
33 def export_to_list(score, error_name):
34 total = redis1.zcount(old_key, score, score) # 查詢對(duì)應(yīng)score值的鏈接數(shù)量
35 start = 0
36 step = 10000
37 while start < total:
38 print start
39 url_list = redis1.zrangebyscore(old_key, score, score, start, step)
40 if url_list:
41 dic = dict().fromkeys(url_list, int(score))
42 redis1.zadd(new_key, **dic)
43 start += step
44
45 def copy_error():
46 error_dic = redis1.hget('hash_dic', old_key)
47 error_dic = json.loads(error_dic)
48 for score, error_name in error_dic.items():
49 export_to_list(score, error_name)
50 error_dic = json.dumps(error_dic)
51 redis1.hset('hash_dic', new_key, error_dic) # 將new_key存到hash中
52 # redis1.delete(old_key)
53 print 'all finished!'
54
55 if __name__ == '__main__':
56 # zset_to_zset() # score值唯一的zset備份
57 copy_error() # score值不唯一的zset備份
對(duì)'hash_dic'的說(shuō)明:之所以將score值存到一個(gè)hash中,是因?yàn)椴煌膕core值代表不同的意義。例如:
1 1 time false 2 2 geo false 3 3 startDate false?少于150字是什么鬼。。。
轉(zhuǎn)載于:https://www.cnblogs.com/sniffing-the-rose/p/6089049.html
總結(jié)
以上是生活随笔為你收集整理的使用python对redis备份zset的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 代理 XP”组件已作为此服务器安全配置的
- 下一篇: 如何安装使用PHP MyAdmin