cache

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTyped

func GetTyped[T any](c Cache, key string) (T, error)

GetTyped is a generic helper for type-safe cache retrieval. Example: user, err := GetTyped[User](cache, "user:123")

func MustGet

func MustGet[T any](c Cache, key string) T

MustGet is a generic helper that panics on error (use only when cache hit is guaranteed). Example: config := MustGet[Config](cache, "app:config")

Types

type BadgerCache

type BadgerCache struct {
	Conn   *badger.DB
	Prefix string
}

func (*BadgerCache) EmptyByMatch

func (b *BadgerCache) EmptyByMatch(str string) error

func (*BadgerCache) Flush

func (b *BadgerCache) Flush() error

func (*BadgerCache) Forget

func (b *BadgerCache) Forget(str string) error

func (*BadgerCache) Get

func (b *BadgerCache) Get(str string) (interface{}, error)

func (*BadgerCache) Has

func (b *BadgerCache) Has(str string) (bool, error)

func (*BadgerCache) Set

func (b *BadgerCache) Set(str string, value interface{}, ttl ...int) error

type Cache

type Cache interface {
	Has(string) (bool, error)
	Get(string) (interface{}, error)
	Set(string, interface{}, ...int) error
	Forget(string) error
	EmptyByMatch(string) error
	Flush() error
}

type Entry

type Entry map[string]interface{}

type RedisCache

type RedisCache struct {
	Conn   *redis.Pool
	Prefix string
}

func (*RedisCache) EmptyByMatch

func (c *RedisCache) EmptyByMatch(str string) error

func (*RedisCache) Flush

func (c *RedisCache) Flush() error

func (*RedisCache) Forget

func (c *RedisCache) Forget(str string) error

func (*RedisCache) Get

func (c *RedisCache) Get(str string) (interface{}, error)

func (*RedisCache) Has

func (c *RedisCache) Has(str string) (bool, error)

func (*RedisCache) Set

func (c *RedisCache) Set(str string, value interface{}, ttl ...int) error

type TypedCache

type TypedCache[T any] struct {
	// contains filtered or unexported fields
}

TypedCache provides a type-safe wrapper around the Cache interface. Example:

userCache := NewTypedCache[User](redisCache)
userCache.Set("user:123", user, 3600)
user, err := userCache.Get("user:123")

func NewTypedCache

func NewTypedCache[T any](c Cache) *TypedCache[T]

NewTypedCache creates a new type-safe cache wrapper.

func (*TypedCache[T]) EmptyByMatch

func (tc *TypedCache[T]) EmptyByMatch(pattern string) error

EmptyByMatch removes all keys matching the pattern.

func (*TypedCache[T]) Flush

func (tc *TypedCache[T]) Flush() error

Flush removes all entries from the cache.

func (*TypedCache[T]) Forget

func (tc *TypedCache[T]) Forget(key string) error

Forget removes a key from the cache.

func (*TypedCache[T]) Get

func (tc *TypedCache[T]) Get(key string) (T, error)

Get retrieves a typed value from the cache.

func (*TypedCache[T]) Has

func (tc *TypedCache[T]) Has(key string) (bool, error)

Has checks if a key exists in the cache.

func (*TypedCache[T]) Set

func (tc *TypedCache[T]) Set(key string, value T, ttl ...int) error

Set stores a typed value in the cache with optional TTL in seconds.

Jump to

Keyboard shortcuts

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