Cache stores a value in the cache.
( ctx context.Context, key K, in V, options ...cachetypes.CacheOption, )
| 73 | |
| 74 | // Cache stores a value in the cache. |
| 75 | func (c *ValkeyDataCache[K, V]) Cache( |
| 76 | ctx context.Context, |
| 77 | key K, |
| 78 | in V, |
| 79 | options ...cachetypes.CacheOption, |
| 80 | ) error { |
| 81 | // Build default config for cache operation |
| 82 | cacheCfg := cachetypes.NewCacheConfig(c.ttl) |
| 83 | |
| 84 | // Apply options to config |
| 85 | for _, opt := range options { |
| 86 | opt(cacheCfg) |
| 87 | } |
| 88 | |
| 89 | err := c.client.Do(ctx, c.client.B().Set().Key(c.cacheKey(key)). |
| 90 | Value(valkey.BinaryString(in)).Ex(cacheCfg.GetTTL()).Build()).Error() |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | // Get retrieves a value from the cache. |
| 99 | // It returns cachetypes.ErrCachedDataNotFound if it does not exist. |