Documentation
¶
Index ¶
- func Get[T any](store contract.CacheStore, key string) (T, bool)
- func NewMemoryStore() contract.CacheStore
- func Put[T any](store contract.CacheStore, key string, val T, ttl time.Duration)
- func Remember[T any](store contract.CacheStore, key string, ttl time.Duration, cb func() (T, error)) (T, error)
- type MemoryStore
- type RedisStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
func Get[T any](store contract.CacheStore, key string) (T, bool)
Get retrieves a typed value from the cache. If the key is missing or the value cannot be type-asserted to T, it returns the zero value and false.
func NewMemoryStore ¶
func NewMemoryStore() contract.CacheStore
NewMemoryStore creates a new MemoryStore and starts an optional cleanup routine.
func Remember ¶
func Remember[T any](store contract.CacheStore, key string, ttl time.Duration, cb func() (T, error)) (T, error)
Remember attempts to get a typed value from the cache. If it does not exist, it executes the provided closure `cb`, stores the result in the cache for the given ttl, and returns the result.
Types ¶
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is the in-memory implementation of CacheStore.
func (*MemoryStore) Forget ¶
func (m *MemoryStore) Forget(key string)
Forget removes a key from the cache.
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore implements contract.CacheStore backed by a Redis server.
func NewRedisStore ¶
func NewRedisStore(addr, password string, db int) *RedisStore
NewRedisStore creates a new RedisStore connected to the given Redis URL. addr is in the format "host:port" (e.g. "localhost:6379").
func (*RedisStore) Flush ¶
func (r *RedisStore) Flush()
Flush deletes all keys from the current Redis database.
func (*RedisStore) Forget ¶
func (r *RedisStore) Forget(key string)
Forget removes a key from the Redis cache.