Redis Using Python Drivers

Install

The Redis drivers can be installed on the machine by using the python package redis.

pip3 install redis

Start a Redis Container Using Docker

There are many ways to init a Redis database. Docker might be the best one however. To init Redis with a Docker container, run this command:

docker run -p 6379:6379 --name name_of_container -d redis

After opening Docker & running the command above, you should see a docker container running redis.

Using Python to Define Redis Database

Redis stands for Remote Dictionary Service. This definition may make you think of Python dictionaries and in some sense that's true. Remember that Redis databases are defined using key-value pairs, like Python dictionaries.

The main difference between Redis & Python dictionaries are:

How to Define a Database in Redis

Redis databases can be initialized using the following:

import redis
# Connect
r = redis.Redis(host='localhost', port=6379, db=0)
# Write to DB
t = python_code_to_define_key_value_pairs
r.push('entries', t)

Note that the code above, d=0 inits the database 0. In Redis, by default, there are 16 available databases numbered from 0 ~ 15.

After defining the database and its entries, you may want to read those entries to ensure everything is defined correctly. You can do that with this:

import redis
# Connect
r = redis.Redis(host='localhost', port=6379, db=0)
# Read
for item in r.lrange('items', 0, -1)
print(item)

Methods in Redis

The table below explains the most common Redis methods that can be used to access and modify database entries.

References

Web References

Note References