免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2749 | 回复: 0
打印 上一主题 下一主题

用memcache处理webpy的session [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-14 10:57 |只看该作者 |倒序浏览
转:wangfsec

用memcache处理webpy的session



目前用的webpy0.34里没有对session提供memcache后端,自己参照其它的Store写了一个。


Python代码
  1. 1.import memcache as memcache   
  2. 2.from web.session import Store   
  3. 3.  
  4. 4.class MemcacheStore(Store):   
  5. 5.    def __init__(self, config):   
  6. 6.        '''''  
  7. 7.        config = {  
  8. 8.        'servers': ['127.0.0.1:11211'],  
  9. 9.        'timeout': 1440  
  10. 10.        }  
  11. 11.        '''  
  12. 12.        self.mc = memcache.Client(config['servers'])   
  13. 13.        self.timeout = config['timeout']   
  14. 14.  
  15. 15.    def __contains__(self, key):   
  16. 16.        return True if self.mc.get(key) else False  
  17. 17.  
  18. 18.    def __getitem__(self, key):   
  19. 19.        return self.mc.get(key)   
  20. 20.  
  21. 21.    def __setitem__(self, key, value):   
  22. 22.        self.mc.set(key, value, self.timeout)   
  23. 23.  
  24. 24.    def __delitem__(self, key):   
  25. 25.        self.mc.delete(key)   
  26. 26.  
  27. 27.    def cleanup(self, timeout):   
  28. 28.        '''''You need nothing to do. Memcache can handle it.'''  
  29. 29.        pass  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP