@rivascva/dt-idl 1.1.180 → 1.1.181

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.
@@ -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,11 +31,16 @@ 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
46
  func NewBaseRDB(address string, username string, password string, service string) RDB {
@@ -44,15 +51,17 @@ func NewBaseRDB(address string, username string, password string, service string
44
51
  Password: password,
45
52
  })
46
53
  return &BaseRDB{
47
- client: client,
48
- database: service,
54
+ client: client,
55
+ database: service,
56
+ verboseLog: nil,
49
57
  }
50
58
  }
51
59
 
52
60
  func (r *BaseRDB) NewRDB(database string) RDB {
53
61
  return &BaseRDB{
54
- client: r.client,
55
- database: fmt.Sprintf("%s:%s", r.database, database),
62
+ client: r.client,
63
+ database: fmt.Sprintf("%s:%s", r.database, database),
64
+ verboseLog: r.verboseLog,
56
65
  }
57
66
  }
58
67
 
@@ -69,6 +78,14 @@ func (r *BaseRDB) Set(ctx context.Context, key string, value any, expiration tim
69
78
  return fmt.Errorf("failed to set the value in the redis client: %w", err)
70
79
  }
71
80
 
81
+ // log the operation if the verbose log is not nil
82
+ if r.verboseLog != nil {
83
+ r.verboseLog.With(
84
+ "key", FormatKey(r.database, key),
85
+ "size_mb", float64(len(bytes))/1024/1024,
86
+ ).Info(ctx, "method SET called")
87
+ }
88
+
72
89
  return nil
73
90
  }
74
91
 
@@ -81,11 +98,13 @@ func (r *BaseRDB) MSet(ctx context.Context, entries map[string]any, expiration t
81
98
  // build a flat list of arguments for the lua script: [k1, v1, k2, v2, ..., expire_sec]
82
99
  // the last element is the TTL in seconds. Zero or negative expire means no TTL.
83
100
  scriptArgs := make([]any, 0, (len(entries)*2)+1)
101
+ totalBytes := 0
84
102
  for key, value := range entries {
85
103
  bytes, err := json.Marshal(value)
86
104
  if err != nil {
87
105
  return fmt.Errorf("failed to marshal the value for key %s: %w", key, err)
88
106
  }
107
+ totalBytes += len(bytes)
89
108
  scriptArgs = append(scriptArgs, FormatKey(r.database, key), bytes)
90
109
  }
91
110
  scriptArgs = append(scriptArgs, int(expiration.Seconds()))
@@ -95,6 +114,15 @@ func (r *BaseRDB) MSet(ctx context.Context, entries map[string]any, expiration t
95
114
  if err != nil {
96
115
  return fmt.Errorf("failed to set the entries in the redis client: %w", err)
97
116
  }
117
+
118
+ // log the operation if the verbose log is not nil
119
+ if r.verboseLog != nil {
120
+ r.verboseLog.With(
121
+ "num_keys", len(entries),
122
+ "size_mb", float64(totalBytes)/1024/1024,
123
+ ).Info(ctx, "method MSET called")
124
+ }
125
+
98
126
  return nil
99
127
  }
100
128
 
@@ -114,6 +142,14 @@ func (r *BaseRDB) Get(ctx context.Context, key string, dest any) error {
114
142
  return fmt.Errorf("failed to unmarshal the value: %w", err)
115
143
  }
116
144
 
145
+ // log the operation if the verbose log is not nil
146
+ if r.verboseLog != nil {
147
+ r.verboseLog.With(
148
+ "key", FormatKey(r.database, key),
149
+ "size_mb", float64(len(val))/1024/1024,
150
+ ).Info(ctx, "method GET called")
151
+ }
152
+
117
153
  return nil
118
154
  }
119
155
 
@@ -156,6 +192,7 @@ func (r *BaseRDB) MGet(ctx context.Context, keys []string, dests any) error {
156
192
  }
157
193
 
158
194
  // unmarshal the values into the given destinations
195
+ totalBytes := 0
159
196
  for i, val := range vals {
160
197
  if val == nil {
161
198
  continue
@@ -164,6 +201,7 @@ func (r *BaseRDB) MGet(ctx context.Context, keys []string, dests any) error {
164
201
  if !ok {
165
202
  return fmt.Errorf("the value is not a string")
166
203
  }
204
+ totalBytes += len(str)
167
205
 
168
206
  // create a new value of the pointed-to type
169
207
  pointedValue := reflect.New(pointedType).Elem()
@@ -179,6 +217,14 @@ func (r *BaseRDB) MGet(ctx context.Context, keys []string, dests any) error {
179
217
  destsValue.Index(i).Set(ptrValue)
180
218
  }
181
219
 
220
+ // log the operation if the verbose log is not nil
221
+ if r.verboseLog != nil {
222
+ r.verboseLog.With(
223
+ "num_keys", len(keys),
224
+ "size_mb", float64(totalBytes)/1024/1024,
225
+ ).Info(ctx, "method MGET called")
226
+ }
227
+
182
228
  return nil
183
229
  }
184
230
 
@@ -208,6 +254,14 @@ func (r *BaseRDB) GetKeysWithPattern(ctx context.Context, pattern string, window
208
254
  }
209
255
  }
210
256
 
257
+ // log the operation if the verbose log is not nil
258
+ if r.verboseLog != nil {
259
+ r.verboseLog.With(
260
+ "pattern", FormatKey(r.database, pattern),
261
+ "num_keys", len(result),
262
+ ).Info(ctx, "method GET_KEYS_WITH_PATTERN called")
263
+ }
264
+
211
265
  return result, nil
212
266
  }
213
267
 
@@ -218,6 +272,13 @@ func (r *BaseRDB) Delete(ctx context.Context, key string) error {
218
272
  return fmt.Errorf("failed to delete the value from the redis client: %w", err)
219
273
  }
220
274
 
275
+ // log the operation if the verbose log is not nil
276
+ if r.verboseLog != nil {
277
+ r.verboseLog.With(
278
+ "key", FormatKey(r.database, key),
279
+ ).Info(ctx, "method DELETE called")
280
+ }
281
+
221
282
  return nil
222
283
  }
223
284
 
@@ -231,5 +292,9 @@ func (r *BaseRDB) Close() error {
231
292
  return nil
232
293
  }
233
294
 
295
+ func (r *BaseRDB) SetVerboseLog(log log.Logger) {
296
+ r.verboseLog = log.With("name", "Redis Verbose Log")
297
+ }
298
+
234
299
  //exhaustruct:ignore - check for interface implementation
235
300
  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.181",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",