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)
| 37 | |
| 38 | // NewValkeyDataCache creates a new ValkeyDataCache instance. |
| 39 | func 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 | |
| 70 | func (c *ValkeyDataCache[K, V]) cacheKey(key K) string { |
| 71 | return fmt.Sprintf("%s-%v", c.keyPrefix, key) |
no outgoing calls