Skip to content

Commit 75251eb

Browse files
committed
feat(yacache): update MGet response signature
1 parent 6b040ef commit 75251eb

5 files changed

Lines changed: 48 additions & 54 deletions

File tree

yacache/memory.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,32 +344,43 @@ func (m *Memory) Get(
344344
return value.Value, nil
345345
}
346346

347-
// MGet fetches several keys at once and returns a map[key]value.
348-
// If any requested key is absent, the call fails with ErrFailedToMGetValues.
347+
// MGet fetches the values for the specified keys from the in-memory cache.
348+
//
349+
// It returns a map where each found key is mapped to its corresponding string value.
350+
// Keys that are not found are silently skipped — no error is returned.
351+
// As a result, the returned map may contain fewer entries than requested.
349352
//
350353
// Example:
351354
//
352-
// values, _ := memory.MGet(ctx, "k1", "k2", "k3")
355+
// ctx := context.Background()
356+
// values, err := memory.MGet(ctx, "k1", "k2", "k3")
357+
// if err != nil {
358+
// log.Fatalf("memory MGet failed: %v", err)
359+
// }
360+
// for k, v := range values {
361+
// fmt.Printf("%s = %s\n", k, v)
362+
// }
363+
//
364+
// Returns:
365+
// - map[string]string: found keys mapped to their values (missing keys are excluded)
366+
// - yaerrors.Error: always nil in this implementation
353367
func (m *Memory) MGet(
354368
_ context.Context,
355369
keys ...string,
356-
) (map[string]*string, yaerrors.Error) {
370+
) (map[string]string, yaerrors.Error) {
357371
m.mutex.RLock()
358372

359373
defer m.mutex.RUnlock()
360374

361-
result := make(map[string]*string)
375+
result := make(map[string]string)
362376

363377
for _, key := range keys {
364378
value, ok := m.inner.Map[key]
365379
if !ok {
366-
result[key] = nil
367-
368380
continue
369381
}
370382

371-
v := value.Value
372-
result[key] = &v
383+
result[key] = value.Value
373384
}
374385

375386
return result, nil

yacache/memory_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,9 @@ func TestMemory_FetchWorkflow_Works(t *testing.T) {
220220
})
221221

222222
t.Run("[MGET] - get items works", func(t *testing.T) {
223-
expected := make(map[string]*string)
223+
expected := make(map[string]string)
224224

225-
yavaluePtr := yavalue
226-
expected[yachildKey] = &yavaluePtr
225+
expected[yachildKey] = yavalue
227226

228227
var keys []string
229228

@@ -240,8 +239,7 @@ func TestMemory_FetchWorkflow_Works(t *testing.T) {
240239
panic(err)
241240
}
242241

243-
yavaluePtr := fmt.Sprintf("%s:%d", yavalue, i)
244-
expected[keys[len(keys)-1]] = &yavaluePtr
242+
expected[keys[len(keys)-1]] = fmt.Sprintf("%s:%d", yavalue, i)
245243
}
246244

247245
result, _ := memory.MGet(ctx, keys...)

yacache/redis.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,12 @@ func (r *Redis) Get(
331331

332332
// MGet performs a batch GET operation for the given keys using Redis.
333333
//
334-
// It expects the number of returned values to exactly match the number
335-
// of requested keys. If the underlying Redis call fails, or the response
336-
// is incomplete or invalid, the method returns ErrFailedToMGetValues.
334+
// It returns a map where each successfully fetched key is mapped to its
335+
// corresponding string value. Keys that are missing in Redis or whose
336+
// values cannot be cast to strings are silently skipped.
337337
//
338-
// The returned map contains each requested key mapped to its corresponding
339-
// value. If a key does not exist or the value cannot be cast to string,
340-
// it will be mapped to nil.
338+
// Unlike atomic variants, this method does not fail if some keys are missing;
339+
// the resulting map may contain fewer entries than requested.
341340
//
342341
// Example:
343342
//
@@ -347,20 +346,16 @@ func (r *Redis) Get(
347346
// log.Fatalf("failed to fetch keys: %v", err)
348347
// }
349348
// for k, v := range values {
350-
// if v != nil {
351-
// fmt.Printf("%s = %s\n", k, *v)
352-
// } else {
353-
// fmt.Printf("%s = <nil>\n", k)
354-
// }
349+
// fmt.Printf("%s = %s\n", k, v)
355350
// }
356351
//
357352
// Returns:
358-
// - map[string]*string: keys mapped to their corresponding string values (or nil)
359-
// - yaerrors.Error: wrapped error if Redis call fails or result is inconsistent
353+
// - map[string]string: found keys mapped to their string values
354+
// - yaerrors.Error: wrapped Redis error if the MGET command fails
360355
func (r *Redis) MGet(
361356
ctx context.Context,
362357
keys ...string,
363-
) (map[string]*string, yaerrors.Error) {
358+
) (map[string]string, yaerrors.Error) {
364359
values, err := r.client.MGet(ctx, keys...).Result()
365360
if err != nil {
366361
return nil, yaerrors.FromError(
@@ -370,24 +365,19 @@ func (r *Redis) MGet(
370365
)
371366
}
372367

373-
result := make(map[string]*string)
368+
result := make(map[string]string)
374369

375370
for i, key := range keys {
376371
if values[i] == nil {
377-
result[key] = nil
378-
379372
continue
380373
}
381374

382375
value, ok := values[i].(string)
383376
if !ok {
384-
result[key] = nil
385-
386377
continue
387378
}
388379

389-
v := value
390-
result[key] = &v
380+
result[key] = value
391381
}
392382

393383
return result, nil

yacache/redis_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,27 @@ func TestRedisCacheService(t *testing.T) {
5454
})
5555

5656
t.Run("[MGet] - multi get values works", func(t *testing.T) {
57-
expected := make(map[string]*string)
57+
expected := make(map[string]string)
5858

59-
yavaluePtr := yavalue2
60-
expected[yachildKey2] = &yavaluePtr
59+
expected[yachildKey2] = yavalue2
6160

6261
var keys []string
6362

6463
for i := range 10 {
6564
keys = append(keys, fmt.Sprintf("%s:%d", yamainKey2, i))
65+
value := fmt.Sprintf("%s:%d", yavalue2, i)
6666

6767
err := redis.Set(
6868
ctx,
69-
keys[len(keys)-1],
70-
fmt.Sprintf("%s:%d", yavalue2, i),
69+
keys[i],
70+
value,
7171
yattl,
7272
)
7373
if err != nil {
7474
panic(err)
7575
}
7676

77-
yavaluePtr := fmt.Sprintf("%s:%d", yavalue2, i)
78-
expected[keys[len(keys)-1]] = &yavaluePtr
77+
expected[keys[i]] = value
7978
}
8079

8180
keys = append(keys, "key_which_doesnt_contains___))")

yacache/yacache.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ type Cache[T Container] interface {
204204

205205
// MGet fetches the values for the specified keys from the cache.
206206
//
207-
// It returns a map where each key is mapped to its corresponding value.
208-
// If any of the keys are missing or the operation fails,
209-
// it returns an error of type ErrFailedToMGetValues, allowing callers to
210-
// rely on the atomicity of the operation — it's all or nothing.
207+
// It returns a map where each key is mapped to its corresponding string value.
208+
// If any key is missing or the number of returned values does not match
209+
// the number of requested keys, the method fails with ErrFailedToMGetValues,
210+
// ensuring atomicity — either all values are returned or none.
211211
//
212212
// Example:
213213
//
@@ -217,20 +217,16 @@ type Cache[T Container] interface {
217217
// log.Fatalf("failed to fetch keys: %v", err)
218218
// }
219219
// for k, v := range values {
220-
// if v != nil {
221-
// fmt.Printf("%s = %s\n", k, *v)
222-
// } else {
223-
// fmt.Printf("%s = <nil>\n", k)
224-
// }
220+
// fmt.Printf("%s = %s\n", k, v)
225221
// }
226222
//
227223
// Returns:
228-
// - map[string]*string: a map of keys to their string values (or nil if not found)
229-
// - yaerrors.Error: a wrapped error indicating failure
224+
// - map[string]string: all requested keys mapped to their string values
225+
// - yaerrors.Error: a wrapped error if the operation fails or is incomplete
230226
MGet(
231227
ctx context.Context,
232228
keys ...string,
233-
) (map[string]*string, yaerrors.Error)
229+
) (map[string]string, yaerrors.Error)
234230

235231
// GetDel atomically reads **and then deletes** key.
236232
// Useful for one-shot tokens or queue semantics.

0 commit comments

Comments
 (0)