Introduction
Redis is a fast database that stores data in memory. Using Python with Redis lets you quickly save and get data in your programs.
Redis is a fast database that stores data in memory. Using Python with Redis lets you quickly save and get data in your programs.
import redis r = redis.Redis(host='localhost', port=6379, db=0) r.set('key', 'value') value = r.get('key')
redis.Redis() to connect to your Redis server.set() to save data and get() to retrieve it.r.set('name', 'Alice') print(r.get('name'))
r.set('count', 10) print(r.get('count'))
r.delete('name') print(r.get('name'))
This program connects to Redis, saves the color 'blue', then gets and prints it.
import redis r = redis.Redis(host='localhost', port=6379, db=0) r.set('color', 'blue') value = r.get('color') print(value.decode('utf-8'))
Redis stores data as bytes, so use decode('utf-8') to convert to string.
Make sure Redis server is running before connecting.
Use r.delete('key') to remove data.
Redis with Python lets you store and get data fast using simple commands.
Use set() to save and get() to retrieve data.
Remember to decode bytes to strings when reading data.
set() method do in redis-py?set()set() method is used to save data in Redis by associating a value with a key.get() retrieves data, delete() removes keys, and keys() lists keys, so they do not store data.set() means store data [OK]r.get('name') is correct.import redis
r = redis.Redis()
r.set('color', 'blue')
value = r.get('color')
print(value)get() method returns the value as bytes, so value is b'blue'.value shows the bytes literal b'blue', not the decoded string.import redis
r = redis.Redis()
r.set('age', 30)
age = r.get('age')
print(age)get() returns bytes, so printing age shows bytes, not a readable string or integer. Decoding is needed.json.dumps() converts dict to string for set(), and json.loads() converts back after get().hmset() is deprecated; direct dict storage fails; storing string without decoding causes errors.Pass the test and this pattern moves unknown