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| Name | Type | Description |
|---|---|---|
handle | void* | The connection pointer. |
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)
endReply
struct Reply| Name | Type | Description |
|---|---|---|
handle | void* | 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 = 1The example for this item is shown with the ones above: db-redis-Client.salam
REPLY_ARRAY
const REPLY_ARRAY: i32 = 2The example for this item is shown with the ones above: db-redis-Client.salam
REPLY_INTEGER
const REPLY_INTEGER: i32 = 3The example for this item is shown with the ones above: db-redis-Client.salam
REPLY_NIL
const REPLY_NIL: i32 = 4The example for this item is shown with the ones above: db-redis-Client.salam
REPLY_STATUS
const REPLY_STATUS: i32 = 5OK.The example for this item is shown with the ones above: db-redis-Client.salam
REPLY_ERROR
const REPLY_ERROR: i32 = 6The example for this item is shown with the ones above: db-redis-Client.salam
REPLY_DOUBLE
const REPLY_DOUBLE: i32 = 7The example for this item is shown with the ones above: db-redis-Client.salam
Functions
Connect
func Connect(host: str, port: int): ClientThe example for this item is shown with the ones above: db-redis-Client.salam
Ok
func Ok(c: Client): boolThe example for this item is shown with the ones above: db-redis-Client.salam
Error
func Error(c: Client): strThe example for this item is shown with the ones above: db-redis-Client.salam
Close
func Close(c: Client)The example for this item is shown with the ones above: db-redis-Client.salam
Ping
func Ping(c: Client): boolThe example for this item is shown with the ones above: db-redis-Client.salam
Select
func Select(c: Client, index: int): boolThe example for this item is shown with the ones above: db-redis-Client.salam
Auth
func Auth(c: Client, password: str): boolThe example for this item is shown with the ones above: db-redis-Client.salam
FlushDB
func FlushDB(c: Client): boolThe example for this item is shown with the ones above: db-redis-Client.salam
Set
func Set(c: Client, key: str, val: str): boolThe 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): boolThe example for this item is shown with the ones above: db-redis-Client.salam
Get
func Get(c: Client, key: str): strThe example for this item is shown with the ones above: db-redis-Client.salam
Exists
func Exists(c: Client, key: str): boolThe example for this item is shown with the ones above: db-redis-Client.salam
Del
func Del(c: Client, key: str): boolThe example for this item is shown with the ones above: db-redis-Client.salam
Expire
func Expire(c: Client, key: str, seconds: int): boolThe example for this item is shown with the ones above: db-redis-Client.salam
TTL
func TTL(c: Client, key: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
Incr
func Incr(c: Client, key: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
IncrBy
func IncrBy(c: Client, key: str, delta: i64): i64The example for this item is shown with the ones above: db-redis-Client.salam
Decr
func Decr(c: Client, key: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
Append
func Append(c: Client, key: str, val: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
StrLen
func StrLen(c: Client, key: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
Type
func Type(c: Client, key: str): strstring 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 example for this item is shown with the ones above: db-redis-Client.salam
LPush
func LPush(c: Client, key: str, val: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
RPush
func RPush(c: Client, key: str, val: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
LPop
func LPop(c: Client, key: str): strThe example for this item is shown with the ones above: db-redis-Client.salam
RPop
func RPop(c: Client, key: str): strThe example for this item is shown with the ones above: db-redis-Client.salam
LLen
func LLen(c: Client, key: str): i64The 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>The example for this item is shown with the ones above: db-redis-Client.salam
SAdd
func SAdd(c: Client, key: str, member: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
SRem
func SRem(c: Client, key: str, member: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
SIsMember
func SIsMember(c: Client, key: str, member: str): boolThe example for this item is shown with the ones above: db-redis-Client.salam
SCard
func SCard(c: Client, key: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
SMembers
func SMembers(c: Client, key: str): Vector<str>The example for this item is shown with the ones above: db-redis-Client.salam
Publish
func Publish(c: Client, channel: str, message: str): i64The 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): boolThe example for this item is shown with the ones above: db-redis-Client.salam
HGet
func HGet(c: Client, key: str, field: str): strThe example for this item is shown with the ones above: db-redis-Client.salam
HDel
func HDel(c: Client, key: str, field: str): boolThe example for this item is shown with the ones above: db-redis-Client.salam
HExists
func HExists(c: Client, key: str, field: str): boolThe example for this item is shown with the ones above: db-redis-Client.salam
HLen
func HLen(c: Client, key: str): i64The example for this item is shown with the ones above: db-redis-Client.salam
HGetAll
func HGetAll(c: Client, key: str): HashMap<str, str>The example for this item is shown with the ones above: db-redis-Client.salam
Command
func Command(c: Client, args: Vector<str>): ReplyFreeReply 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): boolThe example for this item is shown with the ones above: db-redis-Client.salam
ReplyType
func ReplyType(r: Reply): intThe example for this item is shown with the ones above: db-redis-Client.salam
ReplyIsError
func ReplyIsError(r: Reply): boolThe example for this item is shown with the ones above: db-redis-Client.salam
ReplyIsNil
func ReplyIsNil(r: Reply): boolThe example for this item is shown with the ones above: db-redis-Client.salam
ReplyInt
func ReplyInt(r: Reply): i64The example for this item is shown with the ones above: db-redis-Client.salam
ReplyStr
func ReplyStr(r: Reply): strThe example for this item is shown with the ones above: db-redis-Client.salam
ReplyArrayLen
func ReplyArrayLen(r: Reply): intThe example for this item is shown with the ones above: db-redis-Client.salam
ReplyArrayGet
func ReplyArrayGet(r: Reply, i: int): ReplyThe example for this item is shown with the ones above: db-redis-Client.salam
FreeReply
func FreeReply(r: Reply)The example for this item is shown with the ones above: db-redis-Client.salam