@rivascva/dt-idl 1.1.166 → 1.1.167
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/go/redis/baserdb.go +19 -31
- package/go/redis/models.go +20 -1
- package/package.json +1 -1
package/go/redis/baserdb.go
CHANGED
|
@@ -13,12 +13,10 @@ import (
|
|
|
13
13
|
type RDB interface {
|
|
14
14
|
// NewRDB creates a new redis database from the parent database.
|
|
15
15
|
NewRDB(database string) RDB
|
|
16
|
-
// Set sets the value for the given key.
|
|
17
|
-
Set(ctx context.Context, key string, value any) error
|
|
18
|
-
// MSet sets the values for the given entries of key-value pairs.
|
|
19
|
-
MSet(ctx context.Context, entries map[string]any) error
|
|
20
|
-
// SetWithExpiration sets the value for the given key with an expiration time.
|
|
21
|
-
SetWithExpiration(ctx context.Context, key string, value any, expiration time.Duration) error
|
|
16
|
+
// Set sets the value for the given key with the given expiration.
|
|
17
|
+
Set(ctx context.Context, key string, value any, expiration time.Duration) error
|
|
18
|
+
// MSet sets the values for the given entries of key-value pairs with the given expiration.
|
|
19
|
+
MSet(ctx context.Context, entries map[string]any, expiration time.Duration) error
|
|
22
20
|
// Get gets the value for the given key. It unmarshals the value into the given destination.
|
|
23
21
|
Get(ctx context.Context, key string, dest any) error
|
|
24
22
|
// MGet gets the values for the given keys. It unmarshals the values into the given destinations in the same order as the keys.
|
|
@@ -54,7 +52,7 @@ func (r *BaseRDB) NewRDB(database string) RDB {
|
|
|
54
52
|
}
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
func (r *BaseRDB) Set(ctx context.Context, key string, value any) error {
|
|
55
|
+
func (r *BaseRDB) Set(ctx context.Context, key string, value any, expiration time.Duration) error {
|
|
58
56
|
// marshal the value into a JSON byte array
|
|
59
57
|
bytes, err := json.Marshal(value)
|
|
60
58
|
if err != nil {
|
|
@@ -62,7 +60,7 @@ func (r *BaseRDB) Set(ctx context.Context, key string, value any) error {
|
|
|
62
60
|
}
|
|
63
61
|
|
|
64
62
|
// set the value in the redis client
|
|
65
|
-
err = r.client.Set(ctx, FormatKey(r.database, key), bytes,
|
|
63
|
+
err = r.client.Set(ctx, FormatKey(r.database, key), bytes, expiration).Err()
|
|
66
64
|
if err != nil {
|
|
67
65
|
return fmt.Errorf("failed to set the value in the redis client: %w", err)
|
|
68
66
|
}
|
|
@@ -70,39 +68,29 @@ func (r *BaseRDB) Set(ctx context.Context, key string, value any) error {
|
|
|
70
68
|
return nil
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
func (r *BaseRDB) MSet(ctx context.Context, entries map[string]any) error {
|
|
74
|
-
//
|
|
75
|
-
|
|
71
|
+
func (r *BaseRDB) MSet(ctx context.Context, entries map[string]any, expiration time.Duration) error {
|
|
72
|
+
// check that there are entries to set
|
|
73
|
+
if len(entries) == 0 {
|
|
74
|
+
return fmt.Errorf("there are no entries to set")
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// build a flat list of arguments for the lua script: [k1, v1, k2, v2, ..., expire_sec]
|
|
78
|
+
// the last element is the TTL in seconds. Zero or negative expire means no TTL.
|
|
79
|
+
scriptArgs := make([]any, 0, (len(entries)*2)+1)
|
|
76
80
|
for key, value := range entries {
|
|
77
81
|
bytes, err := json.Marshal(value)
|
|
78
82
|
if err != nil {
|
|
79
83
|
return fmt.Errorf("failed to marshal the value for key %s: %w", key, err)
|
|
80
84
|
}
|
|
81
|
-
|
|
85
|
+
scriptArgs = append(scriptArgs, FormatKey(r.database, key), bytes)
|
|
82
86
|
}
|
|
87
|
+
scriptArgs = append(scriptArgs, int(expiration.Seconds()))
|
|
83
88
|
|
|
84
|
-
//
|
|
85
|
-
err :=
|
|
89
|
+
// run the lua script
|
|
90
|
+
err := MSetWithExpireScript.Run(ctx, r.client, []string{}, scriptArgs...).Err()
|
|
86
91
|
if err != nil {
|
|
87
92
|
return fmt.Errorf("failed to set the entries in the redis client: %w", err)
|
|
88
93
|
}
|
|
89
|
-
|
|
90
|
-
return nil
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
func (r *BaseRDB) SetWithExpiration(ctx context.Context, key string, value any, expiration time.Duration) error {
|
|
94
|
-
// marshal the value into a JSON byte array
|
|
95
|
-
bytes, err := json.Marshal(value)
|
|
96
|
-
if err != nil {
|
|
97
|
-
return fmt.Errorf("failed to marshal the value: %w", err)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// set the value in the redis client
|
|
101
|
-
err = r.client.Set(ctx, FormatKey(r.database, key), bytes, expiration).Err()
|
|
102
|
-
if err != nil {
|
|
103
|
-
return fmt.Errorf("failed to set the value in the redis client: %w", err)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
94
|
return nil
|
|
107
95
|
}
|
|
108
96
|
|
package/go/redis/models.go
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
package redis
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import (
|
|
4
|
+
"errors"
|
|
5
|
+
|
|
6
|
+
goredis "github.com/redis/go-redis/v9"
|
|
7
|
+
)
|
|
4
8
|
|
|
5
9
|
// ErrNotFound is returned when a value is not found in the redis database.
|
|
6
10
|
var ErrNotFound = errors.New("value not found in the redis database")
|
|
11
|
+
|
|
12
|
+
// MSetWithExpireScript is a local lua script that sets the value for the given key with the given expiration.
|
|
13
|
+
var MSetWithExpireScript = goredis.NewScript(`
|
|
14
|
+
local n = #ARGV - 1
|
|
15
|
+
if n < 1 then return 'OK' end
|
|
16
|
+
local expire = tonumber(ARGV[#ARGV])
|
|
17
|
+
for i = 1, n, 2 do
|
|
18
|
+
if expire > 0 then
|
|
19
|
+
redis.call('SET', ARGV[i], ARGV[i+1], 'EX', expire)
|
|
20
|
+
else
|
|
21
|
+
redis.call('SET', ARGV[i], ARGV[i+1])
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
return 'OK'
|
|
25
|
+
`)
|