- # RENAME oldkey newkey
- #
- # Atomically renames the key oldkey to newkey. If the source and destination
- # name are the same an error is returned. If newkey already exists it is
- # overwritten.
- #
- # Return value: status code reply
- def rename!(oldkey, newkey)
- timeout_retry(3, 3){
- write "RENAME #{oldkey} #{newkey}\r\n"
- status_code_reply
- }
- end
-
- # RENAMENX oldkey newkey
- # Just like RENAME but fails if the destination key newkey already exists.
- #
- # *Return value: integer reply, specifically:
- #
- # 1 if the key was renamed 0 if the target key already exist -1 if the
- # source key does not exist -3 if source and destination keys are the same
- def rename(oldkey, newkey)
- timeout_retry(3, 3){
- write "RENAMENX #{oldkey} #{newkey}\r\n"
- case integer_reply
- when -1
- raise RedisError, "source key: #{oldkey} does not exist"
- when 0
- raise RedisError, "target key: #{oldkey} already exists"
- when -3
- raise RedisError, "source and destination keys are the same"
- when 1
- true
- end
- }
- end
-
- # EXISTS key
- # Time complexity: O(1)
- # Test if the specified key exists. The command returns "0" if the key
- # exists, otherwise "1" is returned. Note that even keys set with an empty
- # string as value will return "1".
- #
- # *Return value: integer reply, specifically:
- #
- # 1 if the key exists 0 if the key does not exist
- def key?(key)
- timeout_retry(3, 3){
- write "EXISTS #{key}\r\n"
- integer_reply == 1
- }
- end
-
- # DEL key
- # Time complexity: O(1)
- # Remove the specified key. If the key does not exist no operation is
- # performed. The command always returns success.
- #
- # *Return value: integer reply, specifically:
- #
- # 1 if the key was removed 0 if the key does not exist
- def delete(key)
- timeout_retry(3, 3){
- write "DEL #{key}\r\n"
- integer_reply == 1
- }
- end
-
- # KEYS pattern
- # Time complexity: O(n) (with n being the number of keys in the DB)
- # Returns all the keys matching the glob-style pattern as space separated strings.
- # For example if you have in the database the keys "foo" and "foobar" the command
- # "KEYS foo*" will return "foo foobar".
- #
- # Note that while the time complexity for this operation is O(n) the constant times
- # are pretty low. For example Redis running on an entry level laptop can scan a 1
- # million keys database in 40 milliseconds. Still it's better to consider this one
- # of the slow commands that may ruin the DB performance if not used with care.
- #
- # Return value: bulk reply
- def keys(glob)
- timeout_retry(3, 3){
- write "KEYS #{glob}\r\n"
- bulk_reply.split(' ')
- }
- end
-
- # TYPE key
- #
- # Time complexity: O(1) Return the type of the value stored at key in form of
- # a string. The type can be one of "none", "string", "list", "set". "none" is
- # returned if the key does not exist.
- #
- # Return value: single line reply
- def type?(key)
- timeout_retry(3, 3){
- write "TYPE #{key}\r\n"
- single_line_reply
- }
- end
-
- # RPUSH key string
- #
- # Time complexity: O(1)
- # Add the given string to the tail of the list contained at key. If the key
- # does not exist an empty list is created just before the append operation.
- # If the key exists but is not a List an error is returned.
- #
- # Return value: status code reply
- def push_tail(key, string)
- timeout_retry(3, 3){
- write "RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
- status_code_reply
- }
- end
-
- # LPUSH key string
- # Time complexity: O(1)
- # Add the given string to the head of the list contained at key. If the
- # key does not exist an empty list is created just before the append operation.
- # If the key exists but is not a List an error is returned.
- #
- # Return value: status code reply
- def push_head(key, string)
- timeout_retry(3, 3){
- write "LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
- status_code_reply
- }
- end
-
- # LPOP key
- #
- # Time complexity: O(1)
- # Atomically return and remove the first element of the list. For example if
- # the list contains the elements "a","b","c" LPOP will return "a" and the
- # list will become "b","c".
- #
- # If the key does not exist or the list is already empty the special value
- # 'nil' is returned.
- #
- # Return value: bulk reply
- def pop_head(key)
- timeout_retry(3, 3){
- write "LPOP #{key}\r\n"
- bulk_reply
- }
- end
-
- # RPOP key
- # This command works exactly like LPOP, but the last element instead
- # of the first element of the list is returned/deleted.
- def pop_tail(key)
- timeout_retry(3, 3){
- write "RPOP #{key}\r\n"
- bulk_reply
- }
- end
-
- # LSET key index value
- # Time complexity: O(N) (with N being the length of the list)
- # Set the list element at index (see LINDEX for information about the index argument) with the new value. Out of range indexes will generate an error. Note that setting the first or last elements of the list is O(1).
- #
- # Return value: status code reply
- def list_set(key, index, val)
- timeout_retry(3, 3){
- write "LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n"
- status_code_reply
- }
- end
-
-
- # LLEN key
- # Time complexity: O(1)
- # Return the length of the list stored at the specified key. If the key does not
- # exist zero is returned (the same behaviour as for empty lists). If the value
- # stored at key is not a list the special value -1 is returned. Note: client
- # library should raise an exception when -1 is returned instead to pass the
- # value back to the caller like a normal list length value.
- #
- # *Return value: integer reply, specifically:
- #
- # the length of the list as an integer
- # >=
- # 0 if the operation succeeded -2 if the specified key does not hold a list valu
- def list_length(key)
- timeout_retry(3, 3){
- write "LLEN #{key}\r\n"
- case i = integer_reply
- when -2
- raise RedisError, "key: #{key} does not hold a list value"
- else
- i
- end
- }
- end
-
- # LRANGE key start end
- # Time complexity: O(n) (with n being the length of the range)
- # Return the specified elements of the list stored at the specified key. Start
- # and end are zero-based indexes. 0 is the first element of the list (the list head),
- # 1 the next element and so on.
- #
- # For example LRANGE foobar 0 2 will return the first three elements of the list.
- #
- # start and end can also be negative numbers indicating offsets from the end of the list.
- # For example -1 is the last element of the list, -2 the penultimate element and so on.
- #
- # Indexes out of range will not produce an error: if start is over the end of the list,
- # or start > end, an empty list is returned. If end is over the end of the list Redis
- # will threat it just like the last element of the list.
- #
- # Return value: multi bulk reply
- def list_range(key, start, ending)
- timeout_retry(3, 3){
- write "LRANGE #{key} #{start} #{ending}\r\n"
- multi_bulk_reply
- }
- end