MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / NewValkeyDataCache

Function NewValkeyDataCache

lib/valkeycache/cache.go:39–68  ·  view source on GitHub ↗

NewValkeyDataCache creates a new ValkeyDataCache instance.

(
	keyPrefix string,
	host string,
	port string, // Will likely come from the environment variable as a string
	ttl time.Duration)

Source from the content-addressed store, hash-verified

37
38// NewValkeyDataCache creates a new ValkeyDataCache instance.
39func NewValkeyDataCache[K comparable, V []byte](
40 keyPrefix string,
41 host string,
42 port string, // Will likely come from the environment variable as a string
43 ttl time.Duration) (*ValkeyDataCache[K, V], error) {
44
45 addr := fmt.Sprintf("%s:%s", host, port)
46 operation := func() (valkey.Client, error) {
47 // nolint: exhaustruct // No need to use every option of 3rd party struct.
48 return valkey.NewClient(valkey.ClientOption{
49 InitAddress: []string{addr},
50 })
51 }
52
53 c, err := backoff.Retry(context.TODO(), operation,
54 backoff.WithBackOff(backoff.NewExponentialBackOff()),
55 // Should be less than the total time in the startup probe for the backend container in
56 // infra/backend/service.tf
57 backoff.WithMaxElapsedTime(25*time.Second),
58 )
59 if err != nil {
60 return nil, err
61 }
62
63 return &ValkeyDataCache[K, V]{
64 keyPrefix: keyPrefix,
65 client: c,
66 ttl: ttl,
67 }, nil
68}
69
70func (c *ValkeyDataCache[K, V]) cacheKey(key K) string {
71 return fmt.Sprintf("%s-%v", c.keyPrefix, key)

Callers 1

getTestValkeyFunction · 0.85

Calls

no outgoing calls

Tested by 1

getTestValkeyFunction · 0.68