12

I use python redis to match some infomation by using match option? but it doesn't work.

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

It seems that match option does work in scan command.

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

1
  • I scan the userinfo, check it by starting with "userinfo", and solve the problem. Commented Sep 25, 2014 at 8:18

2 Answers 2

31

Try the SCAN command like this :

r = Redis(....) #redis url
for user in r.scan_iter(match='userinfo_*'):
  print(user)
Sign up to request clarification or add additional context in comments.

Comments

0

hscan seems much quicker than scan, better use hscan, only if you are sure about the key, if you need to search all keys then use 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 is quicker than scan, only if you are searching for a correct key, else to fetch all keys with some similar pattern, go for scan. For reference. How to search a key pattern in redis hash?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.