1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-18 04:40:03 +02:00

Also move "redis wrapper" into it's own package.

This commit is contained in:
Henrik Hautakoski 2022-03-11 11:35:33 +01:00
parent 8df93439c3
commit 231b467dbe
5 changed files with 71 additions and 64 deletions

57
redis/wrapper.go Normal file
View file

@ -0,0 +1,57 @@
package redis
import (
"strings"
"context"
"time"
_redis "github.com/go-redis/redis/v8"
)
var rdb *_redis.Client
var redis_pipe _redis.Pipeliner
var redisCtx = context.Background()
var Prefix = "ship."
func Connect(addr string, password string, db int) error {
rdb = _redis.NewClient(&_redis.Options{
Addr: addr,
Password: password,
DB: db,
})
redis_pipe = rdb.Pipeline()
return rdb.Ping(redisCtx).Err()
}
func Client() *_redis.Client {
return rdb
}
func Key(components ...string) (string) {
return Prefix + strings.Join(components, ".")
}
func Get(key string) (*_redis.StringCmd) {
return rdb.Get(redisCtx, key)
}
func Set(key string, value interface{}, expiration time.Duration) (*_redis.StatusCmd) {
return rdb.Set(redisCtx, key, value, expiration)
}
func Publish(channel string, message interface{}) (*_redis.IntCmd) {
return rdb.Publish(redisCtx, channel, message)
}
func RegisterPublish(channel string, message interface{}) (*_redis.IntCmd) {
return redis_pipe.Publish(redisCtx, channel, message)
}
func Send() ([]_redis.Cmder, error) {
return redis_pipe.Exec(redisCtx)
}