lib/oxblood/session.rb in oxblood-0.1.0.dev7 vs lib/oxblood/session.rb in oxblood-0.1.0.dev8
- old
+ new
@@ -185,20 +185,127 @@
#
# Strings
#
+ # Append a value to a key
+ # @see http://redis.io/commands/append
+ #
+ # @param [String] key
+ # @param [String] value
+ #
+ # @return [Integer] the length of the string after the append operation
+ def append(key, value)
+ run(:APPEND, key, value)
+ end
+
+ # Count set bits in a string
+ # @see http://redis.io/commands/bitcount
+ #
+ # @param [String] key
+ # @param [Array] interval to count in
+ #
+ # @return [Integer] the number of bits set to 1
+ def bitcount(key, *interval)
+ run(*interval.unshift(:BITCOUNT, key))
+ end
+
+ # Perform bitwise operations between strings
+ # @see http://redis.io/commands/bitop
+ #
+ # @param [String] operation
+ # @param [String] destkey
+ # @param [Array] keys
+ #
+ # @return [Integer] the size of the string stored in the destination key,
+ # that is equal to the size of the longest input string
+ def bitop(operation, destkey, *keys)
+ run(*keys.unshift(:BITOP, operation, destkey))
+ end
+
+ # Find first bit set or clear in a string
+ # @see http://redis.io/commands/bitpos
+ #
+ # @param [String] key
+ # @param [Integer] bit
+ # @param [Array] interval
+ #
+ # @return [Integer] the command returns the position of the first bit set to
+ # 1 or 0 according to the request
+ def bitpos(key, bit, *interval)
+ run(*interval.unshift(:BITPOS, key, bit))
+ end
+
+ # Decrement the integer value of a key by one
+ # @see http://redis.io/commands/decr
+ #
+ # @param [String] key
+ #
+ # @return [Integer] the value of key after the decrement
+ # @return [RError] if value is not an integer or out of range
+ def decr(key)
+ run(:DECR, key)
+ end
+
+ # Decrement the integer value of a key by the given number
+ # @see http://redis.io/commands/decrby
+ #
+ # @param [String] key
+ # @param [Integer] decrement
+ #
+ # @return [Integer] the value of key after the decrement
+ # @return [RError] if the key contains a value of the wrong type or contains
+ # a string that can not be represented as integer
+ def decrby(key, decrement)
+ run(:DECRBY, key, decrement)
+ end
+
# Get the value of a key
# @see http://redis.io/commands/get
#
# @param [String] key
#
# @return [String, nil] the value of key, or nil when key does not exists
def get(key)
run(:GET, key)
end
+ # Returns the bit value at offset in the string value stored at key
+ # @see http://redis.io/commands/getbit
+ #
+ # @param [String] key
+ # @param [Integer] offset
+ #
+ # @return [Integer] the bit value stored at offset
+ def getbit(key, offset)
+ run(:GETBIT, key, offset)
+ end
+
+ # Get a substring of the string stored at a key
+ # @see http://redis.io/commands/getrange
+ #
+ # @param [String] key
+ # @param [Integer] start_pos
+ # @param [Integer] end_pos
+ #
+ # @return [String] substring
+ def getrange(key, start_pos, end_pos)
+ run(:GETRANGE, key, start_pos, end_pos)
+ end
+
+ # Set the string value of a key and return its old value
+ # @see http://redis.io/commands/getset
+ #
+ # @param [String] key
+ # @param [String] value
+ #
+ # @return [String, nil] the old value stored at key, or nil when
+ # key did not exist
+ def getset(key, value)
+ run(:GETSET, key, value)
+ end
+
# Increment the integer value of a key by one
# @see http://redis.io/commands/incr
#
# @param [String] key
#
@@ -218,20 +325,64 @@
# @return [Integer] the value of key after the increment
def incrby(key, increment)
run(:INCRBY, key, increment)
end
+ # Increment the float value of a key by the given amount
+ # @see http://redis.io/commands/incrbyfloat
+ #
+ # @param [String] key
+ # @param [Float] increment
+ #
+ # @return [String] the value of key after the increment
+ def incrbyfloat(key, increment)
+ run(:INCRBYFLOAT, key, increment)
+ end
+
# Get the values of all the given keys
# @see http://redis.io/commands/mget
#
# @param [Array<String>] keys to retrieve
#
# @return [Array] list of values at the specified keys
def mget(*keys)
run(*keys.unshift(:MGET))
end
+ # Set multiple keys to multiple values
+ # @see http://redis.io/commands/mset
+ #
+ # @param [Array] keys_and_values
+ #
+ # @return [String] 'OK'
+ def mset(*keys_and_values)
+ run(*keys_and_values.unshift(:MSET))
+ end
+
+ # Set multiple keys to multiple values, only if none of the keys exist
+ # @see http://redis.io/commands/msetnx
+ #
+ # @param [Array] keys_and_values
+ #
+ # @return [Integer] 1 if the all the keys were set, or
+ # 0 if no key was set (at least one key already existed)
+ def msetnx(*keys_and_values)
+ run(*keys_and_values.unshift(:MSETNX))
+ end
+
+ # Set the value and expiration in milliseconds of a key
+ # @see http://redis.io/commands/psetex
+ #
+ # @param [String] key
+ # @param [Integer] milliseconds expire time
+ # @param [String] value
+ #
+ # @return [String] 'OK'
+ def psetex(key, milliseconds, value)
+ run(:PSETEX, key, milliseconds, value)
+ end
+
# Set the string value of a key
# @see http://redis.io/commands/set
#
# @todo Add support for set options
# http://redis.io/commands/set#options
@@ -242,11 +393,70 @@
# @return [String] 'OK' if SET was executed correctly
def set(key, value)
run(:SET, key, value)
end
+ # Set or clear the bit at offset in the string value stored at key
+ # @see http://redis.io/commands/setbit
#
+ # @param [String] key
+ # @param [Integer] offset
+ # @param [String] value
+ #
+ # @return [Integer] the original bit value stored at offset
+ def setbit(key, offset, value)
+ run(:SETBIT, key, offset, value)
+ end
+
+ # Set the value and expiration of a key
+ # @see http://redis.io/commands/setex
+ #
+ # @param [String] key
+ # @param [Integer] seconds expire time
+ # @param [String] value
+ #
+ # @return [String] 'OK'
+ def setex(key, seconds, value)
+ run(:SETEX, key, seconds, value)
+ end
+
+ # Set the value of a key, only if the key does not exist
+ # @see http://redis.io/commands/setnx
+ #
+ # @param [String] key
+ # @param [String] value
+ #
+ # @return [Integer] 1 if the key was set, or 0 if the key was not set
+ def setnx(key, value)
+ run(:SETNX, key, value)
+ end
+
+ # Overwrite part of a string at key starting at the specified offset
+ # @see http://redis.io/commands/setrange
+ #
+ # @param [String] key
+ # @param [Integer] offset
+ # @param [String] value
+ #
+ # @return [Integer] the length of the string after it was modified by
+ # the command
+ def setrange(key, offset, value)
+ run(:SETRANGE, key, offset, value)
+ end
+
+ # Get the length of the value stored in a key
+ # @see http://redis.io/commands/strlen
+ #
+ # @param [String] key
+ #
+ # @return [Integer] the length of the string at key,
+ # or 0 when key does not exist
+ def strlen(key)
+ run(:STRLEN, key)
+ end
+
+ #
# Connection
#
# Authenticate to the server
# @see http://redis.io/commands/auth
@@ -302,11 +512,21 @@
# @return [RError] if wrong index was passed
def select(index)
run(:SELECT, index)
end
+ # Close the connection
+ # @see http://redis.io/commands/quit
#
+ # @return [String] 'OK'
+ def quit
+ run(:QUIT)
+ ensure
+ @connection.socket.close
+ end
+
+ #
# Server
#
# Remove all keys from the current database
# @see http://redis.io/commands/flushdb
@@ -541,10 +761,35 @@
#
# Lists
#
+ # Get an element from a list by its index
+ # @see http://www.redis.io/commands/lindex
+ #
+ # @param [String] key
+ # @param [Integer] index zero-based of element in the list
+ #
+ # @return [String] the requested element, or nil when index is out of range.
+ def lindex(key, index)
+ run(:LINDEX, key, index)
+ end
+
+ # Insert an element before or after another element in a list
+ # @see http://www.redis.io/commands/linsert
+ #
+ # @param [String] key
+ # @param [Symbol] place could be :before or :after
+ # @param [String] pivot reference value
+ # @param [String] value to insert
+ #
+ # @return [Integer] the length of the list after the insert operation,
+ # or -1 when the value pivot was not found
+ def linsert(key, place, pivot, value)
+ run(:LINSERT, key, place, pivot, value)
+ end
+
# Get the length of a list
# @see http://redis.io/commands/llen
#
# @param [String] key
#
@@ -574,10 +819,21 @@
# @return [Integer] the length of the list after the push operations
def lpush(key, *values)
run(*values.unshift(:LPUSH, key))
end
+ # Prepend a value to a list, only if the list exists
+ # @see http://www.redis.io/commands/lpushx
+ #
+ # @param [String] key
+ # @param [String] value
+ #
+ # @return [Integer] the length of the list after the push operation
+ def lpushx(key, value)
+ run(:LPUSHX, key, value)
+ end
+
# Get a range of elements from a list
# @see http://redis.io/commands/lrange
#
# @param [String] key
# @param [Integer] start index
@@ -586,10 +842,47 @@
# @return [Array] list of elements in the specified range
def lrange(key, start, stop)
run(:LRANGE, key, start, stop)
end
+ # Remove elements from a list
+ # @see http://www.redis.io/commands/lrem
+ #
+ # @param [String] key
+ # @param [Integer] count (please look into official docs for more info)
+ # @param [String] value to remove
+ #
+ # @return [Integer] the number of removed elements
+ def lrem(key, count, value)
+ run(:LREM, key, count, value)
+ end
+
+ # Set the value of an element in a list by its index
+ # @see http://www.redis.io/commands/lset
+ #
+ # @param [String] key
+ # @param [Integer] index
+ # @param [String] value
+ #
+ # @return [String] 'OK'
+ # @return [RError] if index is out of range
+ def lset(key, index, value)
+ run(:LSET, key, index, value)
+ end
+
+ # Trim a list to the specified range
+ # @see http://www.redis.io/commands/ltrim
+ #
+ # @param [String] key
+ # @param [Integer] start
+ # @param [Integer] stop
+ #
+ # @return [String] 'OK'
+ def ltrim(key, start, stop)
+ run(:LTRIM, key, start, stop)
+ end
+
# Remove and get the last element in a list
# @see http://redis.io/commands/rpop
#
# @param [String] key
#
@@ -597,10 +890,22 @@
# not exist
def rpop(key)
run(:RPOP, key)
end
+ # Remove the last element in a list, prepend it to another list and return
+ # @see http://www.redis.io/commands/rpoplpush
+ #
+ # @param [String] source
+ # @param [String] destination
+ #
+ # @return [String, nil] the element being popped and pushed, or nil when
+ # source does not exist
+ def rpoplpush(source, destination)
+ run(:RPOPLPUSH, source, destination)
+ end
+
# Append one or multiple values to a list
# @see http://redis.io/commands/rpush
#
# @param [String] key
# @param [Array] values to add
@@ -609,11 +914,22 @@
# @return [RError] if key holds a value that is not a list
def rpush(key, *values)
run(*values.unshift(:RPUSH, key))
end
+ # Append a value to a list, only if the list exists
+ # @see http://www.redis.io/commands/rpushx
#
+ # @param [String] key
+ # @param [String] value
+ #
+ # @return [Integer] the length of the list after the push operation
+ def rpushx(key, value)
+ run(:RPUSHX, key, value)
+ end
+
+ #
# Sets
#
# Add one or more members to a set
# @see http://redis.io/commands/sadd
@@ -636,20 +952,121 @@
# key does not exist
def scard(key)
run(:SCARD, key)
end
+ # Subtract multiple sets
+ # @see http://redis.io/commands/sdiff
+ #
+ # @param [String, Array<String>] keys
+ #
+ # @return [Array] array with members of the resulting set
+ def sdiff(*keys)
+ run(*keys.unshift(:SDIFF))
+ end
+
+ # Subtract multiple sets and store the resulting set in a key
+ # @see http://redis.io/commands/sdiffstore
+ #
+ # @param [String] destination key
+ # @param [String, Array<String>] keys of sets to diff
+ #
+ # @return [Integer] the number of elements in the resulting set
+ def sdiffstore(destination, *keys)
+ run(*keys.unshift(:SDIFFSTORE, destination))
+ end
+
+ # Intersect multiple sets
+ # @see http://redis.io/commands/sinter
+ #
+ # @param [String, Array<String>] keys to intersect
+ #
+ # @return [Array] array with members of the resulting set
+ def sinter(*keys)
+ run(*keys.unshift(:SINTER))
+ end
+
+ # Intersect multiple sets and store the resulting key in a key
+ # @see http://redis.io/commands/sinterstore
+ #
+ # @param [String] destination key
+ # @param [String, Array<String>] keys of sets to intersect
+ #
+ # @return [Integer] the number of elements in the resulting set
+ def sinterstore(destination, *keys)
+ run(*keys.unshift(:SINTERSTORE, destination))
+ end
+
+ # Determine if a given value is a member of a set
+ # @see http://redis.io/commands/sismember
+ #
+ # @param [String] key
+ # @param [String] member
+ #
+ # @return [Integer] 1 if the element is a member of the set or
+ # 0 if the element is not a member of the set, or if key does not exist
+ def sismember(key, member)
+ run(:SISMEMBER, key, member)
+ end
+
# Get all the members in a set
# @see http://redis.io/commands/smembers
#
# @param [String] key
#
# @return [Array] all elements of the set
def smembers(key)
run(:SMEMBERS, key)
end
+ # Move a member from one set to another
+ # @see http://redis.io/commands/smove
+ #
+ # @param [String] source
+ # @param [String] destination
+ # @param [String] member
+ #
+ # @return [Integer] 1 if the element is moved, or 0 if the element is not
+ # a member of source and no operation was performed
+ def smove(source, destination, member)
+ run(:SMOVE, source, destination, member)
+ end
+
+ # Remove and return one or multiple random members from a set
+ # @see http://redis.io/commands/spop
+ #
+ # @param [String] key
+ # @param [Integer] count
+ #
+ # @return [String] without the additional count argument the command returns
+ # the removed element, or nil when key does not exist
+ # @return [Array] when the additional count argument is passed the command
+ # returns an array of removed elements, or an empty array when key does
+ # not exist.
+ def spop(key, count = nil)
+ args = [:SPOP, key]
+ args << count if count
+ run(*args)
+ end
+
+ # Get one or multiple random members from a set
+ # @see http://redis.io/commands/srandmember
+ #
+ # @param [String] key
+ # @param [Integer] count
+ #
+ # @return [String, nil] without the additional count argument the command
+ # returns string with the randomly selected element, or nil when key
+ # does not exist
+ # @return [Array] when the additional count argument is passed the command
+ # returns an array of elements, or an empty array when key does not exist
+ def srandmember(key, count = nil)
+ args = [:SRANDMEMBER, key]
+ args << count if count
+ run(*args)
+ end
+
# Remove one or more members from a set
# @see http://redis.io/commands/srem
#
# @param [String] key
# @param [Array] members to remove
@@ -668,11 +1085,22 @@
# @return [Array] list with members of the resulting set
def sunion(*keys)
run(*keys.unshift(:SUNION))
end
+ # Add multipe sets and store the resulting set in a key
+ # @see http://redis.io/commands/sunionstore
#
+ # @param [String] destination
+ # @param [String, Array<String>] keys
+ #
+ # @return [Integer] the number of elements in the resulting set
+ def sunionstore(destination, *keys)
+ run(*keys.unshift(:SUNIONSTORE, destination))
+ end
+
+ #
# Sorted Sets
#
# Add one or more members to a sorted set, or update its score if it already
# exists.
@@ -699,10 +1127,48 @@
# or 0 if key does not exists
def zcard(key)
run(:ZCARD, key)
end
+ # Count the members in a sorted set with scores within the given values
+ # @see http://redis.io/commands/zcount
+ #
+ # @param [String] key
+ # @param [String] min
+ # @param [String] max
+ #
+ # @return [Integer] the number of elements in the specified score range
+ def zcount(key, min, max)
+ run(:ZCOUNT, key, min, max)
+ end
+
+ # Increment the score of a member in a sorted set
+ # @see http://redis.io/commands/zincrby
+ #
+ # @param [String] key
+ # @param [Float] increment
+ # @param [String] member
+ #
+ # @return [String] the new score of member (a double precision floating
+ # point number), represented as string
+ def zincrby(key, increment, member)
+ run(:ZINCRBY, key, increment, member)
+ end
+
+ # Count the number of members in a sorted set between a given
+ # lexicographical range
+ # @see http://redis.io/commands/zlexcount
+ #
+ # @param [String] key
+ # @param [String] min
+ # @param [String] max
+ #
+ # @return the number of elements in the specified score range
+ def zlexcount(key, min, max)
+ run(:ZLEXCOUNT, key, min, max)
+ end
+
# Return a range of members in a sorted set, by index
# @see http://redis.io/commands/zrange
#
# @example
# session.zrange('myzset', 0, -1)
@@ -729,21 +1195,55 @@
end
# Return a range of members in a sorted set, by score
# @see http://redis.io/commands/zrangebyscore
#
- # @todo Support optional args (WITHSCORES/LIMIT)
- #
# @param [String] key under which set is stored
# @param [String] min score
# @param [String] max score
+ # @param [Hash] opts
#
- # @return [Array] list of elements in the specified score range
- def zrangebyscore(key, min, max)
- run(:ZRANGEBYSCORE, key, min, max)
+ # @option opts [Boolean] :withscores (false) Return the scores of
+ # the elements together with the elements
+ # @option opts [Array<Integer, Integer>] :limit Get a range of the matching
+ # elements (similar to SELECT LIMIT offset, count in SQL)
+ #
+ # @example
+ # session.zrangebyscore('myzset', '-inf', '+inf')
+ # # => ['one', 'two', 'three']
+ #
+ # @example
+ # session.zrangebyscore('myzset', '(1', 2, withscores: true)
+ # # => [['two', '2']]
+ #
+ # @example
+ # opts = { withscores: true, limit: [1, 1] }
+ # session.zrangebyscore('myzset', '-inf', '+inf', opts)
+ # # => [['two', '2']]
+ #
+ # @return [Array] list of elements in the specified score range (optionally
+ # with their scores, in case the :withscores option is given)
+ def zrangebyscore(key, min, max, opts = {})
+ args = [:ZRANGEBYSCORE, key, min, max]
+ args << :WITHSCORES if opts[:withscores]
+ args.push(:LIMIT).concat(opts[:limit]) if opts[:limit].is_a?(Array)
+
+ run(*args)
end
+ # Determine the index of a member in a sorted set
+ # @see http://redis.io/commands/zrank
+ #
+ # @param [String] key
+ # @param [String] member
+ #
+ # @return [Integer, nil] the rank of member or nil if member does not exist
+ # in the sorted set or key does not exist
+ def zrank(key, member)
+ run(:ZRANK, key, member)
+ 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
@@ -752,19 +1252,122 @@
# @return [RError] when key exists and does not hold a sorted set.
def zrem(key, *members)
run(*members.unshift(:ZREM, key))
end
+ # Remove all members in a sorted set within the given indexes
+ # @see http://redis.io/commands/zremrangebyrank
+ #
+ # @param [String] key
+ # @param [String] start
+ # @param [String] stop
+ #
+ # @return [Integer] the number of elements removed
+ def zremrangebyrank(key, start, stop)
+ run(:ZREMRANGEBYRANK, key, start, stop)
+ end
+
# Remove all members in a sorted set within the given scores
# @see http://redis.io/commands/zremrangebyscore
#
# @param [String] key
# @param [String] min score
# @param [String] max score
#
# @return [Integer] the number of elements removed
def zremrangebyscore(key, min, max)
run(:ZREMRANGEBYSCORE, key, min, max)
+ end
+
+ # Return a range of members in a sorted set, by index, with scores ordered
+ # from high to low
+ # @see http://redis.io/commands/zrevrange
+ #
+ # @example
+ # session.zrevrange('myzset', 0, -1)
+ # # => ['two', 'one']
+ #
+ # @example
+ # session.zrevrange('myzset', 0, -1, withscores: true)
+ # # => [['two', '2'], ['one', '1']]
+ #
+ # @param [String] key
+ # @param [Integer] start index
+ # @param [Integer] stop index
+ # @param [Hash] opts
+ #
+ # @option opts [Boolean] :withscores (false) Return the scores of
+ # the elements together with the elements
+ #
+ # @return [Array] list of elements in the specified range (optionally with
+ # their scores, in case the :withscores option is given)
+ def zrevrange(key, start, stop, opts = {})
+ args = [:ZREVRANGE, key, start, stop]
+ args << :WITHSCORES if opts[:withscores]
+ run(*args)
+ end
+
+ # Return a range of members in a sorted set, by score, with scores ordered
+ # from high to low
+ # @see http://redis.io/commands/zrevrangebyscore
+ #
+ # @param [String] key under which set is stored
+ # @param [String] min score
+ # @param [String] max score
+ # @param [Hash] opts
+ #
+ # @option opts [Boolean] :withscores (false) Return the scores of
+ # the elements together with the elements
+ # @option opts [Array<Integer, Integer>] :limit Get a range of the matching
+ # elements (similar to SELECT LIMIT offset, count in SQL)
+ #
+ # @example
+ # session.zrevrangebyscore('myzset', '+inf', '-inf')
+ # # => ['three', 'two', 'one']
+ #
+ # @example
+ # session.zrevrangebyscore('myzset', 2, '(1', withscores: true)
+ # # => [['two', '2']]
+ #
+ # @example
+ # opts = { withscores: true, limit: [1, 1] }
+ # session.zrevrangebyscore('myzset', '+inf', '-inf', opts)
+ # # => [['two', '2']]
+ #
+ # @return [Array] list of elements in the specified score range (optionally
+ # with their scores, in case the :withscores option is given)
+ def zrevrangebyscore(key, min, max, opts = {})
+ args = [:ZREVRANGEBYSCORE, key, min, max]
+ args << :WITHSCORES if opts[:withscores]
+ args.push(:LIMIT).concat(opts[:limit]) if opts[:limit].is_a?(Array)
+
+ run(*args)
+ end
+
+ # Determine the index of a member in a sorted set, with scores ordered from
+ # high to low
+ # @see http://redis.io/commands/zrevrank
+ #
+ # @param [String] key
+ # @param [String] member
+ #
+ # @return [Integer, nil] the rank of member, or nil if member does not
+ # exists in the sorted set or key does not exists
+ def zrevrank(key, member)
+ run(:ZREVRANK, key, member)
+ end
+
+ # Get the score associated with the given member in a sorted set
+ # @see http://redis.io/commands/zscore
+ #
+ # @param [String] key
+ # @param [String] member
+ #
+ # @return [String, nil] the score of member (a double precision floating
+ # point number), represented as string, or nil if member does not exist in
+ # the sorted set, or key does not exists
+ def zscore(key, member)
+ run(:ZSCORE, key, member)
end
protected
def serialize(*command)