def __init__(self, servers, behaviors=None, binary=False, username=None, password=None): self._client = Client(servers, behaviors, binary, username, password)
class MemCacheFactory(object): """ memory cache factory """ memcache_configs = None memcache_dict = dict() run_mode = 'development' @classmethod def get_instance(cls, session, runmod=None): if not cls.memcache_configs: with file(os.path.join(Settings.SITE_ROOT_PATH, 'configs/memcaches.yaml'), 'r') as file_stream: yml = yaml.load(file_stream) if runmod is None: runmod = cls.run_mode cls.memcache_configs = yml.get(runmod) memcache_factory = cls.memcache_dict.get(session, None) if memcache_factory is not None: return memcache_factory memcache_config = cls.memcache_configs.get(session) if not memcache_config.get("enabled", False): return None if memcache_config is not None: memcache_factory = MemCacheFactory(memcache_config.get('servers'), behaviors=memcache_config.get('behaviors'), binary=memcache_config.get('binary'), username=memcache_config.get('username'), password=memcache_config.get('password')) cls.memcache_dict[session] = memcache_factory return memcache_factory else: return None def __init__(self, servers, behaviors=None, binary=False, username=None, password=None): self._client = Client(servers, behaviors, binary, username, password) def add(self, *args, **kwargs): """ Set a key only if doesn't exist. """ return self._client.add(*args, **kwargs) def add_multi(self, *args, **kwargs): """ Add multiple keys at once. """ return self._client.add_multi(*args, **kwargs) def append(self, *args, **kwargs): """ Append data to a key. """ return self._client.append(*args, **kwargs) def cas(self, *args, **kwargs): """ Attempt to compare-and-store a key by CAS ID. """ return self._client.cas(*args, **kwargs) def decr(self, *args, **kwargs): """ Decrement a key by a delta. """ return self._client.decr(*args, **kwargs) def delete(self, *args, **kwargs): """ Delete a key. """ return self._client.delete(*args, **kwargs) def delete_multi(self, *args, **kwargs): """ Delete multiple keys at once. """ return self._client.delete_multi(*args, **kwargs) def disconnect_all(self, *args, **kwargs): """ Disconnect from all servers and reset own state. """ return self._client.disconnect_all(*args, **kwargs) def flush_all(self, *args, **kwargs): """ Flush all data on all servers. """ return self._client.flush_all(*args, **kwargs) def get(self, *args, **kwargs): """ Retrieve a key from a memcached. """ return self._client.get(*args, **kwargs) def gets(self, *args, **kwargs): """ Retrieve a key and cas_id from a memcached. """ return self._client.gets(*args, **kwargs) def get_behaviors(self, *args, **kwargs): """ Get behaviors dict. """ return self._client.get_behaviors(*args, **kwargs) def get_multi(self, *args, **kwargs): """ Get multiple keys at once. """ return self._client.get_multi(*args, **kwargs) def get_stats(self, *args, **kwargs): """ Retrieve statistics from all memcached servers. """ return self._client.get_stats(*args, **kwargs) def hash(self, *args, **kwargs): """ Hash value of *key*. """ return self._client.hash(*args, **kwargs) def incr(self, *args, **kwargs): """ Increment a key by a delta. """ return self._client.incr(*args, **kwargs) def incr_multi(self, *args, **kwargs): """ Increment more than one key by a delta. """ return self._client.incr_multi(*args, **kwargs) def prepend(self, *args, **kwargs): """ Prepend data to a key. """ return self._client.prepend(*args, **kwargs) def replace(self, *args, **kwargs): """ Set a key only if it exists. """ return self._client.replace(*args, **kwargs) def set(self, *args, **kwargs): """ Set a key unconditionally. """ return self._client.set(*args, **kwargs) def set_behaviors(self, *args, **kwargs): """ Set behaviors dict. """ return self._client.set_behaviors(*args, **kwargs) def set_multi(self, *args, **kwargs): """ Set multiple keys at once. """ return self._client.set_multi(*args, **kwargs) def touch(self, *args, **kwargs): """ Change the TTL of a key. """ return self._client.touch(*args, **kwargs)