107 lines
3.3 KiB
Go
107 lines
3.3 KiB
Go
package fcredis
|
||
|
||
import (
|
||
"context"
|
||
goRedis "github.com/go-redis/redis/v8"
|
||
"time"
|
||
)
|
||
|
||
func Client() goRedis.UniversalClient {
|
||
return gUniClient
|
||
}
|
||
|
||
// Set Zero expiration means the key has no expiration time.
|
||
func Set(ctx context.Context, key string, value interface{}, expiration time.Duration) (string, error) {
|
||
return gUniClient.Set(ctx, key, value, expiration).Result()
|
||
}
|
||
|
||
func Del(ctx context.Context, key string) (int64, error) {
|
||
return gUniClient.Del(ctx, key).Result()
|
||
}
|
||
|
||
// SetNx Zero expiration means the key has no expiration time.
|
||
func SetNx(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool /*key is not exist*/, error) {
|
||
return gUniClient.SetNX(ctx, key, value, expiration).Result()
|
||
}
|
||
|
||
func SetBytes(ctx context.Context, key string, value []byte, expiration time.Duration) (string, error) {
|
||
return Set(ctx, key, string(value), expiration)
|
||
}
|
||
|
||
func LPush(ctx context.Context, key string, values ...interface{}) (int64, error) {
|
||
return gUniClient.LPush(ctx, key, values...).Result()
|
||
}
|
||
|
||
func LRem(ctx context.Context, key string, count int64, value string) (int64, error) {
|
||
return gUniClient.LRem(ctx, key, count, value).Result()
|
||
}
|
||
|
||
func LPop(ctx context.Context, key string) (string, error) {
|
||
return gUniClient.LPop(ctx, key).Result()
|
||
}
|
||
|
||
func LRange(ctx context.Context, key string, begin, end int64) ([]string, error) {
|
||
return gUniClient.LRange(ctx, key, begin, end).Result()
|
||
}
|
||
|
||
func ScriptLoad(ctx context.Context, script string) (string, error) {
|
||
return gUniClient.ScriptLoad(ctx, script).Result()
|
||
}
|
||
|
||
func EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) (interface{}, error) {
|
||
return gUniClient.EvalSha(ctx, sha1, keys, args...).Result()
|
||
}
|
||
|
||
func Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error) {
|
||
return gUniClient.Eval(ctx, script, keys, args...).Result()
|
||
}
|
||
|
||
// Expire returns bool (0/1) ,false: the key not exist
|
||
func Expire(ctx context.Context, key string, t time.Duration) (bool, error) {
|
||
return gUniClient.Expire(ctx, key, t).Result()
|
||
}
|
||
|
||
//Incr return result
|
||
func Incr(ctx context.Context, key string) (int64, error) {
|
||
return gUniClient.Incr(ctx, key).Result()
|
||
}
|
||
|
||
func Publish(ctx context.Context, channel string, value string) (int64, error) {
|
||
return gUniClient.Publish(ctx, channel, value).Result()
|
||
}
|
||
|
||
//Subscribe 的返回具体使用看其源码示例
|
||
func Subscribe(ctx context.Context, channels ...string) *goRedis.PubSub {
|
||
return gUniClient.Subscribe(ctx, channels...)
|
||
}
|
||
|
||
//Lock 返回true代表key不存在,加锁成功
|
||
//Lock 返回false代表key存在,加锁失败
|
||
func Lock(ctx context.Context, key string, expiration time.Duration) (bool, error) {
|
||
return gUniClient.SetNX(ctx, key, "1", expiration).Result()
|
||
}
|
||
|
||
func UnLock(ctx context.Context, key string) (int64, error) {
|
||
return gUniClient.Del(ctx, key).Result()
|
||
}
|
||
|
||
func Get(ctx context.Context, key string) (string, error) {
|
||
return get(ctx, key).Result()
|
||
}
|
||
|
||
func GetBytes(ctx context.Context, key string) ([]byte, error) {
|
||
return get(ctx, key).Bytes()
|
||
}
|
||
|
||
func GetString(ctx context.Context, key string) (string, error) {
|
||
return get(ctx, key).Result()
|
||
}
|
||
|
||
func TTL(ctx context.Context, key string) (time.Duration, error) {
|
||
return gUniClient.TTL(ctx, key).Result()
|
||
}
|
||
|
||
func get(ctx context.Context, key string) *goRedis.StringCmd {
|
||
return gUniClient.Get(ctx, key)
|
||
}
|