用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

你可能感兴趣的:(Web)