本文整理匯總了Python中redis.client.Redis類的典型用法代碼示例。如果您正苦於以下問題:Python Redis類的具體用法?Python Redis怎麽用?Python Redis使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Redis類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: RedisPublisher
class RedisPublisher(RedisNode):
def start(self):
conf = parse_uri(self._uri)
self._channel = conf.pop('channel', self._name)
self._method = conf.pop('method', 'queue')
self._client = Redis(**conf)
def stop(self):
self._client.connection_pool.disconnect()
def send(self, msg):
"""
Send a message
:param msg: serializable string
"""
self._lock.acquire()
if self._method == 'pubsub':
ret = self._client.publish(self._channel, msg)
if ret < 1:
self._log.error('No subscriber receive this message ')
else:
ret = self._client.rpush(self._channel, msg)
self._lock.release()
return ret
示例2: GET
def GET(self):
r = Redis(connection_pool=redis_pool)
count = 0
for key in r.hgetall(URL_HASH_NAME):
count += 1
return render_template('index.html', count=count, section_class='index', user_id=None)
示例3: __init__
def __init__(self, addr, db=0):
if not addr:
raise ValueError('Invalid redis address')
if addr.startswith('unix://'):
cargs = {'unix_socket_path':addr.replace('unix://', '')}
elif addr.startswith('tcp://'):
h = addr.replace('tcp://', '').split(':')
cargs = {'host': h[0]}
if len(h) == 2:
cargs['port'] = int(h[1])
else:
raise ValueError('Invalid redis address')
Redis.__init__(self, **cargs)
示例4: from_url
def from_url(url, db=None, **kwargs):
"""Returns an active Redis client generated from the given database URL.
Will attempt to extract the database id from the path url fragment, if
none is provided.
"""
return Redis.from_url(url, db, **kwargs)
示例5: RedisSubscriber
class RedisSubscriber(RedisNode):
def __init__(self, config, handler):
RedisNode.__init__(self, config)
self._handler = handler
def connect(self):
conf = parse_uri(self._uri)
self._channel = conf.pop('channel', self._name)
self._method = conf.pop('method', 'pubsub')
self._client = Redis(**conf)
if self._method == 'pubsub':
self._pubsub = self._client.pubsub()
self._pubsub.subscribe(self._channel)
def start(self):
self.connect()
self.loop()
def loop(self):
while True:
self._log.debug('in subscriber loop')
raw_request = self.recv()
self._handler(raw_request)
def recv(self):
"""
Return a message as a string from the receiving queue.
"""
self._log.debug('waiting in recv()')
self._lock.acquire()
if self._method == 'pubsub':
msg = self._pubsub.listen().next()['data']
else:
msg = self._client.blpop(self._channel)[1]
self._log.debug('redisclient: %s' % self._name)
self._log.debug('recv -> %s' % msg)
self._lock.release()
return msg
def close(self):
if self._method == 'pubsub':
self._pubsub.unsubscribe(self._channel)
self._client.connection_pool.disconnect()
示例6: uget
def uget(self, name):
"""
Return the value and key ``name`` or None, and decode it if not None.
"""
value = Redis.get(self, name)
if value:
return value.decode(self.encoding)
return value
示例7: __init__
def __init__(self, client=None, *args, **kwargs):
self._client = client
if not self._client:
kwargs.setdefault('decode_responses', True)
self._client = Redis(*args, **kwargs)
self._bind_atoms()
self._bind_multi()
示例8: __init__
def __init__(self, config):
self._redis = Redis(host=config.get('redis','host'),
port=int(config.get('redis','port')),
db=int(config.get('redis','db')))
self._delta_secs = int(eval(config.get('timeseries',
'delta_secs')))
self._expiration_delay_secs = int(eval(config.get('timeseries',
'expiration_delay_secs')))
示例9: _transfer_slots
def _transfer_slots(redis_conn_from: Redis, redis_id_from: str, redis_conn_to: Redis, redis_id_to: str, slots: list):
"""
Documentation from http://redis.io/commands/cluster-setslot
1. Set the destination node slot to importing state using CLUSTER SETSLOT <slot> IMPORTING <source-node-id>.
2. Set the source node slot to migrating state using CLUSTER SETSLOT <slot> MIGRATING <destination-node-id>.
3. Get keys from the source node with CLUSTER GETKEYSINSLOT command and move them into the destination node
using the MIGRATE command.
4. Use CLUSTER SETSLOT <slot> NODE <destination-node-id> in the source or destination.
"""
print('Transfering %d slots from %s to %s...' % (len(slots), redis_id_from, redis_id_to))
dest_host = redis_conn_to.connection_pool.connection_kwargs['host']
dest_port = redis_conn_to.connection_pool.connection_kwargs['port']
pipeline_to = redis_conn_to.pipeline()
pipeline_from = redis_conn_from.pipeline()
for slot in slots:
# 1, 2
pipeline_to.execute_command('CLUSTER SETSLOT', slot, 'IMPORTING', redis_id_from)
pipeline_from.execute_command('CLUSTER SETSLOT', slot, 'MIGRATING', redis_id_to)
pipeline_to.execute()
pipeline_from.execute()
for slot in slots:
# 3
keys = redis_conn_from.execute_command('CLUSTER GETKEYSINSLOT', slot, 1000000)
if len(keys) > 0:
redis_conn_from.execute_command('MIGRATE', dest_host, dest_port, "", 0, 180000, 'KEYS', *keys)
# 4
redis_conn_to.execute_command('CLUSTER SETSLOT', slot, 'NODE', redis_id_to)
示例10: POST
def POST(self):
r = Redis(connection_pool=redis_pool)
user_id = check_token(r)
form = url_form()
if not form.validates():
return render_template('add.html',
form=form,
user_id=user_id,
is_add=True,
)
url = form['url'].value
token = hashlib.sha1()
token.update(url.replace('http(s)?://', '').strip())
key = token.hexdigest()[:6]
print key + url
if not r.hget(URL_HASH_NAME, key):
r.hset(URL_HASH_NAME, key, url)
r.hset(COUNT_HASH_NAME, key, 0)
if user_id[1]:
r.hset(LOG_HASH_NAME, key, r.hget(TOKEN_HASH_NAME, web.input().token))
if user_id[1] == '':
raise web.seeother('/%s/+' % key)
else:
raise web.seeother('/%s/+?token=%s' % (key, user_id[1]))
示例11: connect
def connect(self):
conf = parse_uri(self._uri)
self._channel = conf.pop('channel', self._name)
self._method = conf.pop('method', 'pubsub')
self._client = Redis(**conf)
if self._method == 'pubsub':
self._pubsub = self._client.pubsub()
self._pubsub.subscribe(self._channel)
示例12: hgetall
def hgetall(self, name):
"""
Return a Python dict of the hash's name/value pairs, both key and
value decoded.
"""
output = {}
info = Redis.hgetall(self, name)
for key, value in info.iteritems():
output[key.decode(self.encoding)] = value.decode(self.encoding)
return output
示例13: _add_new_nodes
def _add_new_nodes(self, cluster_size):
old_nodes = self.nodes.copy()
nodes_before = self._get_nodes_primitive()
self._docker_scale(cluster_size)
nodes_after = self._get_nodes_primitive()
new_ips = [':'.join(map(str, x)) for x in set(nodes_after) - set(nodes_before)]
print(new_ips)
master_ip_port = old_nodes[0]['ip_port']
master_ip, master_port = master_ip_port.split(':')
master_conn = Redis(master_ip, master_port)
print("Adding nodes to the cluster")
for ip in new_ips:
new_ip, new_port = ip.split(':')
master_conn.execute_command('CLUSTER MEET', new_ip, new_port)
print("Preventive fix")
sleep(3)
fix = subprocess.Popen(['ruby', 'redis-trib.rb', 'fix', master_ip_port], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL)
fix.communicate(b'yes\n')
fix.wait()
sleep(3)
new_nodes = [x for x in self._get_running_nodes() if x['ip_port'] in new_ips]
slots_per_node = round(16384 / cluster_size)
old_redises = {x[0]: Redis(x[0], x[1]) for x in (y['ip_port'].split(':') for y in old_nodes)}
new_redises = [Redis(x[0], x[1]) for x in (y['ip_port'].split(':') for y in new_nodes)]
slots_repartition = self._get_slots_repartition(list(old_redises.values())[0])
for dest_node, dest_redis, i in zip(new_nodes, new_redises, range(len(new_nodes))):
slots = slots_repartition[i * slots_per_node: (i + 1) * slots_per_node]
sources_ip = {x[1] for x in slots}
for source_ip in sources_ip:
slots_for_source = [x for x in slots if x[1] == source_ip]
source_redis = old_redises[source_ip]
self._transfer_slots(source_redis, slots_for_source[0][3],
dest_redis, dest_node['id'],
[x[0] for x in slots_for_source])
subprocess.check_call(['ruby', 'redis-trib.rb', 'info', master_ip_port])
示例14: main
def main(argv=sys.argv):
if len(argv) < 2:
usage(argv)
config_uri = argv[1]
options = parse_vars(argv[2:])
setup_logging(config_uri)
settings = get_appsettings(config_uri, options=options)
logging.getLogger('c2corg_ui').setLevel(logging.INFO)
log.info('Cache Redis: {0}'.format(settings['redis.url']))
# we don't really need a connection pool here, but the `from_url`
# function is convenient
redis_pool = ConnectionPool.from_url(
settings['redis.url'], max_connections=1)
# remove all keys from the database
r = Redis(connection_pool=redis_pool)
r.flushdb()
log.info('Flushed cache')
示例15: main
def main(argv=sys.argv):
if len(argv) < 2:
usage(argv)
config_uri = argv[1]
options = parse_vars(argv[2:])
setup_logging(config_uri)
settings = get_appsettings(config_uri, options=options)
logging.getLogger("c2corg_api").setLevel(logging.INFO)
redis_url = "{0}?db={1}".format(settings["redis.url"], settings["redis.db_cache"])
log.info("Cache Redis: {0}".format(redis_url))
# we don't really need a connection pool here, but the `from_url`
# function is convenient
redis_pool = ConnectionPool.from_url(redis_url, max_connections=1)
# remove all keys from the database
r = Redis(connection_pool=redis_pool)
r.flushdb()
log.info("Flushed cache")