webpy save session to redis
来源:互联网 发布:mac finder 拷贝路径 编辑:程序博客网 时间:2024/06/17 09:07
import redisimport webSESSION = '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 passimport webimport memcacheclass 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 memcacheweb.config.debug = Falseurls = ( "/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()
0 0
- webpy save session to redis
- webpy中使用session
- webpy session 设置
- redis is configured to save rdb snapshots
- Redis is configured to save RDB snapshots
- redis error MISCONF Redis is configured to save RDB snapshots
- redis问题:MISCONF Redis is configured to save RDB snapshots
- redis:MISCONF Redis is configured to save RDB snapshots
- session.save
- session.save()
- SQLAlchemy Session Store webpy DBStore
- MISCONF Redis is configured to save RDB snapshots
- MISCONF Redis is configured to save RDB snapshots
- MISCONF Redis is configured to save RDB snapshots
- MISCONF Redis is configured to save RDB snapshots
- MISCONF Redis is configured to save RDB snapshots
- (error) MISCONF Redis is configured to save RDB snapshots...解决
- MISCONF Redis is configured to save RDB snapshots
- Android调试串口无法连接串口设备的问题及解决办法
- C++类的静态成员详细讲解
- poj2762 Going from u to v or from v to u?
- golang带cookie post get请求 例子
- 线性时间排序 计数排序(Counting Sort)
- webpy save session to redis
- 数据处理---Spring Batch之基础
- sql日期格式化
- Android四大组件之Service精通
- RT-thread内核之互斥量
- 跟我学Android之八 ActionBar与菜单
- 操作系统server 2012 r2 安装简体中文语言包
- Java 用 try-catch 代替 if-else 的好处
- tablib(tld)