本文整理汇总了Python中rediscluster.StrictRedisCluster类的典型用法代码示例。如果您正苦于以下问题:Python StrictRedisCluster类的具体用法?Python StrictRedisCluster怎么用?Python StrictRedisCluster使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StrictRedisCluster类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: monkey_link
def monkey_link(host=None, port=None, decode_responses=False):
"""
Helper function to return custom slots cache data from different redis nodes
"""
if port == 7000:
result = [[0, 5460, [b'127.0.0.1', 7000], [b'127.0.0.1', 7003]],
[5461, 10922, [b'127.0.0.1', 7001], [b'127.0.0.1', 7004]]]
elif port == 7001:
result = [[0, 5460, [b'127.0.0.1', 7001], [b'127.0.0.1', 7003]],
[5461, 10922, [b'127.0.0.1', 7000], [b'127.0.0.1', 7004]]]
else:
result = []
r = StrictRedisCluster(host=host, port=port, decode_responses=True)
orig_execute_command = r.execute_command
def execute_command(*args, **kwargs):
if args == ("cluster", "slots"):
return result
return orig_execute_command(*args, **kwargs)
r.execute_command = execute_command
return r
示例2: getRedisKeyValues
def getRedisKeyValues(host, port, keyFilter):
try:
startup_nodes = [{"host": host, "port": port}]
rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
info = rc.info()
for key in info:
print "%s: %s" % (key, info[key])
result = []
# keysCount = len(rc.keys())
count = 0
for key in rc.keys():
if count >= 1024:
break
else:
if key.find(keyFilter) != -1:
count += 1
# 查询类型
# (key, value)
result.append([key, getValue(rc, key)])
# pprint.pprint("type:" + rc.type(key) + "$key:" + key + "$value:" + getValue(rc, key))
# print rc.get(key)
return result
except Exception, e:
return [["<font color='red'>Error Info</font>", "<font color='red'>%s</font>" % (str(e) + "<br/>请检查ip和端口是否正确")]]
示例3: test_access_correct_slave_with_readonly_mode_client
def test_access_correct_slave_with_readonly_mode_client(sr):
"""
Test that the client can get value normally with readonly mode
when we connect to correct slave.
"""
# we assume this key is set on 127.0.0.1:7000(7003)
sr.set("foo16706", "foo")
import time
time.sleep(1)
with patch.object(ClusterReadOnlyConnectionPool, "get_node_by_slot") as return_slave_mock:
return_slave_mock.return_value = {
"name": "127.0.0.1:7003",
"host": "127.0.0.1",
"port": 7003,
"server_type": "slave",
}
master_value = {"host": "127.0.0.1", "name": "127.0.0.1:7000", "port": 7000, "server_type": "master"}
with patch.object(
ClusterConnectionPool, "get_master_node_by_slot", return_value=master_value
) as return_master_mock:
readonly_client = StrictRedisCluster(host="127.0.0.1", port=7000, readonly_mode=True)
assert b("foo") == readonly_client.get("foo16706")
assert return_master_mock.call_count == 0
示例4: test_ask_redirection
def test_ask_redirection():
"""
Test that the server handles ASK response.
At first call it should return a ASK ResponseError that will point
the client to the next server it should talk to.
Important thing to verify is that it tries to talk to the second node.
"""
r = StrictRedisCluster(host="127.0.0.1", port=7000)
m = Mock(autospec=True)
def ask_redirect_effect(connection, command_name, **options):
def ok_response(connection, command_name, **options):
assert connection.host == "127.0.0.1"
assert connection.port == 7001
return "MOCK_OK"
m.side_effect = ok_response
raise AskError("1337 127.0.0.1:7001")
m.side_effect = ask_redirect_effect
r.parse_response = m
assert r.execute_command("SET", "foo", "bar") == "MOCK_OK"
示例5: test_moved_redirection
def test_moved_redirection():
"""
Test that the client handles MOVED response.
At first call it should return a MOVED ResponseError that will point
the client to the next server it should talk to.
Important thing to verify is that it tries to talk to the second node.
"""
r = StrictRedisCluster(host="127.0.0.1", port=7000)
m = Mock(autospec=True)
def ask_redirect_effect(connection, *args, **options):
def ok_response(connection, *args, **options):
assert connection.host == "127.0.0.1"
assert connection.port == 7002
return "MOCK_OK"
m.side_effect = ok_response
raise MovedError("12182 127.0.0.1:7002")
m.side_effect = ask_redirect_effect
r.parse_response = m
assert r.set("foo", "bar") == "MOCK_OK"
示例6: test_moved_redirection_pipeline
def test_moved_redirection_pipeline():
"""
Test that the server handles MOVED response when used in pipeline.
At first call it should return a MOVED ResponseError that will point
the client to the next server it should talk to.
Important thing to verify is that it tries to talk to the second node.
"""
r = StrictRedisCluster(host="127.0.0.1", port=7000)
p = r.pipeline()
m = Mock(autospec=True)
def moved_redirect_effect(connection, command_name, **options):
def ok_response(connection, command_name, **options):
assert connection.host == "127.0.0.1"
assert connection.port == 7002
return "MOCK_OK"
m.side_effect = ok_response
raise MovedError("12182 127.0.0.1:7002")
m.side_effect = moved_redirect_effect
p.parse_response = m
p.set("foo", "bar")
assert p.execute() == ["MOCK_OK"]
示例7: test_ask_redirection
def test_ask_redirection():
"""
Test that the server handles ASK response.
At first call it should return a ASK ResponseError that will point
the client to the next server it should talk to.
Important thing to verify is that it tries to talk to the second node.
"""
r = StrictRedisCluster(host="127.0.0.1", port=7000)
r.connection_pool.nodes.nodes["127.0.0.1:7001"] = {
"host": u"127.0.0.1",
"server_type": "master",
"port": 7001,
"name": "127.0.0.1:7001",
}
with patch.object(StrictRedisCluster, "parse_response") as parse_response:
host_ip = find_node_ip_based_on_port(r, "7001")
def ask_redirect_effect(connection, *args, **options):
def ok_response(connection, *args, **options):
assert connection.host == host_ip
assert connection.port == 7001
return "MOCK_OK"
parse_response.side_effect = ok_response
raise AskError("1337 {0}:7001".format(host_ip))
parse_response.side_effect = ask_redirect_effect
assert r.execute_command("SET", "foo", "bar") == "MOCK_OK"
示例8: test_pipeline_ask_redirection
def test_pipeline_ask_redirection():
"""
Test that the server handles ASK response when used in pipeline.
At first call it should return a ASK ResponseError that will point
the client to the next server it should talk to.
Important thing to verify is that it tries to talk to the second node.
"""
r = StrictRedisCluster(host="127.0.0.1", port=7000)
with patch.object(StrictRedisCluster, "parse_response") as parse_response:
def response(connection, *args, **options):
def response(connection, *args, **options):
def response(connection, *args, **options):
assert connection.host == "127.0.0.1"
assert connection.port == 7001
return "MOCK_OK"
parse_response.side_effect = response
raise AskError("12182 127.0.0.1:7001")
parse_response.side_effect = response
raise AskError("12182 127.0.0.1:7001")
parse_response.side_effect = response
p = r.pipeline()
p.set("foo", "bar")
assert p.execute() == ["MOCK_OK"]
示例9: test_ask_redirection
def test_ask_redirection():
"""
Test that the server handles ASK response.
At first call it should return a ASK ResponseError that will point
the client to the next server it should talk to.
Important thing to verify is that it tries to talk to the second node.
"""
r = StrictRedisCluster(host="127.0.0.1", port=7000)
r.connection_pool.nodes.nodes['127.0.0.1:7001'] = {
'host': u'127.0.0.1',
'server_type': 'master',
'port': 7001,
'name': '127.0.0.1:7001'
}
m = Mock(autospec=True)
host_ip = find_node_ip_based_on_port(r, '7001')
def ask_redirect_effect(connection, *args, **options):
def ok_response(connection, *args, **options):
assert connection.host == host_ip
assert connection.port == 7001
return "MOCK_OK"
m.side_effect = ok_response
raise AskError("1337 {0}:7001".format(host_ip))
m.side_effect = ask_redirect_effect
r.parse_response = m
assert r.execute_command("SET", "foo", "bar") == "MOCK_OK"
示例10: test_access_correct_slave_with_readonly_mode_client
def test_access_correct_slave_with_readonly_mode_client(sr):
"""
Test that the client can get value normally with readonly mode
when we connect to correct slave.
"""
# we assume this key is set on 127.0.0.1:7000(7003)
sr.set('foo16706', 'foo')
import time
time.sleep(1)
with patch.object(ClusterReadOnlyConnectionPool, 'get_node_by_slot') as return_slave_mock:
return_slave_mock.return_value = {
'name': '127.0.0.1:7003',
'host': '127.0.0.1',
'port': 7003,
'server_type': 'slave',
}
master_value = {'host': '127.0.0.1', 'name': '127.0.0.1:7000', 'port': 7000, 'server_type': 'master'}
with patch.object(
ClusterConnectionPool,
'get_master_node_by_slot',
return_value=master_value) as return_master_mock:
readonly_client = StrictRedisCluster(host="127.0.0.1", port=7000, readonly_mode=True)
assert b('foo') == readonly_client.get('foo16706')
assert return_master_mock.call_count == 0
示例11: test_refresh_table_asap
def test_refresh_table_asap():
"""
If this variable is set externally, initialize() should be called.
"""
with patch.object(NodeManager, "initialize") as mock_initialize:
mock_initialize.return_value = None
# Patch parse_response to avoid issues when the cluster sometimes return MOVED
with patch.object(StrictRedisCluster, "parse_response") as mock_parse_response:
def side_effect(self, *args, **kwargs):
return None
mock_parse_response.side_effect = side_effect
r = StrictRedisCluster(host="127.0.0.1", port=7000)
r.connection_pool.nodes.slots[12182] = [
{"host": "127.0.0.1", "port": 7002, "name": "127.0.0.1:7002", "server_type": "master"}
]
r.refresh_table_asap = True
i = len(mock_initialize.mock_calls)
r.execute_command("SET", "foo", "bar")
assert len(mock_initialize.mock_calls) - i == 1
assert r.refresh_table_asap is False
示例12: test_moved_redirection_pipeline
def test_moved_redirection_pipeline():
"""
Test that the server handles MOVED response when used in pipeline.
At first call it should return a MOVED ResponseError that will point
the client to the next server it should talk to.
Important thing to verify is that it tries to talk to the second node.
"""
with patch.object(StrictRedisCluster, 'parse_response') as parse_response:
def moved_redirect_effect(connection, *args, **options):
def ok_response(connection, *args, **options):
assert connection.host == "127.0.0.1"
assert connection.port == 7002
return "MOCK_OK"
parse_response.side_effect = ok_response
raise MovedError("12182 127.0.0.1:7002")
parse_response.side_effect = moved_redirect_effect
r = StrictRedisCluster(host="127.0.0.1", port=7000)
p = r.pipeline()
p.set("foo", "bar")
assert p.execute() == ["MOCK_OK"]
示例13: test_refresh_using_specific_nodes
def test_refresh_using_specific_nodes(r):
"""
Test making calls on specific nodes when the cluster has failed over to
another node
"""
with patch.object(StrictRedisCluster, 'parse_response') as parse_response_mock:
with patch.object(NodeManager, 'initialize', autospec=True) as init_mock:
# simulate 7006 as a failed node
def side_effect(self, *args, **kwargs):
if self.port == 7006:
parse_response_mock.failed_calls += 1
raise ClusterDownError('CLUSTERDOWN The cluster is down. Use CLUSTER INFO for more information')
elif self.port == 7007:
parse_response_mock.successful_calls += 1
def side_effect_rebuild_slots_cache(self):
# start with all slots mapped to 7006
self.nodes = {'127.0.0.1:7006': {'host': '127.0.0.1', 'server_type': 'master', 'port': 7006, 'name': '127.0.0.1:7006'}}
self.slots = {}
for i in range(0, 16383):
self.slots[i] = [{
'host': '127.0.0.1',
'server_type': 'master',
'port': 7006,
'name': '127.0.0.1:7006',
}]
# After the first connection fails, a reinitialize should follow the cluster to 7007
def map_7007(self):
self.nodes = {'127.0.0.1:7007': {'host': '127.0.0.1', 'server_type': 'master', 'port': 7007, 'name': '127.0.0.1:7007'}}
self.slots = {}
for i in range(0, 16383):
self.slots[i] = [{
'host': '127.0.0.1',
'server_type': 'master',
'port': 7007,
'name': '127.0.0.1:7007',
}]
init_mock.side_effect = map_7007
parse_response_mock.side_effect = side_effect
parse_response_mock.successful_calls = 0
parse_response_mock.failed_calls = 0
init_mock.side_effect = side_effect_rebuild_slots_cache
rc = StrictRedisCluster(host='127.0.0.1', port=7006)
assert len(rc.connection_pool.nodes.nodes) == 1
assert '127.0.0.1:7006' in rc.connection_pool.nodes.nodes
rc.ping()
# Cluster should now point to 7006, and there should be one failed and one succesful call
assert len(rc.connection_pool.nodes.nodes) == 1
assert '127.0.0.1:7007' in rc.connection_pool.nodes.nodes
assert parse_response_mock.failed_calls == 1
assert parse_response_mock.successful_calls == 1
示例14: test_cluster_of_one_instance
def test_cluster_of_one_instance():
"""
Test a cluster that starts with only one redis server and ends up with
one server.
There is another redis server joining the cluster, hold slot 0, and
eventually quit the cluster. The StrictRedisCluster instance may get confused
when slots mapping and nodes change during the test.
"""
with patch.object(StrictRedisCluster, 'parse_response') as parse_response_mock:
with patch.object(NodeManager, 'initialize', autospec=True) as init_mock:
def side_effect(self, *args, **kwargs):
def ok_call(self, *args, **kwargs):
assert self.port == 7007
return "OK"
parse_response_mock.side_effect = ok_call
raise ClusterDownError('CLUSTERDOWN The cluster is down. Use CLUSTER INFO for more information')
def side_effect_rebuild_slots_cache(self):
# make new node cache that points to 7007 instead of 7006
self.nodes = [{'host': '127.0.0.1', 'server_type': 'master', 'port': 7006, 'name': '127.0.0.1:7006'}]
self.slots = {}
for i in range(0, 16383):
self.slots[i] = [{
'host': '127.0.0.1',
'server_type': 'master',
'port': 7006,
'name': '127.0.0.1:7006',
}]
# Second call should map all to 7007
def map_7007(self):
self.nodes = [{'host': '127.0.0.1', 'server_type': 'master', 'port': 7007, 'name': '127.0.0.1:7007'}]
self.slots = {}
for i in range(0, 16383):
self.slots[i] = [{
'host': '127.0.0.1',
'server_type': 'master',
'port': 7007,
'name': '127.0.0.1:7007',
}]
# First call should map all to 7006
init_mock.side_effect = map_7007
parse_response_mock.side_effect = side_effect
init_mock.side_effect = side_effect_rebuild_slots_cache
rc = StrictRedisCluster(host='127.0.0.1', port=7006)
rc.set("foo", "bar")
示例15: redis_write
def redis_write(nodes, dict_data):
"""
将字典写入redis集群,并计算时间
"""
time1 = time.time()
conn_rc = StrictRedisCluster(startup_nodes=nodes, decode_responses=True)
for k in dict_data:
# print k, func_dict[k]
key_name = 'access_log' + '-' + str(k)
conn_rc.set(key_name, dict_data[k])
# print conn_rc.get(key_name)
time2 = time.time()
time3 = time2 - time1
print time3