Python-Redis存值分目录层级

import redis

def hash_func(key):
    # 哈希函数的实现
    hash_value = 0
    for c in key:
        hash_value += ord(c)
        print(c)
    print(hash_value)
    return hash_value

def get_directory_name(key):
    hash_value = hash_func(key)
    directory_name = str(hash_value % 1000)
    return directory_name

def store_data(key, value):
    directory_name = get_directory_name(key)
    redis_client.set(directory_name + '/' + key, value)

def get_data(key):
    directory_name = get_directory_name(key)
    value = redis_client.get(directory_name + '/' + key)
    return value

redis_client = redis.Redis(host="redis-master.operation.svc.cluster.local", port=6379,
                           db=10, password="xxxxx", socket_timeout=300, decode_responses=True)

store_data('data1', 'value1')
store_data('data2', 'value2')

value1 = get_data('data1')
value2 = get_data('data2')

print(value1)  # 输出:value1
print(value2)  # 输出:value2