Redis#
Python has a special API to interact with redis. Here are some examples of how to use it. There is a special dedicated to redis page, there you can learn more about it’s datatypes and operations on them. This page focuses on options to use it with python programms.
import redis
Redis client#
Interaction with redis is performed through a special object - redis client. Find out more in specific page.
The following cell creates such an object.
redis_client = redis.Redis()
type(redis_client)
redis.client.Redis
You can retrieve the connection settings through the redis.client.Redis.connection_pool.connection_kwargs
attribute.
redis_client.connection_pool.connection_kwargs
{'db': 0,
'username': None,
'password': None,
'socket_timeout': None,
'encoding': 'utf-8',
'encoding_errors': 'strict',
'decode_responses': False,
'retry_on_error': [],
'retry': None,
'health_check_interval': 0,
'client_name': None,
'redis_connect_func': None,
'credential_provider': None,
'host': 'localhost',
'port': 6379,
'socket_connect_timeout': None,
'socket_keepalive': None,
'socket_keepalive_options': None}
Finally, don’t forget to close the redis client.
redis_client.close()
Save value#
You can store a specific value as a string in Redis using the set
method. To retrieve this value, use the get
method.
The following cell adds the value 25 to Redis under the key value
.
redis_client.set("value", 25)
True
Now lets check if value really added to redis.
!redis-cli GET value
"25"
And same operation but from python client.
redis_client.get("value")
b'25'
Let’s remove the value we saved from Redis.
redis_client.delete("value")
1
However, attempting to access a non-existent key will not result in an error; it will simply return None
.
redis_client.get("value") is None
True
Save list#
Redis also supports a list data structure. You can save a Python list using the rpush
method and retrieve it using the lrange
method.
The following cell demonstrates how to push a Python list to Redis.
lst = ["a", "b", "c", "d", "e"]
redis_client.rpush("python-list", *lst)
5
Let’s check if our Python list actually exists in Redis.
!redis-cli LRANGE python-list 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
Or same lrange
method in Python to retrieve the list from Redis:
redis_client.lrange("python-list", 0, -1)
[b'a', b'b', b'c', b'd', b'e']
Let’s clean up Redis after our experiments.
redis_client.delete("python-list")
1
Save dictionary#
These python clients usually have methods that correspond to functions in the Redis database. So let us try to create zset
using the zadd
method.
redis_client.zadd(
"animal-weights",
{
"elephant": 50,
"lion": 20,
"giraffe": 30,
"cheetah": 10,
"rhinoceros": 40
}
)
5
Now you can access the zset you just created from the redis command line.
!redis-cli ZRANGE animal-weights 0 -1 withscores
1) "cheetah"
2) "10"
3) "lion"
4) "20"
5) "giraffe"
6) "30"
7) "rhinoceros"
8) "40"
9) "elephant"
10) "50"
Or use it’s equivalent in python.
redis_client.zrange("animal-weights", 0, -1, withscores=True)
[(b'cheetah', 10.0),
(b'lion', 20.0),
(b'giraffe', 30.0),
(b'rhinoceros', 40.0),
(b'elephant', 50.0)]
To remove the garbage from the global redis environment, we’ll delete the set we just created.
redis_client.delete("animal-weights")
1