djangoredisredis-pydjango-redis

How we can use redis pipeline in django-redis?


I want to use Redis pipeline (execute multiple commands) in django-redis.

We can use multi and exec command in Redis but how we can in django-redis ?

One solution is :

I have list of hash keys i want to get all hashes using of hash key.

On every iteration command send to redis server to fetch one by one hash.

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

This is not a good idea instead of this we can use redis pipeline. But how i can achieve Redis pipeline in django-redis ?


Solution

  • Using Redis pipeline in django-redis.

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

    Inserting all commands into pipeline. Fetching all hash keys one by one and inserting them into pipeline.

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

    Then call the execute() method on pipeline. It will return result back to result variable.

         result = pipeline.execute()
    

    Redis transactions

    MULTI, EXEC, DISCARD and WATCH are the foundation of transactions in Redis. They allow the execution of a group of commands in a single step.

    Advantages


    Read also

    Redis transactions official doc