def close(self): """ Close the connections to all servers. >>> c = Client('localhost:11211') >>> c.close() """ self.quit() for r in self.hash.all_nodes(): with connpool.pooled_connection(r) as sock: sock.close()
def _per_host_version(self, cpool, rmap): with connpool.pooled_connection(cpool) as sock: socksend(sock, _qnsv(M._version)) (_, _, keylen, _, _, status, bodylen, _, _, extra) = sockresponse(sock) if status != R._no_error: raise MemcachedError("%d: %s" % (status, extra)) version_string = struct.unpack('!%ds' % ((bodylen-keylen), ), extra)[0] host_key = tuple(sock.getpeername()) rmap[host_key] = version_string
def quit(self): """ The quit command closes the remote socket. >>> c = Client('localhost:11211') >>> c.quit() True """ for r in self.hash.all_nodes(): with connpool.pooled_connection(r) as sock: socksend(sock, _qnsv(M._quit)) (_, _, _, _, _, status, _, _, _, extra) = sockresponse(sock) if status != R._no_error: raise MemcachedError("%d: %s" % (status, extra)) return True
def _per_host_stats(self, cpool, rmap): host_stats = {} with connpool.pooled_connection(cpool) as sock: socksend(sock, _qnsv(M._stat)) while 1: (_, _, keylen, _, _, status, bodylen, _, _, extra) = sockresponse(sock) if status != R._no_error: raise MemcachedError("%d: %s" % (status, extra)) if keylen == 0: # last response? host_key = tuple(sock.getpeername()) rmap[host_key] = host_stats break else: key, value = struct.unpack('!%ds%ds' % (keylen, (bodylen-keylen)), extra) host_stats[key] = value
def noop(self): """ The noop command Flushes outstanding getq/getkq's and can be used as a keep-alive. >>> c = Client('localhost:11211') >>> c.noop() True """ for r in self.hash.all_nodes(): with connpool.pooled_connection(r) as sock: socksend(sock, _qnsv(M._noop)) (_, _, _, _, _, status, _, _, _, extra) = sockresponse(sock) if status != R._no_error: raise MemcachedError("%d: %s" % (status, extra)) return True
def flush_all(self, expire=0): """ The flush command flushes all data the DB. Optionally this will happen after `expire` seconds. >>> c = Client('localhost:11211') >>> c.flush_all() True """ for r in self.hash.all_nodes(): with connpool.pooled_connection(r) as sock: socksend(sock, _f(M._flush, expire)) (_, _, _, _, _, status, _, _, _, extra) = sockresponse(sock) if status != R._no_error: raise MemcachedError("%d: %s" % (status, extra)) return True
def sock4key(self, key): r = self.hash.get_node(key) with connpool.pooled_connection(r) as sock: yield sock