本文整理汇总了Python中redis.StrictRedis.scan_iter方法的典型用法代码示例。如果您正苦于以下问题:Python StrictRedis.scan_iter方法的具体用法?Python StrictRedis.scan_iter怎么用?Python StrictRedis.scan_iter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.StrictRedis
的用法示例。
在下文中一共展示了StrictRedis.scan_iter方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IndexCache
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import scan_iter [as 别名]
class IndexCache(object):
def __init__(self, host, port):
self.client = StrictRedis(host=host, port=port, db=0)
def build(self, doc_word_scores):
"""Clears the entire store and adds all the doc_word_scores into a
hash table in the store.
:param doc_word_scores: dictionary of dictionaries that looks like:
doc_word_scores[word][doc_id] = score of word in that
document
"""
self.reset()
for word, doc_id_score in doc_word_scores.items():
# Add table name to word
word_key = DOCUMENT_WORD_SCORE_NAME + word
self.client.hmset(word_key, doc_id_score)
self.save_to_disk()
def reset(self):
"""Deletes all keys in this DB.
"""
self.client.flushdb()
def save_to_disk(self):
"""Asyncronous write to disk for persistent storage.
"""
self.client.bgsave()
def to_dict(self):
"""Returns the "doc_word_scores" table in Redis in dictionary form.
"""
doc_word_scores = {}
for word_key in self.doc_word_scores_iter():
# Remove the table name from the key
word = word_key.replace(DOCUMENT_WORD_SCORE_NAME, "")
# Grab the {doc_ids : scores} dictionary for word
doc_word_scores[word] = self.client.hgetall(word_key)
return doc_word_scores
def doc_word_scores_iter(self):
"""Returns an iterator for the keys of all the words stored in Redis
"""
return self.client.scan_iter(match=DOCUMENT_WORD_SCORE_NAME + "*")
def is_empty(self):
return self.client.dbsize() <= 0
def doc_scores(self, word):
"""Returns a hash table of document_ids mapping to scores
"""
word_key = DOCUMENT_WORD_SCORE_NAME + word
return self.client.hgetall(word_key)
示例2: main
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import scan_iter [as 别名]
def main(stdscr):
# no delay on user input
stdscr.nodelay(True)
# move cursos to 0, 0
stdscr.leaveok(0)
# connect to the redis server
r = StrictRedis(host='127.0.0.1', port=6379, db=0)
# main loop
while True:
# clear screen
stdscr.erase()
# print info message
stdscr.addstr(0, 0, "press 'q' to exit.")
# get list of aphids keys
akeys = sorted(r.scan_iter('aphids*'))
# get and show all aphids keys
for i, k in enumerate(akeys):
# get the type of this key
k_type = r.type(k)
# read differently depending on type
if k_type == "string":
v = r.get(k)
elif k_type == "list":
v = r.lrange(k, 0, -1)
opts = A_STANDOUT if k.endswith('seg_rate') else 0
stdscr.addstr(i+2, 8, k)
stdscr.addstr(i+2, 68, "{0}".format(v), opts)
# refresh screen
stdscr.refresh()
# handle user input
try:
c = stdscr.getkey()
if c == 'q':
break
except:
continue
示例3: get_sync_status
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import scan_iter [as 别名]
def get_sync_status(hostname=None, port=6379, database=1,
alive_threshold=timedelta(seconds=180),
alive_threshold_eas=timedelta(seconds=420),
account_id=None):
if hostname:
client = StrictRedis(host=hostname, port=port, db=database)
else:
try:
client = get_redis_client()
alive_threshold_eas, alive_threshold_eas = get_heartbeat_config()
except Exception as e:
raise e
client_batch = client.pipeline()
keys = []
match_key = None
if account_id:
match_key = SyncStatusKey.all_folders(account_id)
for k in client.scan_iter(match=match_key, count=100):
# this shouldn't happen since we won't use db=0 anymore
if k == 'ElastiCacheMasterReplicationTimestamp':
continue
client_batch.hgetall(k)
keys.append(k)
values = client_batch.execute()
now = datetime.utcnow()
alive_threshold = timedelta(seconds=180)
alive_threshold_eas = timedelta(seconds=420)
accounts = {}
for (k, v) in zip(keys, values):
key = SyncStatusKey.from_string(k)
account_alive, provider_name, folders = accounts.get(key.account_id,
(True, '', {}))
folder_alive, folder_name, devices = folders.get(key.folder_id,
(True, '', {}))
for device_id in v:
value = json.loads(v[device_id])
provider_name = value['provider_name']
folder_name = value['folder_name']
heartbeat_at = datetime.strptime(value['heartbeat_at'],
'%Y-%m-%d %H:%M:%S.%f')
state = value.get('state', None)
action = value.get('action', None)
if provider_name != 'eas' or \
(provider_name == 'eas' and action != 'ping'):
device_alive = (now - heartbeat_at) < alive_threshold
else:
device_alive = (now - heartbeat_at) < alive_threshold_eas
device_alive = device_alive and \
(state in set([None, 'initial', 'poll']))
devices[int(device_id)] = {'heartbeat_at': str(heartbeat_at),
'state': state,
'action': action,
'alive': device_alive}
# a folder is alive if and only if all the devices handling that
# folder are alive
folder_alive = folder_alive and device_alive
folders[key.folder_id] = (folder_alive, folder_name, devices)
# an account is alive if and only if all the folders of the account
# are alive
account_alive = account_alive and folder_alive
accounts[key.account_id] = (account_alive, provider_name, folders)
return accounts
示例4: create_event
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import scan_iter [as 别名]
'buys': [ { 'who': "Jim", 'required': 20 }, { 'who': "Amy", 'required': 17 } ] }
]
for next_event in events:
create_event(next_event['event'], next_event['qty'], next_event['price'])
for buy in next_event['buys']:
reserve_with_pending(buy['who'], next_event['event'], buy['required'])
post_purchases(next_event['event'])
for next_event in events:
print "=== Event: {}".format(next_event['event'])
print "Details: {}".format(redis.hgetall("events:" + next_event['event']))
print "Sales: {}".format(redis.smembers("sales:" + next_event['event']))
for buy in next_event['buys']:
print "Invoices for {}: {}".format(buy['who'], redis.smembers("invoices:" + buy['who']))
print "=== Orders"
for i in redis.scan_iter(match="purchase_orders:*"):
print redis.get(i)
print "=== Sales Summary \n{}".format(redis.hgetall("sales_summary"))
print "=== Sales Summary - hour of sale histogram"
hist = redis.get("sales_histogram:time_of_day")
for i in range(0, 24):
vals = ["GET", "u8", (i+1) * 8]
total_sales = int(redis.execute_command("BITFIELD", "sales_histogram:time_of_day", *vals)[0])
print " {} = {}".format(i, total_sales)