def test_from_url_connection_classes(self): from rediscluster.client import RedisCluster from rediscluster.connection import ClusterConnectionPool, ClusterConnection, SSLClusterConnection r = RedisCluster.from_url('redis://*****:*****@/tmp/random.sock')
def test_moved_redirection_on_slave_with_default(self): """ On Pipeline, we redirected once and finally get from master with readonly client when data is completely moved. """ self.assert_moved_redirection_on_slave( ClusterConnectionPool, RedisCluster(host="127.0.0.1", port=7000, reinitialize_steps=1))
def test_moved_redirection_on_slave_with_readonly_mode_client(self): """ Ditto with READONLY mode. """ self.assert_moved_redirection_on_slave( ClusterReadOnlyConnectionPool, RedisCluster(host="127.0.0.1", port=7000, readonly_mode=True, reinitialize_steps=1))
def test_access_correct_slave_with_readonly_mode_client(self, 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:7001 sr.set('foo87', 'foo') sr.set('foo88', 'bar') 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:7004', 'host': '127.0.0.1', 'port': 7004, 'server_type': 'slave', } master_value = { 'host': '127.0.0.1', 'name': '127.0.0.1:7001', 'port': 7001, 'server_type': 'master' } with patch.object(ClusterConnectionPool, 'get_master_node_by_slot', return_value=master_value) as return_master_mock: readonly_client = RedisCluster(host="127.0.0.1", port=7000, readonly_mode=True) with readonly_client.pipeline() as readonly_pipe: assert readonly_pipe.get('foo88').get( 'foo87').execute() == [b'bar', b'foo']
def Connect(self): from rediscluster.client import RedisCluster try: WriteBehindLog( "Connect: connecting to {}:{} password: {} cluster nodes: {}". format(self.host, self.port, self.password, self.cluster_nodes)) rc = RedisCluster(host=self.host, port=self.port, startup_nodes=self.cluster_nodes, password=self.password, decode_responses=True) except Exception as e: msg = "Cannot connect to Redis Cluster. Exception: {}".format(e) WriteBehindLog(msg) raise Exception(msg) from None return rc
def test_pubsub_thread_publish(): """ This test will never fail but it will still show and be viable to use and to test the threading capability of the connectionpool and the publish mechanism. """ startup_nodes = [{"host": "127.0.0.1", "port": "7000"}] r = RedisCluster( startup_nodes=startup_nodes, decode_responses=True, max_connections=16, max_connections_per_node=16, ) def t_run(rc): for i in range(0, 50): rc.publish('foo', 'bar') rc.publish('bar', 'foo') rc.publish('asd', 'dsa') rc.publish('dsa', 'asd') rc.publish('qwe', 'bar') rc.publish('ewq', 'foo') rc.publish('wer', 'dsa') rc.publish('rew', 'asd') # Use this for debugging # print(rc.connection_pool._available_connections) # print(rc.connection_pool._in_use_connections) # print(rc.connection_pool._created_connections) try: threads = [] for i in range(10): t = threading.Thread(target=t_run, args=(r, )) threads.append(t) t.start() except Exception: print("Error: unable to start thread")
def client(self): return RedisCluster(**self.conn_params)
from functools import wraps from typing import Callable from rediscluster.client import RedisCluster from app.models import Forecast nodes = [ {"host": "127.0.0.1", "port": "7001"}, {"host": "127.0.0.1", "port": "7002"}, {"host": "127.0.0.1", "port": "7003"}, {"host": "127.0.0.1", "port": "7004"}, {"host": "127.0.0.1", "port": "7005"}, {"host": "127.0.0.1", "port": "7006"}, ] redis = RedisCluster(startup_nodes=nodes, decode_responses=True) def save(f: Callable[..., Forecast]): @wraps(f) def wrapper(*args, **kwargs): timestamp = kwargs.get('timestamp', None) res = f(*args, **kwargs) if timestamp is None: return res redis[timestamp] = json.dumps(res.as_json()) print('Wrote to Redis') return res return wrapper