-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti.go
More file actions
72 lines (66 loc) · 2.13 KB
/
multi.go
File metadata and controls
72 lines (66 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright CattleCloud LLC 2025, 2026
// SPDX-License-Identifier: BSD-3-Clause
package memc
import "errors"
// A Pair associates two elements.
type Pair[T, U any] struct {
A T
B U
}
// SetMulti will store each item in items using the item's associated key,
// possibly overwritting any existing data. New items are at the top of the
// LRU.
//
// Errors are accumulated using errors.Join.
//
// Uses Client c to connect to a memcached instance, and automatically handles
// connection pooling and reuse.
//
// One or more Option(s) may be applied to configure things such as the
// value expiration TTL or its associated flags.
func SetMulti[T any](c *Client, items []*Pair[string, T], opts ...Option) error {
var errs []error
for _, item := range items {
if err := Set(c, item.A, item.B, opts...); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// AddMulti will store each item in items using the item's associated key,
// but only if the item does not currently exist. New items are at the top of
// the LRU.
//
// Errors are accumulated using errors.Join.
//
// Uses Client c to connect to a memcached instance, and automatically handles
// connection pooling and reuse.
//
// One or more Option(s) may be applied to configure things such as the
// value expiration TTL or its associated flags.
func AddMulti[T any](c *Client, items []*Pair[string, T], opts ...Option) error {
var errs []error
for _, item := range items {
if err := Add(c, item.A, item.B, opts...); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// Get the values associated with the given keys. One Pair[T, error] return
// value for each of the given keys, in the same order.
//
// Uses Client c to connect to a memcached instance, and automatically handles
// connection pooling and reuse.
func GetMulti[T any](c *Client, keys []string) []*Pair[T, error] {
results := make([]*Pair[T, error], 0, len(keys))
for _, key := range keys {
v, err := Get[T](c, key)
if err != nil {
results = append(results, &Pair[T, error]{B: err})
} else {
results = append(results, &Pair[T, error]{A: v})
}
}
return results
}