Source code for watson.http.sessions.memcache

# -*- coding: utf-8 -*-
import collections
from watson.http.sessions import StorageMixin
from watson.common.contextmanagers import suppress
with suppress(ImportError):
    import memcache

[docs]class Storage(StorageMixin): """A memcache based storage adapter for session data. """ client = None
[docs] def __init__(self, id=None, timeout=None, autosave=True, config=None): super(Storage, self).__init__(id, timeout, autosave) settings = {'servers': ['']} self.config = collections.ChainMap(config or {}, settings)
def open(self): if not self.client: try: self.client = memcache.Client(self.config['servers']) except: raise ImportError('You must have python3-memcached installed.') def close(self): self.client.disconnect_all() return True def load(self): self._data = self._load() or {} def _load(self): return self.client.get( def _exists(self): return True if self.get( else False def _save(self, expires): self.client.set(,, self.timeout) def _destroy(self): self.client.delete(