def dbsize(self): """ Returns the number of keys in the currently selected database @return: Integer reply """ self.connect() self.sock.sendall(comm.constructMessage("DBSIZE")) return self.handleResponse()
def flushdb(self): """ Remove all keys from the current database. @return: Status code reply. """ self.connect() self.sock.sendall(comm.constructMessage("FLUSHDB")) return self.handleResponse()
def select(self, index): """ Selects the DB which has the specified zero-based numeric index. @return: Status code reply. OK if connection was successful """ self.connect() self.sock.sendall(comm.constructMessage("SELECT",[index])) return self.handleResponse()
def hget(self, key, field): """ Returns the value associated with field, in the hash at key. @return: Bulk reply. The value associated with field, or None if field is not present or key does not exist """ self.connect() self.sock.sendall(comm.constructMessage("HGET", [key,field])) return self.handleResponse()
def hset(self, key, field, value): """ Sets the field in the hash stored at key, to value. @return: Integer reply. 1 if field is new and the value was set. 0 if field already exists in the hash and the value was updated. """ self.connect() self.sock.sendall(comm.constructMessage("HSET", [key, field, value])) return self.handleResponse()
def get(self, key): """ Get the value of a key. If the key does not exist, the special value nil is returned. @return: Bulk reply. The value of key, or None when key does not exist. """ self.connect() self.sock.sendall(comm.constructMessage("GET", [key])) return self.handleResponse()
def set(self,key,value): """ Set the string value of a key. If key already holds its value, its overwritten, regardless of its type @return: Status code reply. Always OK """ self.connect() self.sock.sendall(comm.constructMessage("SET", [key,value])) return self.handleResponse()
def hkeys(self, key): """ Returns all field names in the hash stored at key. @return: Multi-bulk reply. List of fields in the hash, or an empty list when key does not exist. """ self.connect() self.sock.sendall(comm.constructMessage("HKEYS", [key])) return self.handleResponse()
def hmset(self, key, *field_value): """ Sets the specified fields to their respective values in the hash stored at key. @param field_value: Tuple of arguments. field1, value1, field2, value2, ... @return: Status code reply """ self.connect() self.sock.sendall(comm.constructMessage("HMSET", [key] + list(field_value))) return self.handleResponse()
def exists(self, key): """ Test if the specified key exists. @return: Integer reply. 1, if the key exists 0, otherwise """ self.connect() self.sock.sendall(comm.constructMessage("EXISTS", [key])) return self.handleResponse()
def hgetall(self, key): """ Returns all fields and values of the hash stored at key @return: Multi-bulk reply. A dictionary of field:value pairs stored at key. """ self.connect() self.sock.sendall(comm.constructMessage("HGETALL", [key])) # Returns a list response = self.handleResponse() # Which I transform to a dictionary dic = {} if isinstance(response, list): for i in range(0, len(response), 2): dic[response[i]] = response[i+1] return dic return False
def quit(self): """ Asks the server to close the connection. The socket closes. Subsequent connections will use different sockets. @return: Status code reply. Always returns OK """ self.connect() self.sock.sendall(comm.constructMessage("QUIT")) # self.sock = None # The socket has closed the connection. None # is used in connect() to symbolize this. #del self # del, only removes the local reference to the # object. So if I had placed "del object" here, # the object would just unbind from the self var. # It would still exist on the calling program's # namespace, since there still would be a reference # there return "OK"
def keys(self, pattern = None): self.connect() self.sock.sendall(comm.constructMessage("KEYS", [pattern])) return self.handleResponse()
def save(self): """ Synchronously save the dataset to disk. """ self.connect() self.sock.sendall(comm.constructMessage("SAVE")) return self.handleResponse()