当前位置: 代码迷 >> Web前端 >> 用memcache处置webpy的session
  详细解决方案

用memcache处置webpy的session

热度:93   发布时间:2012-12-24 10:43:14.0
用memcache处理webpy的session

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

?

import memcache as memcache
from web.session import Store

class MemcacheStore(Store):
    def __init__(self, config):
        '''
        config = {
        'servers': ['127.0.0.1:11211'],
        'timeout': 1440
        }
        '''
        self.mc = memcache.Client(config['servers'])
        self.timeout = config['timeout']

    def __contains__(self, key):
        return True if self.mc.get(key) else False

    def __getitem__(self, key):
        return self.mc.get(key)

    def __setitem__(self, key, value):
        self.mc.set(key, value, self.timeout)

    def __delitem__(self, key):
        self.mc.delete(key)

    def cleanup(self, timeout):
        '''You need nothing to do. Memcache can handle it.'''
        pass
  相关解决方案