Documentation
¶
Index ¶
- Variables
- func Del(key string) error
- func DelCtx(ctx context.Context, key string) error
- func Exists(key string) (bool, error)
- func ExistsCtx(ctx context.Context, key string) (bool, error)
- func Expire(key string, expiration time.Duration) error
- func ExpireCtx(ctx context.Context, key string, expiration time.Duration) error
- func Get[T any](key string) (T, error)
- func GetCtx[T any](ctx context.Context, key string) (T, error)
- func GetOrDefault[T any](key string, defValue T) T
- func GetOrSet[T any](key string, expiration time.Duration, defaultVal T) (T, error)
- func Incr(key string) (int64, error)
- func IncrCtx(ctx context.Context, key string) (int64, error)
- func Lock(ctx context.Context, key string, ttl time.Duration, ...) (unlock func(), err error)
- func Remember[T any](key string, expiration time.Duration, fallback func() (T, error)) (T, error)
- func RememberCtx[T any](ctx context.Context, key string, expiration time.Duration, ...) (T, error)
- func RememberWithCache[T any](c Cache[T], key string, expiration time.Duration, fallback func() (T, error)) (T, error)
- func RestRedisInstance()
- func Set[T any](key string, value T, expiration time.Duration) error
- func SetCtx[T any](ctx context.Context, key string, value T, expiration time.Duration) error
- func TryLock(ctx context.Context, key string, ttl time.Duration) (unlock func(), ok bool, err error)
- func WithLock(ctx context.Context, key string, ttl time.Duration, fn func() error) error
- func WithLockTimeout(ctx context.Context, key string, ttl, timeout time.Duration, fn func() error) error
- type Agg
- type AggField
- type Cache
- type GoCache
- func (g *GoCache[T]) BRPop(timeout time.Duration, key string) (T, error)
- func (g *GoCache[T]) Del(key string) error
- func (g *GoCache[T]) Exists(key string) (bool, error)
- func (g *GoCache[T]) Expire(key string, expiration time.Duration) error
- func (g *GoCache[T]) Flush() error
- func (g *GoCache[T]) Get(key string) (T, error)
- func (g *GoCache[T]) Incr(key string) (int64, error)
- func (g *GoCache[T]) IsInitialized() bool
- func (g *GoCache[T]) Items() map[string]T
- func (g *GoCache[T]) Keys() ([]string, error)
- func (g *GoCache[T]) RPush(key string, value T) error
- func (g *GoCache[T]) Set(key string, value T, expiration time.Duration) error
- type Granularity
- type JSONFileCache
- func (c *JSONFileCache[T]) BRPop(timeout time.Duration, key string) (value T, err error)
- func (c *JSONFileCache[T]) Del(key string) error
- func (c *JSONFileCache[T]) Exists(key string) (bool, error)
- func (c *JSONFileCache[T]) Expire(key string, expiration time.Duration) error
- func (c *JSONFileCache[T]) Flush() error
- func (c *JSONFileCache[T]) Get(key string) (T, error)
- func (c *JSONFileCache[T]) Incr(key string) (int64, error)
- func (c *JSONFileCache[T]) IsInitialized() bool
- func (c *JSONFileCache[T]) Items() map[string]T
- func (c *JSONFileCache[T]) Keys() ([]string, error)
- func (c *JSONFileCache[T]) RPush(key string, value T) error
- func (c *JSONFileCache[T]) Set(key string, value T, expiration time.Duration) error
- type LoaderFunc
- type PageCache
- type PageGroupItem
- type PageSorter
- type PageTimeItem
- type Pager
- type Queue
- type RedisCache
- func (r *RedisCache[T]) AddDelayed(queue string, value T, score float64) error
- func (r *RedisCache[T]) BLPop(timeout time.Duration, queue string) (value T, err error)
- func (r *RedisCache[T]) BLPopCtx(ctx context.Context, timeout time.Duration, queue string) (value T, err error)
- func (r *RedisCache[T]) BRPop(timeout time.Duration, queue string) (value T, err error)
- func (r *RedisCache[T]) BRPopCtx(ctx context.Context, timeout time.Duration, queue string) (value T, err error)
- func (r *RedisCache[T]) Del(key string) error
- func (r *RedisCache[T]) Exists(key string) (bool, error)
- func (r *RedisCache[T]) Expire(key string, expiration time.Duration) error
- func (r *RedisCache[T]) Flush() error
- func (r *RedisCache[T]) Get(key string) (T, error)
- func (r *RedisCache[T]) GetCtx() context.Context
- func (r *RedisCache[T]) Incr(key string) (int64, error)
- func (r *RedisCache[T]) IsInitialized() bool
- func (r *RedisCache[T]) Items() map[string]T
- func (r *RedisCache[T]) Keys() ([]string, error)
- func (r *RedisCache[T]) LPop(queue string) (value T, err error)
- func (r *RedisCache[T]) PopDueDelayed(queue string, now float64, limit int) ([]T, error)
- func (r *RedisCache[T]) RPush(queue string, value T) error
- func (r *RedisCache[T]) Set(key string, value T, expiration time.Duration) error
- type RedisConfig
- type SQLiteCache
- func (c *SQLiteCache[T]) BRPop(sqliteTimeOut time.Duration, key string) (T, error)
- func (c *SQLiteCache[T]) Del(key string) error
- func (c *SQLiteCache[T]) Exists(key string) (bool, error)
- func (c *SQLiteCache[T]) Expire(key string, expiration time.Duration) error
- func (c *SQLiteCache[T]) Flush() error
- func (c *SQLiteCache[T]) Get(key string) (T, error)
- func (c *SQLiteCache[T]) Incr(key string) (int64, error)
- func (c *SQLiteCache[T]) IsInitialized() bool
- func (c *SQLiteCache[T]) Items() map[string]T
- func (c *SQLiteCache[T]) Keys() ([]string, error)
- func (c *SQLiteCache[T]) RPush(key string, value T) error
- func (c *SQLiteCache[T]) Set(key string, value T, expiration time.Duration) error
- type SQLiteCachePage
- func (c *SQLiteCachePage[T]) Count(conditions map[string]any) (int64, error)
- func (c *SQLiteCachePage[T]) Delete(conditions map[string]any) error
- func (c *SQLiteCachePage[T]) Find(page, size int64, conditions map[string]any, sorts ...PageSorter) (*PageCache[T], error)
- func (c *SQLiteCachePage[T]) Get(conditions map[string]any) (*T, error)
- func (c *SQLiteCachePage[T]) GroupByFields(conditions map[string]any, groupFields []string, aggFields []AggField, ...) (*PageCache[PageGroupItem], error)
- func (c *SQLiteCachePage[T]) GroupByTime(conditions map[string]any, from, to time.Time, granularity Granularity, ...) ([]*PageTimeItem, error)
- func (c *SQLiteCachePage[T]) Put(value T) error
- func (c *SQLiteCachePage[T]) Update(conditions map[string]any, value *T) error
- type Tool
- type Type
Constants ¶
This section is empty.
Variables ¶
var ErrCacheMiss = errors.New("cache miss")
ErrCacheMiss indicates a cache miss error
var ErrLockAcquireFailed = errors.New("failed to acquire distributed lock")
ErrLockAcquireFailed indicates failure to acquire lock
var SimpleTool = func(ctx *gin.Context) *Tool[any] { return NewCacheTool[any](ctx, defaultCaches, nil) }
SimpleTool returns a multi-level cache tool (L1 In-Memory + L2 Redis if available).
Functions ¶
func GetOrDefault ¶
GetOrDefault retrieves a value from the default cache or returns defValue on error/miss.
func Lock ¶
func Lock(ctx context.Context, key string, ttl time.Duration, retryInterval, timeout time.Duration) (unlock func(), err error)
Lock blocks until lock is acquired or timeout is reached.
func Remember ¶
Remember retrieves data from the default cache. If cache misses, calls fallback, caches the result with Singleflight protection to prevent cache stampedes, and returns it.
func RememberCtx ¶
func RememberCtx[T any](ctx context.Context, key string, expiration time.Duration, fallback func() (T, error)) (T, error)
RememberCtx retrieves data from the default cache with context and Singleflight protection.
func RememberWithCache ¶
func RememberWithCache[T any](c Cache[T], key string, expiration time.Duration, fallback func() (T, error)) (T, error)
RememberWithCache retrieves data from the specified cache with Singleflight protection.
func RestRedisInstance ¶
func RestRedisInstance()
func TryLock ¶
func TryLock(ctx context.Context, key string, ttl time.Duration) (unlock func(), ok bool, err error)
TryLock attempts to acquire a lock on key immediately without blocking. Returns an unlock function, a boolean indicating if lock was acquired, and any error encountered.
Types ¶
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 GetDefaultCache ¶
GetDefaultCache returns the default cache instance for type T. If Redis is configured and initialized, it returns the Redis cache. Otherwise, it returns the shared in-memory cache.
type GoCache ¶
type GoCache[T any] struct { // contains filtered or unexported fields }
func NewGoCache ¶
func NewSharedGoCache ¶
func (*GoCache[T]) IsInitialized ¶
type Granularity ¶
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 ¶
type JSONFileCache[T any] struct { // contains filtered or unexported fields }
func NewJSONCache ¶
func NewJSONCache[T any](cacheType string) (*JSONFileCache[T], error)
func (*JSONFileCache[T]) BRPop ¶
func (c *JSONFileCache[T]) BRPop(timeout time.Duration, key string) (value T, err error)
func (*JSONFileCache[T]) Del ¶
func (c *JSONFileCache[T]) Del(key string) error
func (*JSONFileCache[T]) Expire ¶
func (c *JSONFileCache[T]) Expire(key string, expiration time.Duration) error
func (*JSONFileCache[T]) Flush ¶
func (c *JSONFileCache[T]) Flush() error
func (*JSONFileCache[T]) Get ¶
func (c *JSONFileCache[T]) Get(key string) (T, error)
func (*JSONFileCache[T]) IsInitialized ¶
func (c *JSONFileCache[T]) IsInitialized() bool
func (*JSONFileCache[T]) Items ¶
func (c *JSONFileCache[T]) Items() map[string]T
func (*JSONFileCache[T]) Keys ¶
func (c *JSONFileCache[T]) Keys() ([]string, error)
func (*JSONFileCache[T]) RPush ¶
func (c *JSONFileCache[T]) RPush(key string, value T) error
type LoaderFunc ¶
LoaderFunc is a function type for loading data from an external source
type PageCache ¶
type PageGroupItem ¶
type PageSorter ¶
func PageSorterAsc ¶
func PageSorterAsc(field string) PageSorter
func PageSorterDesc ¶
func PageSorterDesc(field string) PageSorter
type PageTimeItem ¶
type Pager ¶
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) }
type RedisCache ¶
func GetRedisInstance ¶
func GetRedisInstance[T any](ctx context.Context) *RedisCache[T]
func (*RedisCache[T]) AddDelayed ¶
func (r *RedisCache[T]) AddDelayed(queue string, value T, score float64) error
func (*RedisCache[T]) BLPop ¶
func (r *RedisCache[T]) BLPop(timeout time.Duration, queue string) (value T, err error)
func (*RedisCache[T]) BLPopCtx ¶
func (r *RedisCache[T]) BLPopCtx(ctx context.Context, timeout time.Duration, queue string) (value T, err error)
BLPopCtx consumes the oldest item from a Redis list. It is kept separate from BRPopCtx because RPUSH + BRPOP has LIFO semantics, while cache queues and the message broker promise FIFO delivery.
func (*RedisCache[T]) BRPop ¶
func (r *RedisCache[T]) BRPop(timeout time.Duration, queue string) (value T, err error)
func (*RedisCache[T]) Del ¶
func (r *RedisCache[T]) Del(key string) error
func (*RedisCache[T]) Expire ¶
func (r *RedisCache[T]) Expire(key string, expiration time.Duration) error
func (*RedisCache[T]) Flush ¶
func (r *RedisCache[T]) Flush() error
func (*RedisCache[T]) Get ¶
func (r *RedisCache[T]) Get(key string) (T, error)
func (*RedisCache[T]) GetCtx ¶
func (r *RedisCache[T]) GetCtx() context.Context
func (*RedisCache[T]) IsInitialized ¶
func (r *RedisCache[T]) IsInitialized() bool
func (*RedisCache[T]) Items ¶
func (r *RedisCache[T]) Items() map[string]T
func (*RedisCache[T]) Keys ¶
func (r *RedisCache[T]) Keys() ([]string, error)
func (*RedisCache[T]) LPop ¶
func (r *RedisCache[T]) LPop(queue string) (value T, err error)
func (*RedisCache[T]) PopDueDelayed ¶
func (r *RedisCache[T]) PopDueDelayed(queue string, now float64, limit int) ([]T, error)
func (*RedisCache[T]) RPush ¶
func (r *RedisCache[T]) RPush(queue string, value T) error
type RedisConfig ¶
RedisConfig holds the Redis configuration parameters
type SQLiteCache ¶
type SQLiteCache[T any] struct { // contains filtered or unexported fields }
func NewSQLiteCache ¶
func NewSQLiteCache[T any](cacheType string) (*SQLiteCache[T], error)
func (*SQLiteCache[T]) BRPop ¶
func (c *SQLiteCache[T]) BRPop(sqliteTimeOut time.Duration, key string) (T, error)
func (*SQLiteCache[T]) Del ¶
func (c *SQLiteCache[T]) Del(key string) error
func (*SQLiteCache[T]) Expire ¶
func (c *SQLiteCache[T]) Expire(key string, expiration time.Duration) error
func (*SQLiteCache[T]) Flush ¶
func (c *SQLiteCache[T]) Flush() error
func (*SQLiteCache[T]) Get ¶
func (c *SQLiteCache[T]) Get(key string) (T, error)
func (*SQLiteCache[T]) IsInitialized ¶
func (c *SQLiteCache[T]) IsInitialized() bool
func (*SQLiteCache[T]) Items ¶
func (c *SQLiteCache[T]) Items() map[string]T
func (*SQLiteCache[T]) Keys ¶
func (c *SQLiteCache[T]) Keys() ([]string, error)
func (*SQLiteCache[T]) RPush ¶
func (c *SQLiteCache[T]) RPush(key string, value T) error
type SQLiteCachePage ¶
type SQLiteCachePage[T any] struct { // contains filtered or unexported fields }
func NewSQLiteCachePage ¶
func NewSQLiteCachePage[T any](name string) (*SQLiteCachePage[T], error)
NewSQLiteCachePage Create a new SQLite cache page with the given name.
func (*SQLiteCachePage[T]) Count ¶
func (c *SQLiteCachePage[T]) Count(conditions map[string]any) (int64, error)
func (*SQLiteCachePage[T]) Delete ¶
func (c *SQLiteCachePage[T]) Delete(conditions map[string]any) error
func (*SQLiteCachePage[T]) Find ¶
func (c *SQLiteCachePage[T]) Find(page, size int64, conditions map[string]any, sorts ...PageSorter) (*PageCache[T], error)
func (*SQLiteCachePage[T]) Get ¶
func (c *SQLiteCachePage[T]) Get(conditions map[string]any) (*T, error)
func (*SQLiteCachePage[T]) GroupByFields ¶
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 ¶
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 ¶
func (c *SQLiteCachePage[T]) Put(value T) error
type Tool ¶
Tool is a management tool for multi-level cache
func NewCacheTool ¶
NewCacheTool creates a new Tool instance
func (*Tool[T]) Get ¶
Get retrieves data from the cache, searching each level in order, and loads from the loader if all levels miss