def _hexists(rsock, key, field): # alex rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"HEXISTS", [ key, field, ])) return handleResponse(rsock)
def _hdel(rsock, key, field): # alex rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"HDEL", [ key, field, ])) return handleResponse(rsock)
def flushdb(rsock): """ Remove all keys from the current database. @return: Status code reply. """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"FLUSHDB")) return handleResponse(rsock)
def dbsize(rsock): """ Returns the number of keys in the currently selected database @return: Integer reply """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"DBSIZE")) return handleResponse(rsock)
def select(rsock, index): """ Selects the DB which has the specified zero-based numeric index. @return: Status code reply. OK if connection was successful """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"SELECT", [index])) return handleResponse(rsock)
def _quit(rsock): # redis close """ Asks the server to close the connection. The socket closes. Subsequent connections will use different sockets. @return: Status code reply. Always returns OK """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"QUIT")) rsock.close() # close the redis socket return "OK"
def _srem(rsock, aset, key): """ 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 """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"SREM", [aset, key])) return handleResponse(rsock)
def _hkeys(rsock, 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. """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"HKEYS", [key])) return handleResponse(rsock)
def _hget(rsock, 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 """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"HGET", [key, field])) return handleResponse(rsock)
def _hset(rsock, 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. """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"HSET", [key, field, value])) return handleResponse(rsock)
def _get(rsock, 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. """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"GET", [key])) return handleResponse(rsock)
def _sinterstore(rsock, destination, keys): """ 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 """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"SINTERSTORE", [destination] + keys)) return handleResponse(rsock)
def _hmset(rsock, 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 """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"HMSET", [key] + list(field_value))) return handleResponse(rsock)
def _exists(rsock, key): """ Test if the specified key exists. @return: Integer reply. 1, if the key exists 0, otherwise """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"EXISTS", [key])) return handleResponse(rsock)
def _hgetall(rsock, 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. """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"HGETALL", [key])) # Returns a list response = handleResponse(rsock) # 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 _decrby(rsock, key, by): rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"DECRBY", [key, by])) return handleResponse(rsock)
def _incr(rsock, key): rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"INCR", [key])) return handleResponse(rsock)
def _keys(rsock, pattern=None): rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"KEYS", [pattern])) return handleResponse(rsock)
def _expire(rsock, key, value): rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"EXPIRE", [key, value])) return handleResponse(rsock)
def save(rsock): """ Synchronously save the dataset to disk. """ rsock = reconnect(rsock) rsock.sendall(comm.constructMessage(b"SAVE")) return handleResponse(rsock)