def __init__(self, servers, debug=0): """ Create a new Client object with the given list of servers. @param servers: C{servers} is passed to L{set_servers}. @param debug: whether to display error messages when a server can't be contacted. (A lot less verbose than memcache.py). """ StringClient.__init__(self, servers) self.debug = debug
def get(self, key): """ Retrieves a key from the memcache. @return: The value or None if key doesn't exist (or if there are decoding errors). """ val = StringClient.getflags(self, key) if val: buf, flags = val if flags == 0: val = buf elif flags & Client._FLAG_INTEGER: val = int(buf) elif flags & Client._FLAG_LONG: val = long(buf) elif flags & Client._FLAG_PICKLE: try: val = pickle.loads(buf) except: self.debuglog('Pickle error...\n%s' % traceback.format_exc()) val = None else: self.debuglog("unknown flags on get: %x\n" % flags) val = None return val
def get_multi(self, keys): """ Retrieves multiple keys from the memcache doing just one query. Will use the flags of each entry to create the proper types (see get() and _convert()). @return: A dictionary of key/value pairs that were available. """ return StringClient.get_multiflags(self, keys)
def add(self, key, val, time=0): """ Add new key with value. Like L{set}, but only stores in memcache if the key doesn't already exist. @return: Nonzero on success. @rtype: int """ val, flags = self._convert(val) return StringClient.add(self, key, val, time, flags)
def replace(self, key, val, time=0): """ Replace existing key with value. Like L{set}, but only stores in memcache if the key already exists. The opposite of L{add}. @return: Nonzero on success. @rtype: int """ val, flags = self._convert(val) return StringClient.replace(self, key, val, time, flags)
def set(self, key, val, time=0): """ Unconditionally sets a key to a given value in the memcache. The C{key} can optionally be an tuple, with the first element being the hash value, if you want to avoid making this module calculate a hash value. You may prefer, for example, to keep all of a given user's objects on the same memcache server, so you could use the user's unique id as the hash value. @return: Nonzero on success. @rtype: int """ val, flags = self._convert(val) return StringClient.set(self, key, val, time, flags)
def get_multi(self, keys): return StringClient.get_multiflags(self, keys)