lib/oxblood/session.rb in oxblood-0.1.0.dev5 vs lib/oxblood/session.rb in oxblood-0.1.0.dev6

- old
+ new

@@ -70,10 +70,13 @@ # @param [String] key under which hash is stored # @param [String] field to increment # @param [Integer] increment by value # # @return [String] the value of field after the increment + # @return [RError] field contains a value of the wrong type (not a string). + # Or the current field content or the specified increment are not parsable + # as a double precision floating point number. def hincrbyfloat(key, field, increment) run(:HINCRBYFLOAT, key, field, increment) end # Get all the keys in a hash @@ -169,27 +172,22 @@ # key does not exist def hvals(key) run(:HVALS, key) end - # Incrementally iterate hash fields and associated values - # @see http://redis.io/commands/hscan - # - # @todo Implement this command - def hscan(key, cursor) - end - # ------------------ Strings --------------------- # ------------------ Connection --------------------- # Authenticate to the server # @see http://redis.io/commands/auth # # @param [String] password # # @return [String] 'OK' + # @return [RError] if wrong password was passed or server does not require + # password def auth(password) run(:AUTH, password) end # Echo the given string @@ -229,10 +227,11 @@ # @see http://redis.io/commands/select # # @param [Integer] index database to switch # # @return [String] 'OK' + # @return [RError] if wrong index was passed def select(index) run(:SELECT, index) end # ------------------ Server --------------------- @@ -240,13 +239,14 @@ # Returns information and statistics about the server in a format that is # simple to parse by computers and easy to read by humans # @see http://redis.io/commands/info # # @param [String] section used to select a specific section of information + # + # @return [String] raw redis server response as a collection of text lines. def info(section = nil) section ? run(:INFO, section) : run(:INFO) - # FIXME: Parse response end # ------------------ Keys ------------------------ # Delete a key @@ -257,18 +257,32 @@ # @return [Integer] the number of keys that were removed def del(*keys) run(*keys.unshift(:DEL)) end - # Find all keys matching the given pattern - # @see http://redis.io/commands/keys + # Return a serialized version of the value stored at specified key. + # @see http://redis.io/commands/dump # - # @param [String] pattern used to match keys - def keys(pattern) - run(:KEYS, pattern) + # @param [String] key + # + # @return [String] serialized value + def dump(key) + run(:DUMP, key) end + # Determine if a key exists + # @see http://redis.io/commands/exists + # + # @param [String, Array<String>] keys to check + # + # @return [Integer] the number of keys existing among the ones specified as + # arguments. Keys mentioned multiple times and existing are counted + # multiple times. + def exists(*keys) + run(*keys.unshift(:EXISTS)) + end + # Set a key's time to live in seconds # @see http://redis.io/commands/expire # # @param [String] key to expire # @param [Integer] seconds number of seconds @@ -277,10 +291,174 @@ # the timeout could not be set. def expire(key, seconds) run(:EXPIRE, key, seconds) end + # Set the expiration for a key as a UNIX timestamp + # @see http://redis.io/commands/expireat + # + # @param [String] key + # @param [Integer] timestamp in UNIX format + # + # @return [Integer] 1 if the timeout was set. 0 if key does not exist or + # the timeout could not be set. + def expireat(key, timestamp) + run(:EXPIREAT, key, timestamp) + end + + # Find all keys matching the given pattern + # @see http://redis.io/commands/keys + # + # @param [String] pattern used to match keys + def keys(pattern) + run(:KEYS, pattern) + end + + # Move a key to another database + # @see http://redis.io/commands/move + # + # @param [String] key + # @param [Integer] db index + # + # @return [Integer] 1 if key was moved and 0 otherwise. + def move(key, db) + run(:MOVE, key, db) + end + + # Inspect the internals of Redis objects + # @see http://redis.io/commands/object + # + # @param [String] subcommand `REFCOUNT`, `ENCODING`, `IDLETIME` + # @param [String] key + # + # @return [Integer] in case of `REFCOUNT` and `IDLETIME` subcommands + # @return [String] in case of `ENCODING` subcommand + # @return [nil] if object you try to inspect is missing + def object(subcommand, key) + run(:OBJECT, subcommand, key) + end + + # Remove expiration from a key + # @see http://redis.io/commands/persist + # @param [String] key + # + # @return [Integer] 1 if the timeout was removed and 0 otherwise + def persist(key) + run(:PERSIST, key) + end + + # Set a key's time to live in milliseconds + # @see http://redis.io/commands/pexpire + # + # @param [String] key + # @param [Integer] milliseconds + # + # @return [Integer] 1 if the timeout was set and 0 otherwise + def pexpire(key, milliseconds) + run(:PEXPIRE, key, milliseconds) + end + + # Set the expiration for a key as a UNIX timestamp specified in milliseconds + # @see http://redis.io/commands/pexpireat + # + # @param [String] key + # @param [Integer] timestamp in milliseconds + # + # @return [Integer] 1 if the timeout was set and 0 otherwise + def pexpireat(key, timestamp) + run(:PEXPIREAT, key, timestamp) + end + + # Get the time to live for a key in milliseconds + # @see http://redis.io/commands/pttl + # + # @param [String] key + # + # @return [Integer] TTL in milliseconds, or a negative value in order to + # signal an error + def pttl(key) + run(:PTTL, key) + end + + # Return a random key from the keyspace + # @see http://redis.io/commands/randomkey + # + # @return [String] the random key + # @return [nil] if database is empty + def randomkey + run(:RANDOMKEY) + end + + # Rename a key + # @see http://redis.io/commands/rename + # + # @param [String] key to rename + # @param [String] newkey + # + # @return [String] OK in case of success + # @return [RError] if key does not exist. Before Redis 3.2.0, an error is + # returned if source and destination names are the same. + def rename(key, newkey) + run(:RENAME, key, newkey) + end + + # Rename a key, only if the new key does not exist + # @see http://redis.io/commands/renamenx + # + # @param [String] key to rename + # @param [String] newkey + # + # @return [Integer] 1 if key was renamed to newkey. 0 if newkey already + # exists. + # @return [RError] if key does not exist. Before Redis 3.2.0, an error is + # returned if source and destination names are the same. + def renamenx(key, newkey) + run(:RENAMENX, key, newkey) + end + + # Create a key using the provided serialized value, previously obtained + # using DUMP + # @see http://redis.io/commands/restore + # + # @param [String] key + # @param [Integer] ttl expire time in milliseconds + # @param [String] serialized_value obtained using DUMP command + # @param [Hash] opts + # + # @option opts [Boolean] :replace (false) Override key if it already exists + # + # @return [String] OK on success + # @return [RError] if replace is false and key already exists or RDB version + # and data checksum don't match. + def restore(key, ttl, serialized_value, opts = {}) + args = [:RESTORE, key, ttl, serialized_value] + args << :REPLACE if opts[:replace] + + run(*args) + end + + # Get the time to live for a key + # @see http://redis.io/commands/ttl + # + # @param [String] key + # + # @return [Integer] TTL in seconds, or a negative value in order to signal + # an error + def ttl(key) + run(:TTL, key) + end + + # Determine the type stored at key + # @see http://redis.io/commands/type + # + # @param [String] key + # + # @return [String] type of key, or none when key does not exist. + def type(key) + run(:TYPE, key) + end + # ------------------ Sets ------------------------ # Add one or more members to a set # @see http://redis.io/commands/sadd # @@ -324,11 +502,25 @@ # @todo Support optional args (WITHSCORES/LIMIT) # # @param [String] key under which set is stored # @param [String] min value # @param [String] max value + # + # @return [Array] list of elements in the specified score range def zrangebyscore(key, min, max) run(:ZRANGEBYSCORE, key, min, max) + end + + # Remove one or more members from a sorted set + # @see http://redis.io/commands/zrem + # + # @param [String] key + # @param [Array<String>] members to delete + # + # @return [Integer] number of deleted members + # @return [RError] when key exists and does not hold a sorted set. + def zrem(key, *members) + run(*members.unshift(:ZREM, key)) end protected def serialize(*command)