From 1a4ce91fb5ee3901f97ee643bb654611dcc699a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 19 Jun 2024 16:39:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=85=E8=A3=85redis=E5=B8=B8=E7=94=A8?= =?UTF-8?q?=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wrapper.go | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 wrapper.go diff --git a/wrapper.go b/wrapper.go new file mode 100644 index 0000000..e3792ef --- /dev/null +++ b/wrapper.go @@ -0,0 +1,125 @@ +// Package redis ... +// +// Description : redis ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-06-19 16:15 +package redis + +import ( + "context" + "git.zhangdeman.cn/zhangdeman/redis/define" + "strings" + "time" +) + +var ( + Wrapper = &wrapper{} +) + +// wrapper 常用命令的包装 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:16 2024/6/19 +type wrapper struct { +} + +// Get Get命令 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:17 2024/6/19 +func (w *wrapper) Get(ctx context.Context, instanceFlag string, key string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "GET", key) +} + +// Del 删除命令 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:19 2024/6/19 +func (w *wrapper) Del(ctx context.Context, instanceFlag string, keyList ...string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "DEL", strings.Join(keyList, " ")) +} + +// SetEx 设置数据并且带有效期, 有效期单位 : s +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:22 2024/6/19 +func (w *wrapper) SetEx(ctx context.Context, instanceFlag string, key string, value string, ttl int64, withLock bool) *define.RedisResult { + now := time.Now().Unix() + if ttl > now { + // 传入过期时间大于当前时间, 说明是指定具体时间过期, 做一下处理 + ttl = ttl - now + } + if withLock { + return Client.Exec(ctx, instanceFlag, "SET", key, value, "EX", ttl, "NX") + } + return Client.Exec(ctx, instanceFlag, "SET", key, value, "EX", ttl) +} + +// LPop ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:29 2024/6/19 +func (w *wrapper) LPop(ctx context.Context, instanceFlag string, key string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "LPOP", key) +} + +// RPop ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:30 2024/6/19 +func (w *wrapper) RPop(ctx context.Context, instanceFlag string, key string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "RPOP", key) +} + +// LPush ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:31 2024/6/19 +func (w *wrapper) LPush(ctx context.Context, instanceFlag string, key string, value string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "LPUSH", key, value) +} + +// RPush ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:31 2024/6/19 +func (w *wrapper) RPush(ctx context.Context, instanceFlag string, key string, value string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "RPUSH", key, value) +} + +// HGet ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:33 2024/6/19 +func (w *wrapper) HGet(ctx context.Context, instanceFlag string, key string, field string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "HGET", key, field) +} + +// HSet ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:33 2024/6/19 +func (w *wrapper) HSet(ctx context.Context, instanceFlag string, key string, field string, value string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "HSET", key, field, value) +} + +// HDel ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:37 2024/6/19 +func (w *wrapper) HDel(ctx context.Context, instanceFlag string, key string, fieldList ...string) *define.RedisResult { + return Client.Exec(ctx, instanceFlag, "HSET", key, strings.Join(fieldList, " ")) +}