Python Redis interaction

2019-04-22 07:20发布

问题:

I want to write application in python which uses redis. I googled but I could not find any results for my question. Usually, I do this:

import redis

rs = redis.Redis('localhost')

then do all gets and sets. But can I in redis do something like this:

rs1 = redis.Redis('app1')
rs2 = redis.Redis('app2')

I mean, I want to use two or more instances, each of which stores different things (for example rs1 for urls, rs2 for headers, etc...). And also I want to know how to delete all keys (for example in rs1 delete all records). Any good tutorial, resource? Note: I need to use redis because I need to preform fast check and store, like url-seen for crawler.

回答1:

As showed in the getting started section of the docs redis.Redis and redis.StrictRedis both take an integer db argument as a constructor parameter. That will get you an effectively silo'ed instance.

You could do something like the following:

rs1 = redis.Redis(host="localhost", db=0)
rs2 = redis.Redis(host="localhost", db=1)

flushdb() will clear all the keys for the database you are connected to, while flushall() will clear all the keys for every database.



标签: python redis