cache

package
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 14 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("cache miss")

ErrCacheMiss indicates a cache miss error

View Source
var SimpleTool = func(ctx *gin.Context) *Tool[any] {
	return NewCacheTool[any](ctx, caches, nil)
}

Functions

func RestRedisInstance added in v0.0.11

func RestRedisInstance()

Types

type Cache

type Cache[T any] interface {
	IsInitialized() bool
	Get(key string) (T, error)
	Set(key string, value T, expiration time.Duration) error
	Del(key string) error
	Exists(key string) (bool, error)
	RPush(key string, value T) error
	BRPop(timeout time.Duration, key string) (value interface{}, err error)
	Incr(key string) (int64, error)
	Expire(key string, expiration time.Duration) error
}

Cache is a generic cache interface that defines basic cache operations

type GoCache

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

func NewGoCache

func NewGoCache[T any](defaultExpiration, cleanupInterval time.Duration) *GoCache[T]

func (*GoCache[T]) BRPop added in v0.0.11

func (g *GoCache[T]) BRPop(timeout time.Duration, key string) (T, error)

func (*GoCache[T]) Del added in v0.0.11

func (g *GoCache[T]) Del(key string) error

func (*GoCache[T]) Exists added in v0.0.11

func (g *GoCache[T]) Exists(key string) (bool, error)

func (*GoCache[T]) Expire added in v0.0.11

func (g *GoCache[T]) Expire(key string, expiration time.Duration) error

func (*GoCache[T]) Get

func (g *GoCache[T]) Get(key string) (T, error)

func (*GoCache[T]) Incr added in v0.0.11

func (g *GoCache[T]) Incr(key string) (int64, error)

func (*GoCache[T]) IsInitialized added in v0.0.11

func (g *GoCache[T]) IsInitialized() bool

func (*GoCache[T]) RPush added in v0.0.11

func (g *GoCache[T]) RPush(key string, value T) error

func (*GoCache[T]) Set

func (g *GoCache[T]) Set(key string, value T, expiration time.Duration) error

type LoaderFunc

type LoaderFunc[T any] func(key string) (T, error)

LoaderFunc is a function type for loading data from an external source

type Queue added in v0.0.11

type Queue[T any] struct {
	Items []T
}

type RedisCache

type RedisCache[T any] struct {
	Client *redis.Client
	Ctx    context.Context
}
var (
	RedisInstance *RedisCache[any]
)

func GetRedisInstance

func GetRedisInstance[T any](r ...*RedisCache[T]) *RedisCache[T]

func NewRedisCache

func NewRedisCache[T any](cfg RedisConfig) (*RedisCache[T], error)

func (*RedisCache[T]) BRPop added in v0.0.11

func (r *RedisCache[T]) BRPop(timeout time.Duration, key string) (value interface{}, err error)

func (*RedisCache[T]) Del added in v0.0.11

func (r *RedisCache[T]) Del(key string) error

func (*RedisCache[T]) Exists added in v0.0.11

func (r *RedisCache[T]) Exists(key string) (bool, error)

func (*RedisCache[T]) Expire added in v0.0.11

func (r *RedisCache[T]) Expire(key string, expiration time.Duration) error

func (*RedisCache[T]) Get

func (r *RedisCache[T]) Get(key string) (T, error)

func (*RedisCache[T]) Incr added in v0.0.11

func (r *RedisCache[T]) Incr(key string) (int64, error)

func (*RedisCache[T]) IsInitialized added in v0.0.11

func (r *RedisCache[T]) IsInitialized() bool

func (*RedisCache[T]) RPush added in v0.0.11

func (r *RedisCache[T]) RPush(key string, value interface{}) error

func (*RedisCache[T]) Set

func (r *RedisCache[T]) Set(key string, value T, expiration time.Duration) error

type RedisConfig

type RedisConfig struct {
	Addr     string
	Password string
	DB       int
}

RedisConfig holds the Redis configuration parameters

type Tool

type Tool[T any] struct {
	Ctx *gin.Context
	// contains filtered or unexported fields
}

Tool is a management tool for multi-level cache

func NewCacheTool

func NewCacheTool[T any](ctx *gin.Context, caches []Cache[T], loader LoaderFunc[T]) *Tool[T]

NewCacheTool creates a new Tool instance

func (*Tool[T]) Delete

func (c *Tool[T]) Delete(key string) error

Delete removes data from all cache levels

func (*Tool[T]) Get

func (c *Tool[T]) Get(key string, expiration time.Duration) (T, error)

Get retrieves data from the cache, searching each level in order, and loads from the loader if all levels miss

func (*Tool[T]) Set

func (c *Tool[T]) Set(key string, value T, expiration time.Duration) error

Set stores data in all cache levels

Jump to

Keyboard shortcuts

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