@rivascva/dt-idl 1.1.180 → 1.1.182

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.
Files changed (2) hide show
  1. package/go/redis/baserdb.go +82 -13
  2. package/package.json +1 -1
@@ -7,6 +7,8 @@ import (
7
7
  "reflect"
8
8
  "time"
9
9
 
10
+ "github.com/RivasCVA/dt-idl/go/log"
11
+
10
12
  goredis "github.com/redis/go-redis/v9"
11
13
  )
12
14
 
@@ -29,30 +31,41 @@ type RDB interface {
29
31
  Delete(ctx context.Context, key string) error
30
32
  // Close closes the redis connection.
31
33
  Close() error
34
+ // SetVerboseLog sets the verbose log.
35
+ // It is used to log the operations performed on the redis database.
36
+ // This is useful for debugging and monitoring.
37
+ SetVerboseLog(verboseLog log.Logger)
32
38
  }
33
39
 
34
40
  type BaseRDB struct {
35
- client *goredis.Client
36
- database string
41
+ client *goredis.Client
42
+ database string
43
+ verboseLog log.Logger
37
44
  }
38
45
 
39
- func NewBaseRDB(address string, username string, password string, service string) RDB {
46
+ func NewBaseRDB(url string, service string) (RDB, error) {
47
+ // parse the connection url
48
+ opts, err := goredis.ParseURL(url)
49
+ if err != nil {
50
+ return nil, fmt.Errorf("failed to parse the url: %w", err)
51
+ }
52
+
53
+ // create the redis client
54
+ client := goredis.NewClient(opts)
55
+
40
56
  //exhaustruct:ignore - other fields are optional
41
- client := goredis.NewClient(&goredis.Options{
42
- Addr: address,
43
- Username: username,
44
- Password: password,
45
- })
46
57
  return &BaseRDB{
47
- client: client,
48
- database: service,
49
- }
58
+ client: client,
59
+ database: service,
60
+ verboseLog: nil,
61
+ }, nil
50
62
  }
51
63
 
52
64
  func (r *BaseRDB) NewRDB(database string) RDB {
53
65
  return &BaseRDB{
54
- client: r.client,
55
- database: fmt.Sprintf("%s:%s", r.database, database),
66
+ client: r.client,
67
+ database: fmt.Sprintf("%s:%s", r.database, database),
68
+ verboseLog: r.verboseLog,
56
69
  }
57
70
  }
58
71
 
@@ -69,6 +82,14 @@ func (r *BaseRDB) Set(ctx context.Context, key string, value any, expiration tim
69
82
  return fmt.Errorf("failed to set the value in the redis client: %w", err)
70
83
  }
71
84
 
85
+ // log the operation if the verbose log is not nil
86
+ if r.verboseLog != nil {
87
+ r.verboseLog.With(
88
+ "key", FormatKey(r.database, key),
89
+ "size_mb", float64(len(bytes))/1024/1024,
90
+ ).Info(ctx, "method SET called")
91
+ }
92
+
72
93
  return nil
73
94
  }
74
95
 
@@ -81,11 +102,13 @@ func (r *BaseRDB) MSet(ctx context.Context, entries map[string]any, expiration t
81
102
  // build a flat list of arguments for the lua script: [k1, v1, k2, v2, ..., expire_sec]
82
103
  // the last element is the TTL in seconds. Zero or negative expire means no TTL.
83
104
  scriptArgs := make([]any, 0, (len(entries)*2)+1)
105
+ totalBytes := 0
84
106
  for key, value := range entries {
85
107
  bytes, err := json.Marshal(value)
86
108
  if err != nil {
87
109
  return fmt.Errorf("failed to marshal the value for key %s: %w", key, err)
88
110
  }
111
+ totalBytes += len(bytes)
89
112
  scriptArgs = append(scriptArgs, FormatKey(r.database, key), bytes)
90
113
  }
91
114
  scriptArgs = append(scriptArgs, int(expiration.Seconds()))
@@ -95,6 +118,15 @@ func (r *BaseRDB) MSet(ctx context.Context, entries map[string]any, expiration t
95
118
  if err != nil {
96
119
  return fmt.Errorf("failed to set the entries in the redis client: %w", err)
97
120
  }
121
+
122
+ // log the operation if the verbose log is not nil
123
+ if r.verboseLog != nil {
124
+ r.verboseLog.With(
125
+ "num_keys", len(entries),
126
+ "size_mb", float64(totalBytes)/1024/1024,
127
+ ).Info(ctx, "method MSET called")
128
+ }
129
+
98
130
  return nil
99
131
  }
100
132
 
@@ -114,6 +146,14 @@ func (r *BaseRDB) Get(ctx context.Context, key string, dest any) error {
114
146
  return fmt.Errorf("failed to unmarshal the value: %w", err)
115
147
  }
116
148
 
149
+ // log the operation if the verbose log is not nil
150
+ if r.verboseLog != nil {
151
+ r.verboseLog.With(
152
+ "key", FormatKey(r.database, key),
153
+ "size_mb", float64(len(val))/1024/1024,
154
+ ).Info(ctx, "method GET called")
155
+ }
156
+
117
157
  return nil
118
158
  }
119
159
 
@@ -156,6 +196,7 @@ func (r *BaseRDB) MGet(ctx context.Context, keys []string, dests any) error {
156
196
  }
157
197
 
158
198
  // unmarshal the values into the given destinations
199
+ totalBytes := 0
159
200
  for i, val := range vals {
160
201
  if val == nil {
161
202
  continue
@@ -164,6 +205,7 @@ func (r *BaseRDB) MGet(ctx context.Context, keys []string, dests any) error {
164
205
  if !ok {
165
206
  return fmt.Errorf("the value is not a string")
166
207
  }
208
+ totalBytes += len(str)
167
209
 
168
210
  // create a new value of the pointed-to type
169
211
  pointedValue := reflect.New(pointedType).Elem()
@@ -179,6 +221,14 @@ func (r *BaseRDB) MGet(ctx context.Context, keys []string, dests any) error {
179
221
  destsValue.Index(i).Set(ptrValue)
180
222
  }
181
223
 
224
+ // log the operation if the verbose log is not nil
225
+ if r.verboseLog != nil {
226
+ r.verboseLog.With(
227
+ "num_keys", len(keys),
228
+ "size_mb", float64(totalBytes)/1024/1024,
229
+ ).Info(ctx, "method MGET called")
230
+ }
231
+
182
232
  return nil
183
233
  }
184
234
 
@@ -208,6 +258,14 @@ func (r *BaseRDB) GetKeysWithPattern(ctx context.Context, pattern string, window
208
258
  }
209
259
  }
210
260
 
261
+ // log the operation if the verbose log is not nil
262
+ if r.verboseLog != nil {
263
+ r.verboseLog.With(
264
+ "pattern", FormatKey(r.database, pattern),
265
+ "num_keys", len(result),
266
+ ).Info(ctx, "method GET_KEYS_WITH_PATTERN called")
267
+ }
268
+
211
269
  return result, nil
212
270
  }
213
271
 
@@ -218,6 +276,13 @@ func (r *BaseRDB) Delete(ctx context.Context, key string) error {
218
276
  return fmt.Errorf("failed to delete the value from the redis client: %w", err)
219
277
  }
220
278
 
279
+ // log the operation if the verbose log is not nil
280
+ if r.verboseLog != nil {
281
+ r.verboseLog.With(
282
+ "key", FormatKey(r.database, key),
283
+ ).Info(ctx, "method DELETE called")
284
+ }
285
+
221
286
  return nil
222
287
  }
223
288
 
@@ -231,5 +296,9 @@ func (r *BaseRDB) Close() error {
231
296
  return nil
232
297
  }
233
298
 
299
+ func (r *BaseRDB) SetVerboseLog(log log.Logger) {
300
+ r.verboseLog = log.With("name", "Redis Verbose Log")
301
+ }
302
+
234
303
  //exhaustruct:ignore - check for interface implementation
235
304
  var _ RDB = &BaseRDB{}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivascva/dt-idl",
3
- "version": "1.1.180",
3
+ "version": "1.1.182",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",