Standard library · Database

db/redis

A Redis client: keys and values, hashes, lists, sets and publishing.

import db.redis

Redis is an in memory database for caches, sessions, counters and queues. This package implements the Redis protocol directly and with no outside library; the example needs a running Redis server, so it is only checked. For a command with no function of its own, call Command with the command name and its arguments.

Types

Client

struct Client
A connection to Redis.
NameTypeDescription
handlevoid*The connection pointer.
db-redis-Client.salam
import db.redis

func main:
    c := redis.Connect("127.0.0.1", 6379)
    if not redis.Ok(c):
        println redis.Error(c)
        ret
    end
    _ := redis.Auth(c, "s3cret")
    _ := redis.Select(c, 1)
    println redis.Ping(c)

    _ := redis.Set(c, "greeting", "hello")
    _ := redis.SetEx(c, "session:42", "token", 3600)
    println redis.Get(c, "greeting"), redis.Exists(c, "greeting"), redis.TTL(c, "session:42")
    println redis.Incr(c, "visits"), redis.IncrBy(c, "visits", 10), redis.Decr(c, "visits")
    println redis.Append(c, "greeting", "!"), redis.StrLen(c, "greeting"), redis.Type(c, "greeting")
    _ := redis.Expire(c, "greeting", 60)
    println redis.Keys(c, "session:*").len()
    _ := redis.Del(c, "greeting")

    _ := redis.HSet(c, "user:1", "name", "Maryam")
    _ := redis.HSet(c, "user:1", "city", "Tehran")
    println redis.HGet(c, "user:1", "name"), redis.HExists(c, "user:1", "age"), redis.HLen(c, "user:1")
    all := redis.HGetAll(c, "user:1")
    println all.size()
    _ := redis.HDel(c, "user:1", "city")

    _ := redis.RPush(c, "jobs", "a")
    _ := redis.LPush(c, "jobs", "b")
    println redis.LLen(c, "jobs"), redis.LRange(c, "jobs", 0, -1).len()
    println redis.LPop(c, "jobs"), redis.RPop(c, "jobs")

    _ := redis.SAdd(c, "tags", "go")
    _ := redis.SAdd(c, "tags", "salam")
    println redis.SIsMember(c, "tags", "salam"), redis.SCard(c, "tags"), redis.SMembers(c, "tags").len()
    _ := redis.SRem(c, "tags", "go")
    println redis.Publish(c, "news", "fresh news")

    mut args := Vector {} as Vector<str>
    args.push("GET")
    args.push("visits")
    r := redis.Command(c, args)
    if redis.ReplyOk(r) and not redis.ReplyIsError(r) and not redis.ReplyIsNil(r):
        println redis.ReplyType(r) == redis.REPLY_STRING, redis.ReplyStr(r)
    end
    redis.FreeReply(r)
    mut args2 := Vector {} as Vector<str>
    args2.push("LRANGE")
    args2.push("jobs")
    args2.push("0")
    args2.push("-1")
    r2 := redis.Command(c, args2)
    if redis.ReplyType(r2) == redis.REPLY_ARRAY:
        repeat redis.ReplyArrayLen(r2) in i:
            println redis.ReplyStr(redis.ReplyArrayGet(r2, i))
        end
    end
    println redis.ReplyInt(r2), redis.REPLY_INTEGER, redis.REPLY_NIL, redis.REPLY_STATUS, redis.REPLY_ERROR, redis.REPLY_DOUBLE
    redis.FreeReply(r2)
    _ := redis.FlushDB(c)
    redis.Close(c)
end

Reply

struct Reply
The reply to one command.
NameTypeDescription
handlevoid*The reply pointer.

The example for this item is shown with the ones above: db-redis-Client.salam

Constants and variables

REPLY_STRING

const REPLY_STRING: i32 = 1
Reply type: a string.

The example for this item is shown with the ones above: db-redis-Client.salam

REPLY_ARRAY

const REPLY_ARRAY: i32 = 2
Reply type: an array.

The example for this item is shown with the ones above: db-redis-Client.salam

REPLY_INTEGER

const REPLY_INTEGER: i32 = 3
Reply type: a number.

The example for this item is shown with the ones above: db-redis-Client.salam

REPLY_NIL

const REPLY_NIL: i32 = 4
Reply type: nil, a key that is not there for instance.

The example for this item is shown with the ones above: db-redis-Client.salam

REPLY_STATUS

const REPLY_STATUS: i32 = 5
Reply type: a status, such as OK.

The example for this item is shown with the ones above: db-redis-Client.salam

REPLY_ERROR

const REPLY_ERROR: i32 = 6
Reply type: an error.

The example for this item is shown with the ones above: db-redis-Client.salam

REPLY_DOUBLE

const REPLY_DOUBLE: i32 = 7
Reply type: a float.

The example for this item is shown with the ones above: db-redis-Client.salam

Functions

Connect

func Connect(host: str, port: int): Client
Connects to the Redis server.

The example for this item is shown with the ones above: db-redis-Client.salam

Ok

func Ok(c: Client): bool
The connection is up.

The example for this item is shown with the ones above: db-redis-Client.salam

Error

func Error(c: Client): str
The error message.

The example for this item is shown with the ones above: db-redis-Client.salam

Close

func Close(c: Client)
Closes the connection.

The example for this item is shown with the ones above: db-redis-Client.salam

Ping

func Ping(c: Client): bool
The connection is alive.

The example for this item is shown with the ones above: db-redis-Client.salam

Select

func Select(c: Client, index: int): bool
Picks the numbered database.

The example for this item is shown with the ones above: db-redis-Client.salam

Auth

func Auth(c: Client, password: str): bool
Authenticates with a password.

The example for this item is shown with the ones above: db-redis-Client.salam

FlushDB

func FlushDB(c: Client): bool
Clears every key of the current database.

The example for this item is shown with the ones above: db-redis-Client.salam

Set

func Set(c: Client, key: str, val: str): bool
Sets the value of a key.

The example for this item is shown with the ones above: db-redis-Client.salam

SetEx

func SetEx(c: Client, key: str, val: str, seconds: int): bool
The same, with an expiry in seconds.

The example for this item is shown with the ones above: db-redis-Client.salam

Get

func Get(c: Client, key: str): str
The value of a key; an empty string when there is none.

The example for this item is shown with the ones above: db-redis-Client.salam

Exists

func Exists(c: Client, key: str): bool
The key is there.

The example for this item is shown with the ones above: db-redis-Client.salam

Del

func Del(c: Client, key: str): bool
Deletes the key.

The example for this item is shown with the ones above: db-redis-Client.salam

Expire

func Expire(c: Client, key: str, seconds: int): bool
Sets the expiry of the key.

The example for this item is shown with the ones above: db-redis-Client.salam

TTL

func TTL(c: Client, key: str): i64
The seconds left until the expiry; -1 means no expiry and -2 means no such key.

The example for this item is shown with the ones above: db-redis-Client.salam

Incr

func Incr(c: Client, key: str): i64
Raises the number of a key by one and returns the new value.

The example for this item is shown with the ones above: db-redis-Client.salam

IncrBy

func IncrBy(c: Client, key: str, delta: i64): i64
Raises it by the given amount.

The example for this item is shown with the ones above: db-redis-Client.salam

Decr

func Decr(c: Client, key: str): i64
Lowers it by one.

The example for this item is shown with the ones above: db-redis-Client.salam

Append

func Append(c: Client, key: str, val: str): i64
Adds text to the value and returns the new length.

The example for this item is shown with the ones above: db-redis-Client.salam

StrLen

func StrLen(c: Client, key: str): i64
The length of the value.

The example for this item is shown with the ones above: db-redis-Client.salam

Type

func Type(c: Client, key: str): str
The type of the value of a key, such as string or hash.

The example for this item is shown with the ones above: db-redis-Client.salam

Keys

func Keys(c: Client, pattern: str): Vector<str>
The keys that match a pattern; slow on a large database.

The example for this item is shown with the ones above: db-redis-Client.salam

LPush

func LPush(c: Client, key: str, val: str): i64
Adds to the front of a list and returns the new length.

The example for this item is shown with the ones above: db-redis-Client.salam

RPush

func RPush(c: Client, key: str, val: str): i64
Adds to the end of a list.

The example for this item is shown with the ones above: db-redis-Client.salam

LPop

func LPop(c: Client, key: str): str
Takes one off the front of a list.

The example for this item is shown with the ones above: db-redis-Client.salam

RPop

func RPop(c: Client, key: str): str
Takes one off the end.

The example for this item is shown with the ones above: db-redis-Client.salam

LLen

func LLen(c: Client, key: str): i64
The length of the list.

The example for this item is shown with the ones above: db-redis-Client.salam

LRange

func LRange(c: Client, key: str, start: int, stop: int): Vector<str>
A part of the list; -1 means the last element.

The example for this item is shown with the ones above: db-redis-Client.salam

SAdd

func SAdd(c: Client, key: str, member: str): i64
Adds a member to a set.

The example for this item is shown with the ones above: db-redis-Client.salam

SRem

func SRem(c: Client, key: str, member: str): i64
Removes a member.

The example for this item is shown with the ones above: db-redis-Client.salam

SIsMember

func SIsMember(c: Client, key: str, member: str): bool
It is a member.

The example for this item is shown with the ones above: db-redis-Client.salam

SCard

func SCard(c: Client, key: str): i64
The number of members.

The example for this item is shown with the ones above: db-redis-Client.salam

SMembers

func SMembers(c: Client, key: str): Vector<str>
Every member.

The example for this item is shown with the ones above: db-redis-Client.salam

Publish

func Publish(c: Client, channel: str, message: str): i64
Publishes a message to a channel and returns the number of receivers.

The example for this item is shown with the ones above: db-redis-Client.salam

HSet

func HSet(c: Client, key: str, field: str, val: str): bool
Sets a field of a hash.

The example for this item is shown with the ones above: db-redis-Client.salam

HGet

func HGet(c: Client, key: str, field: str): str
One field of a hash.

The example for this item is shown with the ones above: db-redis-Client.salam

HDel

func HDel(c: Client, key: str, field: str): bool
Deletes a field.

The example for this item is shown with the ones above: db-redis-Client.salam

HExists

func HExists(c: Client, key: str, field: str): bool
The field is there.

The example for this item is shown with the ones above: db-redis-Client.salam

HLen

func HLen(c: Client, key: str): i64
The number of fields.

The example for this item is shown with the ones above: db-redis-Client.salam

HGetAll

func HGetAll(c: Client, key: str): HashMap<str, str>
Every field as a map.

The example for this item is shown with the ones above: db-redis-Client.salam

Command

func Command(c: Client, args: Vector<str>): Reply
Sends any command with its arguments; free the reply with FreeReply when you are done with it.

The example for this item is shown with the ones above: db-redis-Client.salam

ReplyOk

func ReplyOk(r: Reply): bool
A reply arrived.

The example for this item is shown with the ones above: db-redis-Client.salam

ReplyType

func ReplyType(r: Reply): int
The reply type.

The example for this item is shown with the ones above: db-redis-Client.salam

ReplyIsError

func ReplyIsError(r: Reply): bool
The reply is an error.

The example for this item is shown with the ones above: db-redis-Client.salam

ReplyIsNil

func ReplyIsNil(r: Reply): bool
The reply is nil.

The example for this item is shown with the ones above: db-redis-Client.salam

ReplyInt

func ReplyInt(r: Reply): i64
The reply as a number.

The example for this item is shown with the ones above: db-redis-Client.salam

ReplyStr

func ReplyStr(r: Reply): str
The reply as text.

The example for this item is shown with the ones above: db-redis-Client.salam

ReplyArrayLen

func ReplyArrayLen(r: Reply): int
The number of elements in an array reply.

The example for this item is shown with the ones above: db-redis-Client.salam

ReplyArrayGet

func ReplyArrayGet(r: Reply, i: int): Reply
One element of an array reply.

The example for this item is shown with the ones above: db-redis-Client.salam

FreeReply

func FreeReply(r: Reply)
Frees the reply.

The example for this item is shown with the ones above: db-redis-Client.salam