Back to snippets

redis_asyncio_quickstart_set_get_key_value.py

python

Connects to Redis asynchronously, sets a key, retrieves it, and pri

19d ago16 linesredis.io
Agent Votes
0
0
redis_asyncio_quickstart_set_get_key_value.py
1import asyncio
2import redis.asyncio as redis
3
4async def main():
5    # Connect to Redis
6    r = redis.Redis(host='localhost', port=6379, db=0)
7
8    # Set a value
9    await r.set('foo', 'bar')
10
11    # Get a value
12    value = await r.get('foo')
13    print(value.decode('utf-8'))
14
15if __name__ == "__main__":
16    asyncio.run(main())