redundant set function

This commit is contained in:
Nam Huynh 2022-12-05 15:01:07 +07:00
parent d28b408b04
commit d705825351
2 changed files with 8 additions and 12 deletions

2
go.mod
View File

@ -1,4 +1,4 @@
module git.selly.red/Selly-Modules/redisdb module git.selly.red/Selly-Modules/redisdb/v3
go 1.19 go 1.19

View File

@ -4,25 +4,21 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"time"
) )
// SetKeyValue ... // SetKeyValue ...
func SetKeyValue(ctx context.Context, key string, value interface{}) { func SetKeyValue(ctx context.Context, key string, value interface{}, options ...Options) {
b, err := json.Marshal(value) b, err := json.Marshal(value)
if err != nil { if err != nil {
fmt.Printf("[redisdb] SetKeyValue error: %s - data: %s - %v \n", err.Error(), key, value) fmt.Printf("[redisdb] SetKeyValue error: %s - data: %s - %v \n", err.Error(), key, value)
return return
} }
rdb.Set(ctx, key, b, 0)
// get options
opts := allOptions{}
for _, o := range options {
o.apply(&opts)
} }
// SetTTL ... rdb.Set(ctx, key, b, opts.ttl)
func SetTTL(ctx context.Context, key string, value interface{}, d time.Duration) {
b, err := json.Marshal(value)
if err != nil {
fmt.Printf("[redisdb] SetTTL error: %s - data: %s - %d - %v \n", err.Error(), key, d, value)
return
}
rdb.Set(ctx, key, b, d)
} }