Created
May 3, 2021 14:48
-
-
Save BetterProgramming/3281c7cebf31a98ff5e7e7dff3d79891 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis | |
import time | |
# Establish a connection to the Redis database 1 at | |
# redis://localhost:6379 | |
r = redis.Redis(host='localhost', port=6379, db=1) | |
# SET hello world | |
r.set('hello', 'world') # True | |
# GET hello | |
world = r.get('hello') | |
print(world.decode()) # "world" | |
# SET bye "In 60 seconds, I'll self-delete" EX 60 | |
r.set('bye', "In 60 seconds, I'll self-delete", ex=60) # True | |
expiring_message = r.get('bye') | |
print(expiring_message.decode()) # "In 60 seconds, I'll self-delete" | |
# Wait 60 seconds | |
time.sleep(60) | |
# GET bye | |
expired_message = r.get('bye') | |
print(expired_message.decode()) # "None" | |
# DEL hello | |
r.delete('hello') | |
print(r.get('hello').decode()) # "None" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment