赞
踩
本文为博主原创,未经授权,严禁转载及使用。
本文链接:https://blog.csdn.net/zyooooxie/article/details/112484045
之前写过一篇 使用redis-py来操作redis集群, https://blog.csdn.net/zyooooxie/article/details/123760358 ,这期来分享下 使用redis-py-cluster;
【实际这篇博客推迟发布N个月】
个人博客:https://blog.csdn.net/zyooooxie
【以下所有内容仅为个人项目经历,如有不同,纯属正常】
https://pypi.org/project/redis-py-cluster/
This major version of redis-py-cluster supports redis-py >=3.0.0, <4.0.0.
""" @blog: https://blog.csdn.net/zyooooxie @qq: 153132336 @email: zyooooxie@gmail.com """ import traceback from rediscluster import ClusterConnectionPool from rediscluster import RedisCluster from xxx_test.user_log import Log host2 = '' p1wd = '' port = 1234 gl_key_name = 'TEST_xie*' Log.info('------') gl_real_string = '' gl_real_hash = '' gl_real_list = '' gl_real_set = '' gl_no_exist = 'TEST_zyooooxie' gl_test_str = 'test_str' gl_test_hash = 'test_hash' gl_test_list = 'test_list' gl_test_set = 'test_set' Log.info('------') # pip install redis-py-cluster==2.1.3 # https://redis-py-cluster.readthedocs.io/en/2.1.3/index.html def redis_py_cluster_connect_1(): rc = RedisCluster(startup_nodes=[{'host': host2, 'port': port}], decode_responses=True, password=pwd) Log.info('{}'.format(rc)) Log.info(type(rc)) Log.error('已连接') return rc def redis_py_cluster_connect_2(): ccp = ClusterConnectionPool(startup_nodes=[{'host': host2, 'port': port}], decode_responses=True, password=pwd) rc = RedisCluster(connection_pool=ccp) Log.info('{}'.format(rc)) Log.info(type(rc)) Log.error('已连接') return rc def redis_py_cluster_connect_3(): rc = RedisCluster(host=host2, port=port, decode_responses=True, password=pwd) Log.info('{}'.format(rc)) Log.info(type(rc)) Log.error('已连接') return rc
""" @blog: https://blog.csdn.net/zyooooxie @qq: 153132336 @email: zyooooxie@gmail.com """ def cluster_commands(rc: RedisCluster): Log.info(rc.cluster_info()) Log.info(rc.cluster_slots()) Log.info(rc.cluster_nodes()) Log.info('------') exist_key_slot = rc.cluster_keyslot(gl_real_string) # 计算key 应该被放置在哪个slot Log.info(exist_key_slot) Log.info(rc.cluster_countkeysinslot(exist_key_slot)) # 返回 slot 目前包含的键值对数量 Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 0)) # 返回 n 个 slot的键 Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 1)) Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 2)) Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 3)) Log.info('------') Log.info(rc.cluster_keyslot(gl_real_hash)) Log.info(rc.cluster_keyslot(gl_real_list)) Log.info(rc.cluster_keyslot(gl_real_set)) Log.info(rc.cluster_keyslot(gl_no_exist))
""" @blog: https://blog.csdn.net/zyooooxie @qq: 153132336 @email: zyooooxie@gmail.com """ def keys_commands(rc: RedisCluster): data_list = rc.keys(gl_key_name) Log.info(len(data_list)) Log.info(type(data_list)) Log.error('------') def scan_commands(rc: RedisCluster): data_list = list() cursor = 0 # args = rc.scan(cursor, gl_key_name, count=5000) # Log.info(args) # 返回值有问题 # https://redis-py-cluster.readthedocs.io/en/2.1.3/commands.html#keys-generic # SCAN command has currently a buggy client side implementation. # # It is not recommended to use any *SCAN methods. # 不建议使用任何*SCAN方法。 Log.error('------') def scan_iter_method(rc: RedisCluster): args = rc.scan_iter(gl_key_name, count=5000) Log.info(args) Log.info(type(args)) data = list(args) Log.info(len(data)) Log.info(data[-10:])
""" @blog: https://blog.csdn.net/zyooooxie @qq: 153132336 @email: zyooooxie@gmail.com """ def cluster_str(rc: RedisCluster): """ :param rc: :return: """ # https://redis.io/docs/data-types/strings/ Log.info(rc.delete(gl_test_str)) Log.info(rc.set(gl_test_str, 'https://blog.csdn.net/zyooooxie', ex=1000)) Log.info(rc.get(gl_test_str)) key1 = 'external:customer:xxx_1' key2 = 'external:customer:xxx_2' key3 = 'external:customer:xxx_3' key4 = 'external:TEST' Log.info(rc.mset( {key1: 'value 1', key2: 'value 2', key3: '3个确定都是相同slot', key4: 'redis-py-cluster的mget、mset 支持 不同slot的key'})) Log.info(rc.mget(key1, key3, key2)) Log.info(rc.mget(key1, key4)) Log.info('------') Log.info('redis-py-cluster的unlink 必须是 the same slot的key') Log.info(rc.unlink(key1, key3)) # Log.info(rc.unlink(key1, key4, gl_no_exist)) # Keys in request don't hash to the same slot Log.info(rc.exists(gl_test_str)) Log.info(rc.type(gl_test_str)) Log.info(rc.ttl(gl_test_str)) Log.info(rc.expire(gl_test_str, 2 * 60 * 60)) def cluster_hash(rc: RedisCluster): """ :param rc: :return: """ # https://redis.io/docs/data-types/hashes/ Log.info(rc.delete(gl_test_hash)) Log.info(rc.hset(gl_test_hash, mapping={'hash_key0': 'hash_value0', 'hash_key1': 'hash_value1', 'hash_key2': 'hash_value2', 'hash_key3': 'hash_value3', 'hk4': 'hv4', 'hk5': 'hv5', 'hk6': 'hv6' })) Log.info(rc.hget(gl_test_hash, 'hash_key0')) Log.info(rc.hlen(gl_test_hash)) Log.info(rc.hexists(gl_test_hash, 'hash_key2222')) Log.info(rc.hkeys(gl_test_hash)) Log.info(rc.hvals(gl_test_hash)) Log.info(rc.hdel(gl_test_hash, 'hash_key2222', 'hash_key0', 'hk6')) Log.info(rc.hmget(gl_test_hash, 'hash_key2222', 'hash_key2')) Log.info(rc.hmget(gl_test_hash, ['hash_key2222', 'hash_key2'])) Log.info(rc.hmset(gl_test_hash, {'test': 'test_value', 'test2': 'test_value2'})) Log.info(rc.hgetall(gl_test_hash)) Log.info('------') Log.info(rc.hset(gl_no_exist, mapping={'test': 'test_value', 'test2': 'test_value2'})) Log.info(rc.unlink(gl_no_exist)) Log.info(rc.exists(gl_test_hash)) Log.info(rc.type(gl_test_hash)) Log.info(rc.ttl(gl_test_hash)) Log.info(rc.expire(gl_test_hash, 2 * 60 * 60)) def cluster_list(rc: RedisCluster): """ :param rc: :return: """ # https://redis.io/docs/data-types/lists/ Log.info(rc.delete(gl_test_list)) Log.info(rc.rpush(gl_test_list, 'list1', 'list2', 'list3')) Log.info(rc.lindex(gl_test_list, 1)) Log.info(rc.llen(gl_test_list)) Log.info(rc.lpush(gl_test_list, 'list0', 'list0')) Log.info(rc.linsert(gl_test_list, 'BEFORE', 'list0', 'BEFORE__')) Log.info(rc.linsert(gl_test_list, 'AFTER', 'list0', 'AFTER__')) # 放在第一个list0 之后 Log.info(rc.lrange(gl_test_list, 0, -1)) Log.info(rc.lpop(gl_test_list)) Log.info(rc.rpop(gl_test_list)) Log.info(rc.lrem(gl_test_list, 1, 'list0')) Log.info(rc.lset(gl_test_list, 0, '新的_0')) Log.info('------') Log.info(rc.lpush(gl_no_exist, 0, 'list_0', 1, 'list_1')) Log.info(rc.unlink(gl_no_exist)) Log.info(rc.type(gl_test_list)) Log.info(rc.exists(gl_test_list)) Log.info(rc.ttl(gl_test_list)) Log.info(rc.expire(gl_test_list, 2 * 60 * 60)) def cluster_set(rc: RedisCluster): """ :param rc: :return: """ # https://redis.io/docs/data-types/sets/ Log.info(rc.delete(gl_test_set)) Log.info(rc.sadd(gl_test_set, 'set1', 'set2', 'set3', 'set3', 'set3', 'set3', 'set4')) Log.info(rc.sismember(gl_test_set, 'set1111')) Log.info(rc.srem(gl_test_set, 'set1')) Log.info(rc.scard(gl_test_set)) Log.info(rc.smembers(gl_test_set)) Log.info('------') Log.info(rc.sadd(gl_no_exist, 'set3', 'set3', 'set3', 'set3')) Log.info(rc.unlink(gl_no_exist)) Log.info(rc.type(gl_test_set)) Log.info(rc.exists(gl_test_set)) Log.info(rc.ttl(gl_test_set)) Log.info(rc.expire(gl_test_set, 2 * 60 * 60)) if __name__ == '__main__': Log.error('------') # rc_m = redis_py_cluster_connect_1() rc_m = redis_py_cluster_connect_2() # rc_m = redis_py_cluster_connect_3() # cluster_commands(rc=rc_m) try: # cluster_str(rc_m) # cluster_hash(rc_m) # cluster_list(rc_m) # cluster_set(rc_m) Log.error(gl_key_name) scan_commands(rc_m) keys_commands(rc_m) # scan_iter_method(rc_m) except Exception as e: Log.error(e.args) Log.info(traceback.format_exc()) rc_m.close() Log.error('------')
本文链接:https://blog.csdn.net/zyooooxie/article/details/112484045
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。