2014-09-22 82 views
4

我使用python redis通過匹配選項來匹配某些信息?但它不起作用。Redis掃描命令匹配選項在Python中不起作用

import redis 
import REDIS 

class UserCache(object): 
    def __init__(self): 
     self.rds = self._connectRds() 

    def _connectRds(self): 
     _db = REDIS['usercache'] 
     pool = redis.ConnectionPool(host=_db['HOST'], port= _db['PORT'], db=_db['DB']) 
     rds = redis.Redis(connection_pool=pool) 
     return rds 

cache = UserCahce() 
cache.rds.execute("scan", "0", match="userinfo_*") 

似乎匹配選項在掃描命令中工作。

在[68]:cache.rds.execute_command( 「掃描」, 「0」,匹配= 「userinfo_ *」) 缺貨[68]: [ '28', [ 'user_dev_20199116', 'devinfo_af85d776fcc9dbc56e3cca16594e1c9ec36fecd10000000001', 'devinfo_dd552211d1b97a825c416aaaf3c62ce8ce4661820000000002', 'user_dev_2', 'userinfo_20130243', 'session_r4XXdvzJ28VuPMoWWU4cnsNv7NEAAAAAAQ ==', 'devinfo_35372afae1de3dbf6a213f659c2814c7b1767f2400013436cc', 'session_3IaTKySREBKjMTAi1puQSwzO20wAAAAAAQ ==', 「session_3VUiEdG5eoJcQWqq88Ys6M5GYYIAA AAAAG ==」, 'user_dev_20130243']

+0

我掃描用戶信息,通過從「userinfo」開始檢查並解決問題。 – 2014-09-25 08:18:10

回答

18

嘗試SCAN命令是這樣的:

r = Redis(....) #redis url 
for user in r.scan_iter(match='userinfo_*'): 
    print(user) 
0

HSCAN似乎比掃描更快,更好地運用HSCAN只有當你確定關於關鍵,如果你需要搜索所有的鍵,然後用掃描

for name in r.hscan_iter('live_AP_460000','name'): 
    print(name) #for a single child key 

for hash in r.hscan_iter('live_AP_460000'): 
    print(hash) #for all child keys 

hscan比掃描快,只有當您正在搜索正確的密鑰,否則獲取具有某種相似模式的所有密鑰,請參閱掃描。以供參考。 How to search a key pattern in redis hash?