【问题标题】:Redis scan command match option does not work in PythonRedis 扫描命令匹配选项在 Python 中不起作用
【发布时间】:2018-01-31 13:16:12
【问题描述】:

我使用 python redis 通过 match 选项匹配一些信息?但它不起作用。

 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_*")

似乎 match 选项在扫描命令中确实有效。

在 [68] 中:cache.rds.execute_command("scan", "0", match="userinfo_*") 出[68]: ['28', ['user_dev_20199116', 'devinfo_af85d776fcc9dbc56e3cca16594e1c9ec36fecd10000000001', 'devinfo_dd552211d1b97a825c416aaaf3c62ce8ce4661820000000002', 'user_dev_2', 'userinfo_20130243', 'session_r4XXdvzJ28VuPMoWWU4cnsNv7NEAAAAAAQ==', 'devinfo_35372afae1de3dbf6a213f659c2814c7b1767f2400013436cc', 'session_3IaTKySREBKjMTAi1puQSwzO20wAAAAAAQ==', 'session_3VUiEdG5eoJcQWqq88Ys6M5GYYIAAAAAAAg==', 'user_dev_20130243']]

【问题讨论】:

  • 我扫描userinfo,以“userinfo”开头查看,问题解决。

标签: python redis


【解决方案1】:

试试这样的 SCAN 命令:

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

【讨论】:

    【解决方案2】:

    hscan 似乎比 scan 快得多,最好使用 hscan只有在您确定密钥的情况下,如果您需要搜索所有密钥然后使用 scan

    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 比 scan 快,只有当您正在搜索正确的键时,否则要获取具有某种相似模式的所有键,请使用 scan。以供参考。 How to search a key pattern in redis hash?

    【讨论】:

      猜你喜欢
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多