本文介绍了Python Redis 交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用python编写使用redis的应用程序.我用谷歌搜索但我找不到我的问题的任何结果.通常,我这样做:

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')

然后执行所有的获取和设置.但是我可以在 redis 中做这样的事情:

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

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

我的意思是,我想使用两个或多个实例,每个实例存储不同的内容(例如 rs1 用于 url,rs2 用于标头等......).而且我还想知道如何删除所有键(例如在 rs1 中删除所有记录).有什么好的教程,资源?注意:我需要使用redis,因为我需要进行快速检查和存储,比如爬虫的url-seen.

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.Redisredis.StrictRedis 都采用整数 db 参数作为构造函数参数.这将为您提供一个有效的孤立实例.

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.

您可以执行以下操作:

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

flushdb() 将清除您连接的数据库的所有键,而 flushall() 将清除每个数据库的所有键.

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 交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:35