@rivascva/dt-idl 1.1.156 → 1.1.158
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 +27 -0
- package/package.json +1 -1
package/go/redis/baserdb.go
CHANGED
|
@@ -18,6 +18,9 @@ type RDB interface {
|
|
|
18
18
|
SetWithExpiration(ctx context.Context, key string, value any, expiration time.Duration) error
|
|
19
19
|
// Get gets the value for the given key. It unmarshals the value into the given destination.
|
|
20
20
|
Get(ctx context.Context, key string, dest any) error
|
|
21
|
+
// GetKeysWithPattern gets the stored keys containing the given pattern.
|
|
22
|
+
// The window specifies the number of keys to scan at a time.
|
|
23
|
+
GetKeysWithPattern(ctx context.Context, pattern string, window int) ([]string, error)
|
|
21
24
|
// Delete deletes the value for the given key.
|
|
22
25
|
Delete(ctx context.Context, key string) error
|
|
23
26
|
// Close closes the redis connection.
|
|
@@ -96,6 +99,30 @@ func (r *BaseRDB) Get(ctx context.Context, key string, dest any) error {
|
|
|
96
99
|
return nil
|
|
97
100
|
}
|
|
98
101
|
|
|
102
|
+
func (r *BaseRDB) GetKeysWithPattern(ctx context.Context, pattern string, window int) ([]string, error) {
|
|
103
|
+
// initiate helper variables
|
|
104
|
+
result := make([]string, 0)
|
|
105
|
+
cursor := uint64(0)
|
|
106
|
+
|
|
107
|
+
for {
|
|
108
|
+
// scan the database for keys matching the given pattern
|
|
109
|
+
keys, cursor, err := r.client.Scan(ctx, cursor, FormatKey(r.database, pattern), int64(window)).Result()
|
|
110
|
+
if err != nil {
|
|
111
|
+
return nil, fmt.Errorf("failed to scan the database: %w", err)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// add the keys to the result
|
|
115
|
+
result = append(result, keys...)
|
|
116
|
+
|
|
117
|
+
// if the cursor is 0, we have scanned all the keys
|
|
118
|
+
if cursor == 0 {
|
|
119
|
+
break
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return result, nil
|
|
124
|
+
}
|
|
125
|
+
|
|
99
126
|
func (r *BaseRDB) Delete(ctx context.Context, key string) error {
|
|
100
127
|
// delete the value from the redis client
|
|
101
128
|
err := r.client.Del(ctx, FormatKey(r.database, key)).Err()
|