33 lines
812 B
Python
33 lines
812 B
Python
|
"""Session management interface module."""
|
||
|
from json import dumps, loads
|
||
|
|
||
|
|
||
|
class RedisSessionDB(dict):
|
||
|
"""Dict interface for Redis session management."""
|
||
|
|
||
|
def __init__(self, connection):
|
||
|
self.connection = connection
|
||
|
|
||
|
def keys(self):
|
||
|
return self.connection.keys()
|
||
|
|
||
|
def flushall(self):
|
||
|
self.connection.flushall()
|
||
|
|
||
|
def __getitem__(self, key):
|
||
|
value = self.connection.get(key)
|
||
|
if value is None:
|
||
|
raise KeyError(key)
|
||
|
utf8ified = str(value, "utf-8")
|
||
|
return loads(utf8ified)
|
||
|
|
||
|
def __setitem__(self, key, value):
|
||
|
value = dumps(value)
|
||
|
self.connection.set(key, value)
|
||
|
|
||
|
def __contains__(self, item):
|
||
|
return self.connection.exists(item)
|
||
|
|
||
|
def __delitem__(self, key):
|
||
|
self.connection.delete(key)
|