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.
Redis can handle many connections, and by default, Redis has a maximum number of client connections set at 10,000 connections. You can set the maximum number of client connections you want the Redis server to accept by altering the maxclient from within the redis. conf file.
Actually, you can store python objects in redis using the built-in module pickle. Here is example. This is dangerous: unpickling can execute code. The JSON solution is more robust.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With