cache

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

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 Put

func Put[T any](store contract.CacheStore, key string, val T, ttl time.Duration)

Put stores a typed value in the cache.

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) Flush

func (m *MemoryStore) Flush()

Flush removes all items from the cache.

func (*MemoryStore) Forget

func (m *MemoryStore) Forget(key string)

Forget removes a key from the cache.

func (*MemoryStore) Get

func (m *MemoryStore) Get(key string) (any, bool)

Get retrieves a value from the cache.

func (*MemoryStore) Put

func (m *MemoryStore) Put(key string, val any, ttl time.Duration)

Put stores a value in 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.

func (*RedisStore) Get

func (r *RedisStore) Get(key string) (any, bool)

Get retrieves a value from Redis and deserializes it.

func (*RedisStore) Put

func (r *RedisStore) Put(key string, val any, ttl time.Duration)

Put stores a value in Redis, serialized as JSON, with an expiry TTL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL