# Module redis

This module implements a redis client. It allows you to connect to a redis-server instance, send commands and receive replies.

Beware: Most (if not all) functions that return a `TRedisString` may return `redisNil`, and functions which return a `TRedisList` may return `nil`.

# Imports

sockets, os, strutils, parseutils

# Types

    TRedis* {.pure, final.} = object
      socket: TSocket
      connected: bool

    TRedisStatus* = string

    TRedisInteger* = biggestInt

    TRedisString* = string

  Bulk reply

    TRedisList* = seq[TRedisString]

  Multi-bulk reply

    EInvalidReply* = object of ESynch

  Invalid reply from redis

    ERedis* = object of ESynch

  Error in redis

# Consts

    redisNil* = "\0\0"

# Procs

    proc open*(host = "localhost"; port = 6379.TPort): TRedis

  Opens a connection to the redis server.

    proc del*(r: TRedis; keys: varargs[string]): TRedisInteger

  Delete a key or multiple keys

    proc exists*(r: TRedis; key: string): bool

  Determine if a key exists

    proc expire*(r: TRedis; key: string; seconds: int): bool

  Set a key's time to live in seconds. Returns false if the key could not be found or the timeout could not be set.

    proc expireAt*(r: TRedis; key: string; timestamp: int): bool

  Set the expiration for a key as a UNIX timestamp. Returns false if the key could not be found or the timeout could not be set.

    proc keys*(r: TRedis; pattern: string): TRedisList

  Find all keys matching the given pattern

    proc move*(r: TRedis; key: string; db: int): bool

  Move a key to another database. Returns true on a successful move.

    proc persist*(r: TRedis; key: string): bool

  Remove the expiration from a key. Returns true when the timeout was removed.

    proc randomKey*(r: TRedis): TRedisString

  Return a random key from the keyspace

    proc rename*(r: TRedis; key, newkey: string): TRedisStatus

  Rename a key.

  WARNING: Overwrites newkey if it exists!

    proc renameNX*(r: TRedis; key, newkey: string): bool

  Same as `rename` but doesn't continue if newkey exists. Returns true if key was renamed.

    proc ttl*(r: TRedis; key: string): TRedisInteger

  Get the time to live for a key

    proc keyType*(r: TRedis; key: string): TRedisStatus

  Determine the type stored at key

    proc append*(r: TRedis; key, value: string): TRedisInteger

  Append a value to a key

    proc decr*(r: TRedis; key: string): TRedisInteger

  Decrement the integer value of a key by one

    proc decrBy*(r: TRedis; key: string; decrement: int): TRedisInteger

  Decrement the integer value of a key by the given number

    proc get*(r: TRedis; key: string): TRedisString

  Get the value of a key. Returns redisNil when key doesn't exist.

    proc getBit*(r: TRedis; key: string; offset: int): TRedisInteger

  Returns the bit value at offset in the string value stored at key

    proc getRange*(r: TRedis; key: string; start, stop: int): TRedisString

  Get a substring of the string stored at a key

    proc getSet*(r: TRedis; key: string; value: string): TRedisString

  Set the string value of a key and return its old value. Returns redisNil when key doesn't exist.

    proc incr*(r: TRedis; key: string): TRedisInteger

  Increment the integer value of a key by one.

    proc incrBy*(r: TRedis; key: string; increment: int): TRedisInteger

  Increment the integer value of a key by the given number

    proc setk*(r: TRedis; key, value: string)

  Set the string value of a key.

  NOTE: This function had to be renamed due to a clash with the set type.

    proc setNX*(r: TRedis; key, value: string): bool

  Set the value of a key, only if the key does not exist. Returns true if the key was set.

    proc setBit*(r: TRedis; key: string; offset: int; value: string): TRedisInteger

  Sets or clears the bit at offset in the string value stored at key

    proc setEx*(r: TRedis; key: string; seconds: int; value: string): TRedisStatus

  Set the value and expiration of a key

    proc setRange*(r: TRedis; key: string; offset: int; value: string): TRedisInteger

  Overwrite part of a string at key starting at the specified offset

    proc strlen*(r: TRedis; key: string): TRedisInteger

  Get the length of the value stored in a key. Returns 0 when key doesn't exist.

    proc hDel*(r: TRedis; key, field: string): bool

  Delete a hash field at key. Returns true if the field was removed.

    proc hExists*(r: TRedis; key, field: string): bool

  Determine if a hash field exists.

    proc hGet*(r: TRedis; key, field: string): TRedisString

  Get the value of a hash field

    proc hGetAll*(r: TRedis; key: string): TRedisList

  Get all the fields and values in a hash

    proc hIncrBy*(r: TRedis; key, field: string; incr: int): TRedisInteger

  Increment the integer value of a hash field by the given number

    proc hKeys*(r: TRedis; key: string): TRedisList

  Get all the fields in a hash

    proc hLen*(r: TRedis; key: string): TRedisInteger

  Get the number of fields in a hash

    proc hMGet*(r: TRedis; key: string; fields: varargs[string]): TRedisList

  Get the values of all the given hash fields

    proc hMSet*(r: TRedis; key: string;
                fieldValues: openarray[tuple[field, value: string]])

  Set multiple hash fields to multiple values

    proc hSet*(r: TRedis; key, field, value: string): TRedisInteger

  Set the string value of a hash field

    proc hSetNX*(r: TRedis; key, field, value: string): TRedisInteger

  Set the value of a hash field, only if the field does not exist

    proc hVals*(r: TRedis; key: string): TRedisList

  Get all the values in a hash

    proc bLPop*(r: TRedis; keys: varargs[string]; timeout: int): TRedisList

  Remove and get the first element in a list, or block until one is available

    proc bRPop*(r: TRedis; keys: varargs[string]; timeout: int): TRedisList

  Remove and get the last element in a list, or block until one is available.

    proc bRPopLPush*(r: TRedis; source, destination: string; timeout: int): TRedisString

  Pop a value from a list, push it to another list and return it; or block until one is available.

  http://redis.io/commands/brpoplpush

    proc lIndex*(r: TRedis; key: string; index: int): TRedisString

  Get an element from a list by its index

    proc lInsert*(r: TRedis; key: string; before: bool; pivot, value: string): TRedisInteger

  Insert an element before or after another element in a list

    proc lLen*(r: TRedis; key: string): TRedisInteger

  Get the length of a list

    proc lPop*(r: TRedis; key: string): TRedisString

  Remove and get the first element in a list

    proc lPush*(r: TRedis; key, value: string; create: bool = True): TRedisInteger

  Prepend a value to a list. Returns the length of the list after the push. The `create` param specifies whether a list should be created if it doesn't exist at `key`. More specifically if `create` is True, LPUSH will be used, otherwise LPUSHX.

    proc lRange*(r: TRedis; key: string; start, stop: int): TRedisList

  Get a range of elements from a list. Returns nil when key doesn't exist.

    proc lRem*(r: TRedis; key: string; value: string; count: int = 0): TRedisInteger

  Remove elements from a list. Returns the number of elements that have been removed.

    proc lSet*(r: TRedis; key: string; index: int; value: string)

  Set the value of an element in a list by its index

    proc lTrim*(r: TRedis; key: string; start, stop: int)

  Trim a list to the specified range

    proc rPop*(r: TRedis; key: string): TRedisString

  Remove and get the last element in a list

    proc rPopLPush*(r: TRedis; source, destination: string): TRedisString

  Remove the last element in a list, append it to another list and return it

    proc rPush*(r: TRedis; key, value: string; create: bool = True): TRedisInteger

  Append a value to a list. Returns the length of the list after the push. The `create` param specifies whether a list should be created if it doesn't exist at `key`. More specifically if `create` is True, RPUSH will be used, otherwise RPUSHX.

    proc sadd*(r: TRedis; key: string; member: string): TRedisInteger

  Add a member to a set

    proc scard*(r: TRedis; key: string): TRedisInteger

  Get the number of members in a set

    proc sdiff*(r: TRedis; keys: varargs[string]): TRedisList

  Subtract multiple sets

    proc sdiffstore*(r: TRedis; destination: string; keys: varargs[string]): TRedisInteger

  Subtract multiple sets and store the resulting set in a key

    proc sinter*(r: TRedis; keys: varargs[string]): TRedisList

  Intersect multiple sets

    proc sinterstore*(r: TRedis; destination: string; keys: varargs[string]): TRedisInteger

  Intersect multiple sets and store the resulting set in a key

    proc sismember*(r: TRedis; key: string; member: string): TRedisInteger

  Determine if a given value is a member of a set

    proc smembers*(r: TRedis; key: string): TRedisList

  Get all the members in a set

    proc smove*(r: TRedis; source: string; destination: string; member: string): TRedisInteger

  Move a member from one set to another

    proc spop*(r: TRedis; key: string): TRedisString

  Remove and return a random member from a set

    proc srandmember*(r: TRedis; key: string): TRedisString

  Get a random member from a set

    proc srem*(r: TRedis; key: string; member: string): TRedisInteger

  Remove a member from a set

    proc sunion*(r: TRedis; keys: varargs[string]): TRedisList

  Add multiple sets

    proc sunionstore*(r: TRedis; destination: string; key: varargs[string]): TRedisInteger

  Add multiple sets and store the resulting set in a key

    proc zadd*(r: TRedis; key: string; score: int; member: string): TRedisInteger

  Add a member to a sorted set, or update its score if it already exists

    proc zcard*(r: TRedis; key: string): TRedisInteger

  Get the number of members in a sorted set

    proc zcount*(r: TRedis; key: string; min: string; max: string): TRedisInteger

  Count the members in a sorted set with scores within the given values

    proc zincrby*(r: TRedis; key: string; increment: string; member: string): TRedisString

  Increment the score of a member in a sorted set

    proc zinterstore*(r: TRedis; destination: string; numkeys: string;
                      keys: openarray[string]; weights: openarray[string] = [];
                      aggregate: string = ""): TRedisInteger

  Intersect multiple sorted sets and store the resulting sorted set in a new key

    proc zrange*(r: TRedis; key: string; start: string; stop: string;
                 withScores: bool): TRedisList

  Return a range of members in a sorted set, by index

    proc zrangebyscore*(r: TRedis; key: string; min: string; max: string;
                        withScore: bool = false; limit: bool = False;
                        limitOffset: int = 0; limitCount: int = 0): TRedisList

  Return a range of members in a sorted set, by score

    proc zrank*(r: TRedis; key: string; member: string): TRedisString

  Determine the index of a member in a sorted set

    proc zrem*(r: TRedis; key: string; member: string): TRedisInteger

  Remove a member from a sorted set

    proc zremrangebyrank*(r: TRedis; key: string; start: string; stop: string): TRedisInteger

  Remove all members in a sorted set within the given indexes

    proc zremrangebyscore*(r: TRedis; key: string; min: string; max: string): TRedisInteger

  Remove all members in a sorted set within the given scores

    proc zrevrange*(r: TRedis; key: string; start: string; stop: string;
                    withScore: bool): TRedisList

  Return a range of members in a sorted set, by index, with scores ordered from high to low

    proc zrevrangebyscore*(r: TRedis; key: string; min: string; max: string;
                           withScore: bool = false; limit: bool = False;
                           limitOffset: int = 0; limitCount: int = 0): TRedisList

  Return a range of members in a sorted set, by score, with scores ordered from high to low

    proc zrevrank*(r: TRedis; key: string; member: string): TRedisString

  Determine the index of a member in a sorted set, with scores ordered from high to low

    proc zscore*(r: TRedis; key: string; member: string): TRedisString

  Get the score associated with the given member in a sorted set

    proc zunionstore*(r: TRedis; destination: string; numkeys: string;
                      keys: openarray[string]; weights: openarray[string] = [];
                      aggregate: string = ""): TRedisInteger

  Add multiple sorted sets and store the resulting sorted set in a new key

    proc discardMulti*(r: TRedis)

  Discard all commands issued after MULTI

    proc exec*(r: TRedis): TRedisList

  Execute all commands issued after MULTI

    proc multi*(r: TRedis)

  Mark the start of a transaction block

    proc unwatch*(r: TRedis)

  Forget about all watched keys

    proc watch*(r: TRedis; key: varargs[string])

  Watch the given keys to determine execution of the MULTI/EXEC block

    proc auth*(r: TRedis; password: string)

  Authenticate to the server

    proc echoServ*(r: TRedis; message: string): TRedisString

  Echo the given string

    proc ping*(r: TRedis): TRedisStatus

  Ping the server

    proc quit*(r: TRedis)

  Close the connection

    proc select*(r: TRedis; index: int): TRedisStatus

  Change the selected database for the current connection

    proc bgrewriteaof*(r: TRedis)

  Asynchronously rewrite the append-only file

    proc bgsave*(r: TRedis)

  Asynchronously save the dataset to disk

    proc configGet*(r: TRedis; parameter: string): TRedisList

  Get the value of a configuration parameter

    proc configSet*(r: TRedis; parameter: string; value: string)

  Set a configuration parameter to the given value

    proc configResetStat*(r: TRedis)

  Reset the stats returned by INFO

    proc dbsize*(r: TRedis): TRedisInteger

  Return the number of keys in the selected database

    proc debugObject*(r: TRedis; key: string): TRedisStatus

  Get debugging information about a key

    proc debugSegfault*(r: TRedis)

  Make the server crash

    proc flushall*(r: TRedis): TRedisStatus

  Remove all keys from all databases

    proc flushdb*(r: TRedis): TRedisStatus

  Remove all keys from the current database

    proc info*(r: TRedis): TRedisString

  Get information and statistics about the server

    proc lastsave*(r: TRedis): TRedisInteger

  Get the UNIX time stamp of the last successful save to disk

    proc save*(r: TRedis)

  Synchronously save the dataset to disk

    proc shutdown*(r: TRedis)

  Synchronously save the dataset to disk and then shut down the server

    proc slaveof*(r: TRedis; host: string; port: string)

  Make the server a slave of another instance, or promote it as master

# Iterators

    iterator hPairs*(r: TRedis; key: string): tuple[key, value: string]

  Iterator for keys and values in a hash.

Generated: 2012-09-23 21:47:54 UTC