cache

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnsafeBytes added in v0.1.1

func UnsafeBytes(s string) []byte

UnsafeBytes 字符串转字节切片(零拷贝)

func UnsafeString added in v0.1.1

func UnsafeString(b []byte) string

UnsafeString 字节切片转字符串(零拷贝)

Types

type Cacheable added in v0.1.1

type Cacheable interface {
	CacheKey() string        // 缓存键
	CacheTTL() time.Duration // 缓存过期时间
}

Cacheable 可缓存的模型接口

type Handler

type Handler interface {
	Get(key []byte) ([]byte, error)
	Set(key, value []byte) error
	SetWithTTL(key, value []byte, ttl time.Duration) error
	Del(keys ...[]byte) error
	Exists(key []byte) (bool, error)
}

Handler 缓存处理器接口(字节级别操作)

type Store

type Store interface {
	// Get 获取缓存值
	Get(ctx context.Context, key string) (string, error)

	// Set 设置缓存值
	Set(ctx context.Context, key string, value string, ttl time.Duration) error

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

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

	// Clear 清除所有缓存(按前缀)
	Clear(ctx context.Context, prefix string) error
}

Store 缓存存储接口

type Wrapper added in v0.1.1

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

Wrapper 缓存包装器,用于 Repository 层的缓存操作

func NewWrapper added in v0.1.1

func NewWrapper(store Store, keyPrefix string) *Wrapper

NewWrapper 创建缓存包装器

func (*Wrapper) Clear added in v0.1.1

func (w *Wrapper) Clear(ctx context.Context, prefix string) error

Clear 清除指定前缀的所有缓存

func (*Wrapper) Delete added in v0.1.1

func (w *Wrapper) Delete(ctx context.Context, keys ...string) error

Delete 删除缓存

func (*Wrapper) DeleteByModels added in v0.1.1

func (w *Wrapper) DeleteByModels(ctx context.Context, models ...Cacheable) error

DeleteByModels 根据可缓存模型删除缓存

func (*Wrapper) Exists added in v0.1.1

func (w *Wrapper) Exists(ctx context.Context, key string) (bool, error)

Exists 检查缓存是否存在

func (*Wrapper) Get added in v0.1.1

func (w *Wrapper) Get(ctx context.Context, key string, target interface{}) (bool, error)

Get 从缓存获取并反序列化到目标对象

func (*Wrapper) GetBytes added in v0.1.1

func (w *Wrapper) GetBytes(ctx context.Context, key string) ([]byte, error)

GetBytes 获取原始字节数据

func (*Wrapper) GetOrLoad added in v0.1.1

func (w *Wrapper) GetOrLoad(ctx context.Context, key string, ttl time.Duration, target interface{}, loadFn func() (interface{}, error)) error

GetOrLoad 获取缓存,如果不存在则执行加载函数

func (*Wrapper) Set added in v0.1.1

func (w *Wrapper) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set 序列化并设置缓存

func (*Wrapper) SetBytes added in v0.1.1

func (w *Wrapper) SetBytes(ctx context.Context, key string, value []byte, ttl time.Duration) error

SetBytes 设置原始字节数据

Jump to

Keyboard shortcuts

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