比对redis数据是否一致

import redis


# 连接redis
def redis_different_keys():
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
    pool2 = redis.ConnectionPool(host='39.105.69.93', port=6379, db=0)
    r = redis.StrictRedis(connection_pool=pool)
    r2 = redis.StrictRedis(connection_pool=pool2)
    # 获取所有的key
    keys = r.keys()
    keys2 = r2.keys()
    # 建一个空列表用来存放不一致的key
    list01 = []
    for i in keys:
        # print('当前的key:', i.decode('utf-8'))
        # 将i的类型解码
        type = r.type(i).decode('utf-8')
        # print('类型', type)
        # 判断i的类型
        if type == 'string':
            # 获取value
            value1 = r.get(i)
            value2 = r2.get(i)
            # 判断两个value是否相等
            # 相等则跳过,不想等则加入列表
            if value1 == value2:
                continue
            else:
                list01.append(i)
        elif type == 'list':
            value1 = r.lrange(i, 0, -1)
            value2 = r2.lrange(i, 0, -1)
            if value1 == value2:
                continue
            else:
                list01.append(i)
        elif type == 'set':
            value1 = r.smembers(i)
            value2 = r2.smembers(i)
            if value1 == value2:
                continue
            else:
                list01.append(i)
        elif type == 'zset':
            value1 = r.zrange(i, 0, -1)
            value2 = r2.zrange(i, 0, -1)
            if value1 == value2:
                continue
            else:
                list01.append(i)
        elif type == 'hash':
            value1 = r.hgetall(i)
            value2 = r2.hgetall(i)
            if value1 == value2:
                continue
            else:
                list01.append(i)
    # print(list01)
    return list01


if __name__ == '__main__':
    print(redis_different_keys())

上一篇:Unity碰撞检测实操


下一篇:Unity 镜子效果