cache

package
v0.0.0-...-dabeb0a Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound 缓存未找到错误
	ErrNotFound = errors.New("cache: key not found")

	// ErrKeyExists 缓存键已存在错误
	ErrKeyExists = errors.New("cache: key already exists")
)
View Source
var DefaultOptions = Options{
	Expiration: 5 * time.Minute,
	MaxEntries: 10000,
}

DefaultOptions 默认缓存选项

Functions

func WithRedisClient

func WithRedisClient(client *redis.Client) func(*RedisOptions)

WithRedisClient 设置Redis客户端

func WithRedisKeyPrefix

func WithRedisKeyPrefix(prefix string) func(*RedisOptions)

WithRedisKeyPrefix 设置Redis键前缀

Types

type Cache

type Cache interface {
	// Get 获取缓存
	Get(ctx context.Context, key string) ([]byte, error)

	// Set 设置缓存
	Set(ctx context.Context, key string, value []byte, expiration time.Duration) error

	// Delete 删除缓存
	Delete(ctx context.Context, key string) error

	// Clear 清空缓存
	Clear(ctx context.Context) error

	// GetMulti 批量获取缓存
	GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

	// SetMulti 批量设置缓存
	SetMulti(ctx context.Context, items map[string][]byte, expiration time.Duration) error

	// DeleteMulti 批量删除缓存
	DeleteMulti(ctx context.Context, keys []string) error

	// Incr 自增
	Incr(ctx context.Context, key string, delta int64) (int64, error)

	// Decr 自减
	Decr(ctx context.Context, key string, delta int64) (int64, error)

	// Exists 检查缓存是否存在
	Exists(ctx context.Context, key string) (bool, error)

	// Expire 设置过期时间
	Expire(ctx context.Context, key string, expiration time.Duration) error

	// TTL 获取过期时间
	TTL(ctx context.Context, key string) (time.Duration, error)

	// Close 关闭缓存
	Close() error
}

Cache 缓存接口

type CacheType

type CacheType string

CacheType 缓存类型

const (
	// Memory 内存缓存
	Memory CacheType = "memory"
	// Redis Redis缓存
	Redis CacheType = "redis"
)

type Factory

type Factory struct {
	// contains filtered or unexported fields
}

Factory 缓存工厂

func GetFactory

func GetFactory() *Factory

GetFactory 获取缓存工厂实例

func (*Factory) Close

func (f *Factory) Close() error

Close 关闭所有缓存

func (*Factory) CloseWithContext

func (f *Factory) CloseWithContext(ctx context.Context) error

CloseWithContext 使用上下文关闭所有缓存

func (*Factory) Create

func (f *Factory) Create(name string, cacheType CacheType, opts ...Option) (Cache, error)

Create 创建缓存

func (*Factory) CreateRedis

func (f *Factory) CreateRedis(name string, client *redis.Client, opts ...Option) (Cache, error)

CreateRedis 创建Redis缓存

func (*Factory) CreateRedisWithContext

func (f *Factory) CreateRedisWithContext(ctx context.Context, name string, client *redis.Client, opts ...Option) (Cache, error)

CreateRedisWithContext 使用上下文创建Redis缓存

func (*Factory) CreateWithContext

func (f *Factory) CreateWithContext(ctx context.Context, name string, cacheType CacheType, opts ...Option) (Cache, error)

CreateWithContext 使用上下文创建缓存

func (*Factory) Delete

func (f *Factory) Delete(name string) error

Delete 删除缓存

func (*Factory) DeleteWithContext

func (f *Factory) DeleteWithContext(ctx context.Context, name string) error

DeleteWithContext 使用上下文删除缓存

func (*Factory) Get

func (f *Factory) Get(name string) (Cache, error)

Get 获取缓存

func (*Factory) GetWithContext

func (f *Factory) GetWithContext(ctx context.Context, name string) (Cache, error)

GetWithContext 使用上下文获取缓存

type MemoryCache

type MemoryCache struct {
	// contains filtered or unexported fields
}

MemoryCache 内存缓存

func NewMemoryCache

func NewMemoryCache(opts ...Option) *MemoryCache

NewMemoryCache 创建内存缓存

func (*MemoryCache) Clear

func (c *MemoryCache) Clear(ctx context.Context) error

Clear 清空缓存

func (*MemoryCache) Close

func (c *MemoryCache) Close() error

Close 关闭缓存

func (*MemoryCache) Decr

func (c *MemoryCache) Decr(ctx context.Context, key string, delta int64) (int64, error)

Decr 自减

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(ctx context.Context, key string) error

Delete 删除缓存

func (*MemoryCache) DeleteMulti

func (c *MemoryCache) DeleteMulti(ctx context.Context, keys []string) error

DeleteMulti 批量删除缓存

func (*MemoryCache) Exists

func (c *MemoryCache) Exists(ctx context.Context, key string) (bool, error)

Exists 检查缓存是否存在

func (*MemoryCache) Expire

func (c *MemoryCache) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire 设置过期时间

func (*MemoryCache) Get

func (c *MemoryCache) Get(ctx context.Context, key string) ([]byte, error)

Get 获取缓存

func (*MemoryCache) GetMulti

func (c *MemoryCache) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

GetMulti 批量获取缓存

func (*MemoryCache) Incr

func (c *MemoryCache) Incr(ctx context.Context, key string, delta int64) (int64, error)

Incr 自增

func (*MemoryCache) Set

func (c *MemoryCache) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error

Set 设置缓存

func (*MemoryCache) SetMulti

func (c *MemoryCache) SetMulti(ctx context.Context, items map[string][]byte, expiration time.Duration) error

SetMulti 批量设置缓存

func (*MemoryCache) TTL

func (c *MemoryCache) TTL(ctx context.Context, key string) (time.Duration, error)

TTL 获取过期时间

type Option

type Option func(*Options)

Option 缓存选项函数

func WithExpiration

func WithExpiration(expiration time.Duration) Option

WithExpiration 设置默认过期时间

func WithKeyPrefix

func WithKeyPrefix(prefix string) Option

WithKeyPrefix 设置键前缀

func WithMaxEntries

func WithMaxEntries(maxEntries int) Option

WithMaxEntries 设置最大缓存条目数

func WithOnEvicted

func WithOnEvicted(onEvicted func(key string, value []byte)) Option

WithOnEvicted 设置缓存淘汰回调函数

type Options

type Options struct {
	// Expiration 默认过期时间
	Expiration time.Duration

	// MaxEntries 最大缓存条目数
	MaxEntries int

	// OnEvicted 缓存淘汰回调函数
	OnEvicted func(key string, value []byte)
}

Options 缓存选项

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions 创建缓存选项

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache Redis缓存实现

func NewRedisCache

func NewRedisCache(client *redis.Client, opts ...Option) *RedisCache

NewRedisCache 创建Redis缓存

func (*RedisCache) Clear

func (c *RedisCache) Clear(ctx context.Context) error

Clear 清空缓存(谨慎使用)

func (*RedisCache) Close

func (c *RedisCache) Close() error

Close 关闭缓存

func (*RedisCache) Decr

func (c *RedisCache) Decr(ctx context.Context, key string, delta int64) (int64, error)

Decr 自减

func (*RedisCache) Delete

func (c *RedisCache) Delete(ctx context.Context, key string) error

Delete 删除缓存

func (*RedisCache) DeleteMulti

func (c *RedisCache) DeleteMulti(ctx context.Context, keys []string) error

DeleteMulti 批量删除缓存

func (*RedisCache) Exists

func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)

Exists 检查缓存是否存在

func (*RedisCache) Expire

func (c *RedisCache) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire 设置过期时间

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)

Get 获取缓存

func (*RedisCache) GetMulti

func (c *RedisCache) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

GetMulti 批量获取缓存

func (*RedisCache) GetWithBloomFilter

func (c *RedisCache) GetWithBloomFilter(ctx context.Context, key string, loader func(ctx context.Context) (any, error), bloomFilterKey string, expiration time.Duration) (any, error)

GetWithBloomFilter 使用布隆过滤器防止缓存穿透 key: 缓存键 loader: 加载数据的函数,当缓存未命中时调用 bloomFilterKey: 布隆过滤器的键 expiration: 缓存过期时间,为0时使用默认过期时间

func (*RedisCache) GetWithProtection

func (c *RedisCache) GetWithProtection(ctx context.Context, key string, loader func(ctx context.Context) (any, error), expiration time.Duration) (any, error)

GetWithProtection 获取缓存,带穿透保护 key: 缓存键 loader: 加载数据的函数,当缓存未命中时调用 expiration: 缓存过期时间,为0时使用默认过期时间

func (*RedisCache) Incr

func (c *RedisCache) Incr(ctx context.Context, key string, delta int64) (int64, error)

Incr 自增

func (*RedisCache) IsNilValue

func (c *RedisCache) IsNilValue(ctx context.Context, data []byte) bool

IsNilValue 检查是否为空值缓存

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error

Set 设置缓存

func (*RedisCache) SetMulti

func (c *RedisCache) SetMulti(ctx context.Context, items map[string][]byte, expiration time.Duration) error

SetMulti 批量设置缓存

func (*RedisCache) SetNilValue

func (c *RedisCache) SetNilValue(ctx context.Context, key string, expiration time.Duration) error

SetNilValue 设置空值缓存,用于缓存穿透保护 当查询结果为空时,缓存一个特殊的空值,避免频繁查询数据库

func (*RedisCache) TTL

func (c *RedisCache) TTL(ctx context.Context, key string) (time.Duration, error)

TTL 获取过期时间

func (*RedisCache) WarmUp

func (c *RedisCache) WarmUp(ctx context.Context, keys []string, loader func(ctx context.Context, key string) (any, error)) error

WarmUp 缓存预热 keys: 需要预热的键列表 loader: 加载数据的函数,接收键并返回对应的值

type RedisOptions

type RedisOptions struct {
	Client    *redis.Client // Redis客户端
	KeyPrefix string        // 键前缀
}

RedisOptions Redis缓存选项

Jump to

Keyboard shortcuts

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