Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created May 3, 2021 14:48
Show Gist options
  • Save BetterProgramming/3281c7cebf31a98ff5e7e7dff3d79891 to your computer and use it in GitHub Desktop.
Save BetterProgramming/3281c7cebf31a98ff5e7e7dff3d79891 to your computer and use it in GitHub Desktop.
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