cache

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2025 License: MIT Imports: 13 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

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

ErrCacheMiss 表示缓存未命中的错误

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 是一个通用的缓存接口,定义了基本的缓存操作

type GoCache

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

GoCache 是 go-cache 的实现,支持泛型,并使用通道进行阻塞操作

func NewGoCache

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

NewGoCache 创建一个新的 GoCache 实例 defaultExpiration: 默认过期时间 cleanupInterval: 清理间隔

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

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

BRPop 从 go-cache 中弹出数据,支持阻塞和超时

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

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

Del 从 go-cache 中删除数据

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

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

Exists 检查 key 是否存在

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

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

Expire 设置 key 的过期时间

func (*GoCache[T]) Get

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

Get 从 go-cache 中获取数据

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

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

Incr 递增 key 的值

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

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

IsInitialized 检查缓存是否已初始化

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

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

RPush 将数据存入 go-cache

func (*GoCache[T]) Set

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

Set 将数据存入 go-cache

type LoaderFunc

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

LoaderFunc 是一个用于从外部源加载数据的函数类型

type Queue added in v0.0.11

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

Queue 定义一个泛型队列

type RedisCache

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

RedisCache 是 Redis 的实现

var (
	RedisInstance *RedisCache[any]
)

func GetRedisInstance

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

GetRedisInstance 返回单例的 RedisCache 实例

func NewRedisCache

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

NewRedisCache 创建一个新的 RedisCache 实例

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

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

BRPop 从 Redis 中弹出数据 timeout 为超时时间 key 为队列名

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

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

Del 从 Redis 中删除数据

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

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

Exists 检查 Redis 中是否存在指定 key

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

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

Expire 设置 Redis 中 key 的过期时间

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)

Incr 递增 Redis 中的值

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

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

IsInitialized bool

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

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

RPush 将数据存入 Redis

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 是多级缓存的管理工具

func NewCacheTool

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

NewCacheTool 创建一个新的 Tool 实例

func (*Tool[T]) Delete

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

Delete 从所有缓存层中删除数据

func (*Tool[T]) Get

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

Get 从缓存中获取数据,依次查找各级缓存,如果都未命中则通过 loader 加载

func (*Tool[T]) Set

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

Set 将数据存入所有缓存层

Jump to

Keyboard shortcuts

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