cache

package
v0.0.38 Latest Latest
Warning

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

Go to latest
Published: May 8, 2025 License: MIT Imports: 19 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 T, err error)
	Incr(key string) (int64, error)
	Expire(key string, expiration time.Duration) error
	Flush() error
}

Cache is a generic cache interface that defines basic cache operations

func New added in v0.0.33

func New[T any](t Type, args ...any) Cache[T]

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]) Flush added in v0.0.33

func (g *GoCache[T]) Flush() 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 JSONFileCache added in v0.0.33

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

func NewJSONCache added in v0.0.33

func NewJSONCache[T any](cacheType string) (*JSONFileCache[T], error)

func (*JSONFileCache[T]) BRPop added in v0.0.33

func (c *JSONFileCache[T]) BRPop(timeout time.Duration, key string) (value T, err error)

func (*JSONFileCache[T]) Del added in v0.0.33

func (c *JSONFileCache[T]) Del(key string) error

func (*JSONFileCache[T]) Exists added in v0.0.33

func (c *JSONFileCache[T]) Exists(key string) (bool, error)

func (*JSONFileCache[T]) Expire added in v0.0.33

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

func (*JSONFileCache[T]) Flush added in v0.0.33

func (c *JSONFileCache[T]) Flush() error

func (*JSONFileCache[T]) Get added in v0.0.33

func (c *JSONFileCache[T]) Get(key string) (T, error)

func (*JSONFileCache[T]) Incr added in v0.0.33

func (c *JSONFileCache[T]) Incr(key string) (int64, error)

func (*JSONFileCache[T]) IsInitialized added in v0.0.33

func (c *JSONFileCache[T]) IsInitialized() bool

func (*JSONFileCache[T]) RPush added in v0.0.33

func (c *JSONFileCache[T]) RPush(key string, value T) error

func (*JSONFileCache[T]) Set added in v0.0.33

func (c *JSONFileCache[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 PageCache added in v0.0.36

type PageCache[T any] struct {
	Total int64 `json:"total"`
	Page  int64 `json:"page"`
	Size  int64 `json:"size"`
	Items []*T  `json:"items"`
}

type PageSorter added in v0.0.36

type PageSorter struct {
	SortField string
	Asc       bool
}

func PageSorterAsc added in v0.0.36

func PageSorterAsc(field string) PageSorter

func PageSorterDesc added in v0.0.36

func PageSorterDesc(field string) PageSorter

type PageStorage added in v0.0.36

type PageStorage[T any] interface {
	Put(value T) error
	Find(page, size int64, conditions map[string]any, sort ...PageSorter) (*PageCache[T], error)
	Get(conditions map[string]any) (*T, error)
	Count(conditions map[string]any) (int64, error)
	Update(conditions map[string]any, value *T) error
}

func NewPageStorage added in v0.0.36

func NewPageStorage[T any](ctx context.Context, t Type, args ...any) PageStorage[T]

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 T, 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]) Flush added in v0.0.33

func (r *RedisCache[T]) Flush() 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 T) 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 SQLiteCache added in v0.0.36

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

func NewSQLiteCache added in v0.0.36

func NewSQLiteCache[T any](cacheType string) (*SQLiteCache[T], error)

func (*SQLiteCache[T]) BRPop added in v0.0.36

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

func (*SQLiteCache[T]) Del added in v0.0.36

func (c *SQLiteCache[T]) Del(key string) error

func (*SQLiteCache[T]) Exists added in v0.0.36

func (c *SQLiteCache[T]) Exists(key string) (bool, error)

func (*SQLiteCache[T]) Expire added in v0.0.36

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

func (*SQLiteCache[T]) Flush added in v0.0.36

func (c *SQLiteCache[T]) Flush() error

func (*SQLiteCache[T]) Get added in v0.0.36

func (c *SQLiteCache[T]) Get(key string) (T, error)

func (*SQLiteCache[T]) Incr added in v0.0.36

func (c *SQLiteCache[T]) Incr(key string) (int64, error)

func (*SQLiteCache[T]) IsInitialized added in v0.0.36

func (c *SQLiteCache[T]) IsInitialized() bool

func (*SQLiteCache[T]) RPush added in v0.0.36

func (c *SQLiteCache[T]) RPush(key string, value T) error

func (*SQLiteCache[T]) Set added in v0.0.36

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

type SQLiteCachePage added in v0.0.36

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

func NewSQLiteCachePage added in v0.0.36

func NewSQLiteCachePage[T any](name string) (*SQLiteCachePage[T], error)

NewSQLiteCachePage Create a new SQLite cache page with the given name.

func (*SQLiteCachePage[T]) Count added in v0.0.36

func (c *SQLiteCachePage[T]) Count(conditions map[string]any) (int64, error)

func (*SQLiteCachePage[T]) Find added in v0.0.36

func (c *SQLiteCachePage[T]) Find(page, size int64, conditions map[string]any, sorts ...PageSorter) (*PageCache[T], error)

func (*SQLiteCachePage[T]) Get added in v0.0.36

func (c *SQLiteCachePage[T]) Get(conditions map[string]any) (*T, error)

func (*SQLiteCachePage[T]) Put added in v0.0.36

func (c *SQLiteCachePage[T]) Put(value T) error

func (*SQLiteCachePage[T]) Update added in v0.0.36

func (c *SQLiteCachePage[T]) Update(conditions map[string]any, value *T) error

type Tool

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

Tool is a management tool for multi-level cache

func NewCacheTool

func NewCacheTool[T any](ctx context.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

type Type added in v0.0.33

type Type string
const (
	Memory Type = "memory"
	Redis  Type = "redis"
	JSON   Type = "json"
	Sqlite Type = "sqlite"
)

Jump to

Keyboard shortcuts

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