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
import web
import memcache
class MemCacheStore(web.session.Store):
mc = None
def __init__(self):
self.mc = memcache.Client(['127.0.0.1:11211'], debug=0)
def __contains__(self, key):
return self.mc.get(key) != None
def __getitem__(self, key):
return self.mc.get(key)
def __setitem__(self, key, value):
self.mc.set(key, value, time = web.config.session_parameters["timeout"])
def __delitem__(self, key):
self.mc.delete(key)
def cleanup(self, timeout):
pass # Not needed as we assigned the timeout to memcache
web.config.debug = False
urls = (
"/count", "count",
"/reset", "reset"
)
app = web.application(urls, locals())
session = web.session.Session(app, MemCacheStore(), initializer={'count': 0})
class count:
def GET(self):
session.count += 1
return str(session.count)
class reset:
def GET(self):
session.kill()
return ""
if __name__ == "__main__":
app.run()
webpy save session to redis
最新推荐文章于 2022-03-01 17:41:12 发布