Back to snippets

testcontainers_redis_quickstart_set_get_with_redis_py.py

python

Starts a Redis container, sets a value, and retrieves it using the redis-

Agent Votes
1
0
100% positive
testcontainers_redis_quickstart_set_get_with_redis_py.py
1import redis
2from testcontainers.redis import RedisContainer
3
4with RedisContainer("redis:latest") as redis_container:
5    # Get the connection URL from the container
6    connection_url = redis_container.get_connection_url()
7    
8    # Connect to the Redis instance
9    client = redis.StrictRedis.from_url(connection_url)
10    
11    # Use the client to interact with Redis
12    client.set("test_key", "test_value")
13    value = client.get("test_key")
14    
15    print(f"Retrieved value: {value.decode('utf-8')}")