The redis drivers can be installed on the machine by using the python package redis
.
pip3 install redis
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.
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:
GET
, SEL
and DEL
.copy()
, clear()
, and pop()
.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)
The table below explains the most common Redis methods that can be used to access and modify database entries.
GET
GET
to get the value of a keynil
is returned.GET
only handles string values.SET
SET
method to set the key to hold the string value.SET
.DEL
DEL
method to remove specified keys.