Redis扫描命令匹配选项在Python中不起作用

时间:2014-09-22 09:08:45

标签: python redis

我使用python redis通过匹配选项匹配一些信息?但它没有用。

 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(" scan"," 0",match =" userinfo _ *")   出[68]:   [' 28&#39 ;,    [' user_dev_20199116&#39 ;,     ' devinfo_af85d776fcc9dbc56e3cca16594e1c9ec36fecd10000000001&#39 ;,     ' devinfo_dd552211d1b97a825c416aaaf3c62ce8ce4661820000000002&#39 ;,     ' user_dev_2&#39 ;,     ' userinfo_20130243&#39 ;,     ' session_r4XXdvzJ28VuPMoWWU4cnsNv7NEAAAAAAQ ==&#39 ;,     ' devinfo_35372afae1de3dbf6a213f659c2814c7b1767f2400013436cc&#39 ;,     ' session_3IaTKySREBKjMTAi1puQSwzO20wAAAAAAQ ==&#39 ;,     ' session_3VUiEdG5eoJcQWqq88Ys6M5GYYIAAAAAAg ==&#39 ;,     ' user_dev_20130243']]

2 个答案:

答案 0 :(得分:22)

尝试这样的SCAN命令:

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

答案 1 :(得分: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?