cache

package
v0.0.52 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 21 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 Agg added in v0.0.47

type Agg string
const (
	AggSum   Agg = "sum"
	AggAvg   Agg = "avg"
	AggMax   Agg = "max"
	AggMin   Agg = "min"
	AggCount Agg = "count"
	AggTotal Agg = "total"
)

type AggField added in v0.0.49

type AggField struct {
	Field string
	Agg   Agg
	Alias string
}

type Cache

type Cache[T any] interface {
	IsInitialized() bool
	Keys() ([]string, error)
	Items() map[string]T
	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]) Items added in v0.0.49

func (g *GoCache[T]) Items() map[string]T

func (*GoCache[T]) Keys added in v0.0.49

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

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 Granularity added in v0.0.44

type Granularity string
const (
	GranularityMinute    Granularity = "minute"
	GranularityHour      Granularity = "hour"
	GranularityDay       Granularity = "day"
	GranularityWeek      Granularity = "week"
	GranularityMonth     Granularity = "month"
	GranularityYear      Granularity = "year"
	Granularity5Minutes  Granularity = "5m"
	Granularity10Minutes Granularity = "10m"
	Granularity30Minutes Granularity = "30m"
)

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]) Items added in v0.0.49

func (c *JSONFileCache[T]) Items() map[string]T

func (*JSONFileCache[T]) Keys added in v0.0.49

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

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"`
}

func (*PageCache[T]) JSON added in v0.0.44

func (p *PageCache[T]) JSON() string

type PageGroupItem added in v0.0.49

type PageGroupItem struct {
	Group map[string]string  `json:"group"`
	Value map[string]float64 `json:"value"`
}

type PageSorter added in v0.0.36

type PageSorter struct {
	SortField string
	Asc       bool
	Expr      string // optional raw expression
}

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 PageTimeItem added in v0.0.44

type PageTimeItem struct {
	At    string             `json:"at"`
	Value map[string]float64 `json:"value"`
}

type Pager added in v0.0.44

type Pager[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
	Delete(conditions map[string]any) error
	GroupByTime(conditions map[string]any, from, to time.Time, granularity Granularity, agg Agg, fields ...string) ([]*PageTimeItem, error)
	GroupByFields(conditions map[string]any, groupFields []string, aggFields []AggField, page, size int64, sorts ...PageSorter) (*PageCache[PageGroupItem], error)
}

func NewPager added in v0.0.44

func NewPager[T any](ctx context.Context, t Type, args ...any) Pager[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
}

func GetRedisInstance

func GetRedisInstance[T any](ctx context.Context) *RedisCache[T]

func (*RedisCache[T]) AddDelayed added in v0.0.49

func (r *RedisCache[T]) AddDelayed(queue string, value T, score float64) error

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

func (r *RedisCache[T]) BRPop(timeout time.Duration, queue string) (value T, err error)

func (*RedisCache[T]) BRPopCtx added in v0.0.48

func (r *RedisCache[T]) BRPopCtx(ctx context.Context, timeout time.Duration, queue 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]) GetCtx added in v0.0.48

func (r *RedisCache[T]) GetCtx() context.Context

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]) Items added in v0.0.49

func (r *RedisCache[T]) Items() map[string]T

func (*RedisCache[T]) Keys added in v0.0.49

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

func (*RedisCache[T]) LPop added in v0.0.49

func (r *RedisCache[T]) LPop(queue string) (value T, err error)

func (*RedisCache[T]) PopDueDelayed added in v0.0.49

func (r *RedisCache[T]) PopDueDelayed(queue string, now float64, limit int) ([]T, error)

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

func (r *RedisCache[T]) RPush(queue 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
	PoolSize 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(sqliteTimeOut 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]) Items added in v0.0.49

func (c *SQLiteCache[T]) Items() map[string]T

func (*SQLiteCache[T]) Keys added in v0.0.49

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

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]) Delete added in v0.0.44

func (c *SQLiteCachePage[T]) Delete(conditions map[string]any) 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]) GroupByFields added in v0.0.49

func (c *SQLiteCachePage[T]) GroupByFields(
	conditions map[string]any,
	groupFields []string,
	aggFields []AggField,
	page, size int64,
	sorts ...PageSorter,
) (*PageCache[PageGroupItem], error)

func (*SQLiteCachePage[T]) GroupByTime added in v0.0.44

func (c *SQLiteCachePage[T]) GroupByTime(
	conditions map[string]any,
	from, to time.Time,
	granularity Granularity,
	agg Agg,
	fields ...string,
) ([]*PageTimeItem, 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