Created
September 7, 2011 23:02
-
-
Save pokstad/1202066 to your computer and use it in GitHub Desktop.
Redis session store backend for web.py
This file contains 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 web | |
SESSION = 'SESSION:' | |
class RedisStore(web.session.Store): | |
"""Store for saving a session in redis: | |
import rediswebpy | |
session = web.session.Session(app, rediswebpy.RedisStore(), initializer={'count': 0}) | |
""" | |
def __init__(self, ip='localhost', port=6379, db=0, initial_flush=False): | |
self.redis_server = redis.Redis(ip, port, db) | |
if initial_flush: | |
""" | |
flushing the database is very important when you update your | |
Session object initializer dictionary argument. | |
E.g. | |
# Before Update: | |
session = web.session.Session(app, | |
rediswebpy.RedisStore(initial_flush=True), | |
initializer={'a':1}) | |
# After Update: | |
session = web.session.Session(app, | |
rediswebpy.RedisStore(initial_flush=True), | |
initializer={'a':1, 'b':2}) | |
# This will cause an error if initial_flush=False since existing | |
# sessions in Redis will not contain the key 'b'. | |
""" | |
self.redis_server.flushdb() | |
def __contains__(self, key): | |
# test if session exists for given key | |
return bool(self.redis_server.get(SESSION+key)) | |
def __getitem__(self, key): | |
# attempt to get session data from redis store for given key | |
data = self.redis_server.get(SESSION+key) | |
# if the session existed for the given key | |
if data: | |
# update the expiration time | |
self.redis_server.expire(SESSION+key, | |
web.webapi.config.session_parameters.timeout) | |
return self.decode(data) | |
else: | |
raise KeyError | |
def __setitem__(self, key, value): | |
# set the redis value for given key to the encoded value, and reset the | |
# expiration time | |
self.redis_server.set(SESSION+key, | |
self.encode(value)) | |
self.redis_server.expire(SESSION+key, | |
web.webapi.config.session_parameters.timeout) | |
def __delitem__(self, key): | |
self.redis_server.delete(SESSION+key) | |
def cleanup(self, timeout): | |
# since redis takes care of expiration for us, we don't need to do any | |
# clean up | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment