如何在django-redis中使用redis管道



我想在django-Redis中使用Redis管道(执行多个命令(。

我们可以在Redis中使用multi和exec命令,但如何在django-Redis中使用呢?

一种解决方案是:

我有一个哈希键列表,我想使用哈希键获得所有哈希。

在每次迭代时,将命令发送到redis服务器以逐个获取哈希。

for hashkey in feedlist:
result = con.execute_command('hgetall',hashkey)
print(result)

这不是一个好主意,相反,我们可以使用redis管道。但是如何在django-Redis中实现Redis管道呢?

在django-Redis中使用Redis管道。

#first create pipline object using con object 
pipeline = con.pipeline()

将所有命令插入管道。逐个获取所有散列密钥,并将它们插入管道中。

if feedlist:
for post in feedlist:
pipeline.execute_command('hgetall',post)
#hgetall redis command to get all items of given hash key

然后调用管道上的execute()方法。它将把结果返回给结果变量。

result = pipeline.execute()

Redis事务

MULTI、EXEC、DISCARD和WATCH是Redis中事务的基础。它们允许在一个步骤中执行一组命令。

优点

  • 在一次调用中,我们可以获取所有记录
  • 减少了网络呼叫

同时读取

Redis交易公文

最新更新