cache

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled   bool          // 是否启用缓存
	TTL       time.Duration // 默认缓存过期时间
	KeyPrefix string        // 缓存键前缀
}

Config 缓存配置

func NewConfig

func NewConfig() *Config

NewConfig 创建默认缓存配置

func (*Config) WithEnabled

func (c *Config) WithEnabled(enabled bool) *Config

WithEnabled 设置是否启用

func (*Config) WithKeyPrefix

func (c *Config) WithKeyPrefix(prefix string) *Config

WithKeyPrefix 设置键前缀

func (*Config) WithTTL

func (c *Config) WithTTL(ttl time.Duration) *Config

WithTTL 设置 TTL

type GoRedisAdapter

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

GoRedisAdapter go-redis v9 库的适配器实现

func (*GoRedisAdapter) Del

func (g *GoRedisAdapter) Del(ctx context.Context, keys ...string) (int64, error)

Del 删除缓存

func (*GoRedisAdapter) Exists

func (g *GoRedisAdapter) Exists(ctx context.Context, keys ...string) (int64, error)

Exists 检查缓存是否存在

func (*GoRedisAdapter) Get

func (g *GoRedisAdapter) Get(ctx context.Context, key string) (string, error)

Get 获取缓存

func (*GoRedisAdapter) Keys

func (g *GoRedisAdapter) Keys(ctx context.Context, pattern string) ([]string, error)

Keys 获取匹配的键

func (*GoRedisAdapter) Set

func (g *GoRedisAdapter) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

Set 设置缓存

type Handler

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

Handler 缓存处理器接口

type Manager

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

Manager 缓存管理器 - 提供线程安全的统计和管理功能

func NewManager

func NewManager(store Store) *Manager

NewManager 创建缓存管理器

func (*Manager) GetStats

func (cm *Manager) GetStats() Stats

GetStats 获取缓存统计 (线程安全)

func (*Manager) InvalidatePattern

func (cm *Manager) InvalidatePattern(ctx context.Context, pattern string) error

InvalidatePattern 使匹配模式的缓存失效 (线程安全)

func (*Manager) RecordHit

func (cm *Manager) RecordHit()

RecordHit 记录缓存命中 (线程安全)

func (*Manager) RecordMiss

func (cm *Manager) RecordMiss()

RecordMiss 记录缓存未命中 (线程安全)

func (*Manager) ResetStats

func (cm *Manager) ResetStats()

ResetStats 重置统计 (线程安全)

type MockStore

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

MockStore 模拟缓存存储实现(用于开发和测试)

func NewMockStore

func NewMockStore() *MockStore

NewMockStore 创建模拟缓存存储

func (*MockStore) Clear

func (m *MockStore) Clear(ctx context.Context, prefix string) error

Clear 清除所有缓存(按前缀)

func (*MockStore) Delete

func (m *MockStore) Delete(ctx context.Context, key string) error

Delete 删除缓存

func (*MockStore) Exists

func (m *MockStore) Exists(ctx context.Context, key string) (bool, error)

Exists 检查缓存是否存在

func (*MockStore) Get

func (m *MockStore) Get(ctx context.Context, key string) (string, error)

Get 获取缓存

func (*MockStore) GetStats

func (m *MockStore) GetStats() map[string]interface{}

GetStats 获取缓存统计信息

func (*MockStore) Set

func (m *MockStore) Set(ctx context.Context, key string, value string, ttl time.Duration) error

Set 设置缓存

type NoCacheHandler

type NoCacheHandler struct{}

NoCacheHandler 无缓存实现 - 所有操作都直接返回

func (*NoCacheHandler) Close

func (n *NoCacheHandler) Close() error

func (*NoCacheHandler) Del

func (n *NoCacheHandler) Del(keys ...[]byte) error

func (*NoCacheHandler) Exists

func (n *NoCacheHandler) Exists(key []byte) (bool, error)

func (*NoCacheHandler) Get

func (n *NoCacheHandler) Get(key []byte) ([]byte, error)

func (*NoCacheHandler) Set

func (n *NoCacheHandler) Set(key, value []byte, ttl time.Duration) error

func (*NoCacheHandler) SetWithTTL

func (n *NoCacheHandler) SetWithTTL(key, value []byte, ttl time.Duration) error

type RedisClientInterface

type RedisClientInterface interface {
	Get(ctx context.Context, key string) (string, error)
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
	Del(ctx context.Context, keys ...string) (int64, error)
	Exists(ctx context.Context, keys ...string) (int64, error)
	Keys(ctx context.Context, pattern string) ([]string, error)
}

RedisClientInterface Redis 客户端接口 - 支持多种 Redis 库的适配

func NewGoRedisAdapter

func NewGoRedisAdapter(redisClient interface{}) (RedisClientInterface, error)

NewGoRedisAdapter 创建 go-redis 适配器 使用方式: adapter := NewGoRedisAdapter(redisClient) 注意:这是一个模板实现,需要根据实际的 redis 库版本完成具体方法

type RedisStore

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

RedisStore Redis 缓存存储实现

func NewRedisStore

func NewRedisStore(client RedisClientInterface, prefix string) *RedisStore

NewRedisStore 创建 Redis 缓存存储

func (*RedisStore) Clear

func (r *RedisStore) Clear(ctx context.Context, prefix string) error

Clear 清除所有缓存(按前缀)

func (*RedisStore) Delete

func (r *RedisStore) Delete(ctx context.Context, key string) error

Delete 删除缓存

func (*RedisStore) Exists

func (r *RedisStore) Exists(ctx context.Context, key string) (bool, error)

Exists 检查缓存是否存在

func (*RedisStore) Get

func (r *RedisStore) Get(ctx context.Context, key string) (string, error)

Get 获取缓存

func (*RedisStore) Set

func (r *RedisStore) Set(ctx context.Context, key string, value string, ttl time.Duration) error

Set 设置缓存

type Stats

type Stats struct {
	TotalHits   int64         // 总命中数
	TotalMisses int64         // 总未命中数
	HitRate     float64       // 命中率
	AvgTTL      time.Duration // 平均 TTL
}

Stats 缓存统计信息

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 缓存存储接口 - 定义所有缓存操作的合约

Jump to

Keyboard shortcuts

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