Skip to content

clients.redisclient #

Redisclient

basic example to connect to local redis on 127.0.0.1:6379


import freeflowuniverse.crystallib.clients.redisclient

mut redis := redisclient.core_get()!
redis.set('test', 'some data') or { panic('set' + err.str() + '\n' + c.str()) }
r := redis.get('test')?
if r != 'some data' {
    panic('get error different result.' + '\n' + c.str())
}

redis commands can be found on https://redis.io/commands/

fn core_get #

fn core_get(url RedisURL) !Redis

fn get #

fn get(addr string) !Redis

fn get_redis_url #

fn get_redis_url(url string) !RedisURL

fn new #

fn new(addrs []string) !Redis

https://redis.io/topics/protocol examples: localhost:6379 /tmp/redis-default.sock

fn (RedisInternal) read_line #

fn (mut r RedisInternal) read_line() !string

fn (RedisInternal) disconnect #

fn (mut r RedisInternal) disconnect()

enum KeyType #

enum KeyType {
	t_none
	t_string
	t_list
	t_set
	t_zset
	t_hash
	t_stream
	t_unknown
}

struct Message #

struct Message {
pub:
	ret_queue string
	now       i64
	cmd       string
	data      string
}

struct RPCArgs #

struct RPCArgs {
pub:
	cmd     string @[required]
	data    string @[required]
	timeout u64  = 60000 // 60 sec
	wait    bool = true
}

struct Redis #

@[heap]
struct Redis {
pub:
	// current int
	connection_nr []int // is the position in the global
}

fn (Redis) append #

fn (mut r Redis) append(key string, value string) !int

fn (Redis) blpop #

fn (mut r Redis) blpop(keys []string, timeout f64) ![]string

fn (Redis) brpop #

fn (mut r Redis) brpop(keys []string, timeout f64) ![]string

fn (Redis) cache #

fn (mut r Redis) cache(namespace string) RedisCache

return a cache object starting from a redis connection

fn (Redis) decr #

fn (mut r Redis) decr(key string) !int

fn (Redis) decrby #

fn (mut r Redis) decrby(key string, decrement int) !int

fn (Redis) del #

fn (mut r Redis) del(key string) !int

fn (Redis) exists #

fn (mut r Redis) exists(key string) !bool

fn (Redis) expire #

fn (mut r Redis) expire(key string, seconds int) !int

fn (Redis) expireat #

fn (mut r Redis) expireat(key string, timestamp int) !int

fn (Redis) flushall #

fn (mut r Redis) flushall() !

fn (Redis) flushdb #

fn (mut r Redis) flushdb() !

fn (Redis) get #

fn (mut r Redis) get(key string) !string

fn (Redis) get_bool #

fn (mut r Redis) get_bool() !bool

fn (Redis) get_bytes #

fn (mut r Redis) get_bytes() ![]u8

fn (Redis) get_bytes_nil #

fn (mut r Redis) get_bytes_nil() ![]u8

fn (Redis) get_int #

fn (mut r Redis) get_int() !int

Todo: needs to use the resp library

fn (Redis) get_list_int #

fn (mut r Redis) get_list_int() ![]int

fn (Redis) get_list_str #

fn (mut r Redis) get_list_str() ![]string

fn (Redis) get_response #

fn (mut r Redis) get_response() !resp.RValue

fn (Redis) get_string #

fn (mut r Redis) get_string() !string

fn (Redis) get_string_nil #

fn (mut r Redis) get_string_nil() !string

fn (Redis) getrange #

fn (mut r Redis) getrange(key string, start int, end int) !string

fn (Redis) getset #

fn (mut r Redis) getset(key string, value string) !string

fn (Redis) hdel #

fn (mut r Redis) hdel(key string, skey string) !int

fn (Redis) hexists #

fn (mut r Redis) hexists(key string, skey string) !bool

fn (Redis) hget #

fn (mut r Redis) hget(key string, skey string) !string

fn (Redis) hgetall #

fn (mut r Redis) hgetall(key string) !map[string]string

fn (Redis) hkeys #

fn (mut r Redis) hkeys(key string) ![]string

fn (Redis) hset #

fn (mut r Redis) hset(key string, skey string, value string) !

fn (Redis) incr #

fn (mut r Redis) incr(key string) !int

fn (Redis) incrby #

fn (mut r Redis) incrby(key string, increment int) !int

fn (Redis) incrbyfloat #

fn (mut r Redis) incrbyfloat(key string, increment f64) !f64

fn (Redis) keys #

fn (mut r Redis) keys(pattern string) ![]string

fn (Redis) llen #

fn (mut r Redis) llen(key string) !int

fn (Redis) lpop #

fn (mut r Redis) lpop(key string) !string

fn (Redis) lpush #

fn (mut r Redis) lpush(key string, element string) !int

fn (Redis) lrange #

fn (mut r Redis) lrange(key string, start int, end int) ![]resp.RValue

fn (Redis) persist #

fn (mut r Redis) persist(key string) !int

fn (Redis) pexpire #

fn (mut r Redis) pexpire(key string, millis int) !int

fn (Redis) pexpireat #

fn (mut r Redis) pexpireat(key string, millistimestamp i64) !int

fn (Redis) ping #

fn (mut r Redis) ping() !string

fn (Redis) psetex #

fn (mut r Redis) psetex(key string, millisecond i64, value string) !

fn (Redis) pttl #

fn (mut r Redis) pttl(key string) !int

fn (Redis) queue_get #

fn (mut r Redis) queue_get(key string) RedisQueue

fn (Redis) randomkey #

fn (mut r Redis) randomkey() !string

fn (Redis) rename #

fn (mut r Redis) rename(key string, newkey string) !

fn (Redis) renamenx #

fn (mut r Redis) renamenx(key string, newkey string) !int

fn (Redis) rpc_get #

fn (mut r Redis) rpc_get(key string) RedisRpc

return a rpc mechanism

fn (Redis) rpop #

fn (mut r Redis) rpop(key string) !string

fn (Redis) rpush #

fn (mut r Redis) rpush(key string, element string) !int

fn (Redis) sadd #

fn (mut r Redis) sadd(key string, members []string) !int

Add the specified members to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members. An error is returned when the value stored at key is not a set.

fn (Redis) scan #

fn (mut r Redis) scan(cursor int) !(string, []string)

fn (Redis) script_load #

fn (mut r Redis) script_load(script string) !string

load a script and return the hash

fn (Redis) selectdb #

fn (mut r Redis) selectdb(database int) !

select is reserved

fn (Redis) send_expect_bool #

fn (mut r Redis) send_expect_bool(items []string) !bool

fn (Redis) send_expect_int #

fn (mut r Redis) send_expect_int(items []string) !int

send list of strings, expect int back

fn (Redis) send_expect_list #

fn (mut r Redis) send_expect_list(items []string) ![]resp.RValue

fn (Redis) send_expect_list_int #

fn (mut r Redis) send_expect_list_int(items []string) ![]int

fn (Redis) send_expect_list_str #

fn (mut r Redis) send_expect_list_str(items []string) ![]string

send list of strings, expect list of strings back

fn (Redis) send_expect_ok #

fn (mut r Redis) send_expect_ok(items []string) !

send list of strings, expect OK back

fn (Redis) send_expect_str #

fn (mut r Redis) send_expect_str(items []string) !string

send list of strings, expect string back

fn (Redis) send_expect_strnil #

fn (mut r Redis) send_expect_strnil(items []string) !string

send list of strings, expect string or nil back

fn (Redis) set #

fn (mut r Redis) set(key string, value string) !

fn (Redis) set_ex #

fn (mut r Redis) set_ex(key string, value string, ex string) !

fn (Redis) set_opts #

fn (mut r Redis) set_opts(key string, value string, opts SetOpts) !bool

fn (Redis) setex #

fn (mut r Redis) setex(key string, second i64, value string) !

fn (Redis) setnx #

fn (mut r Redis) setnx(key string, value string) !int

fn (Redis) setrange #

fn (mut r Redis) setrange(key string, offset int, value string) !int

fn (Redis) smismember #

fn (mut r Redis) smismember(key string, members []string) ![]int

Returns if member is a member of the set stored at key.

fn (Redis) strlen #

fn (mut r Redis) strlen(key string) !int

fn (Redis) ttl #

fn (mut r Redis) ttl(key string) !int

fn (Redis) type_of #

fn (mut r Redis) type_of(key string) !string

fn (Redis) write_rval #

fn (mut r Redis) write_rval(val resp.RValue) !

write resp value to the redis channel

struct RedisCache #

struct RedisCache {
mut:
	redis     &Redis @[str: skip]
	namespace string
	enabled   bool = true
}

fn (RedisCache) get #

fn (mut h RedisCache) get(key string) ?string

fn (RedisCache) set #

fn (mut h RedisCache) set(key string, val string, expire int) !

fn (RedisCache) exists #

fn (mut h RedisCache) exists(key string) bool

fn (RedisCache) reset #

fn (mut h RedisCache) reset() !

struct RedisQueue #

struct RedisQueue {
pub mut:
	key   string
	redis &Redis
}

fn (RedisQueue) add #

fn (mut q RedisQueue) add(val string) !

fn (RedisQueue) get #

fn (mut q RedisQueue) get(timeout u64) !string

timeout in msec

fn (RedisQueue) pop #

fn (mut q RedisQueue) pop() !string

get without timeout, returns none if nil

struct RedisRpc #

struct RedisRpc {
pub mut:
	key   string // queue name as used by this rpc
	redis &Redis
}

fn (RedisRpc) call #

fn (mut q RedisRpc) call(args RPCArgs) !string

send data to a queue and wait till return comes back timeout in milliseconds params cmd string @[required] data string @[required] timeout u64=60000 //60 sec wait bool=true

fn (RedisRpc) result #

fn (mut q RedisRpc) result(timeout u64, retqueue string) !string

get return once result processed

fn (RedisRpc) process #

fn (mut q RedisRpc) process(timeout u64, op fn (string, string) !string) !string

to be used by processor, to get request and execute, this is the server side of a RPC mechanism 2nd argument is a function which needs to execute the job: fn (string,string) !string

fn (RedisRpc) delete #

fn (mut q RedisRpc) delete() !

get without timeout, returns none if nil

struct RedisURL #

@[params]
struct RedisURL {
	address string = '127.0.0.1'
	port    int    = 6379
	db      int
}

struct Response #

struct Response {
pub:
	result string
	error  string
}

struct SetOpts #

struct SetOpts {
	ex       int = -4
	px       int = -4
	nx       bool
	xx       bool
	keep_ttl bool
}