cache

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound = errors.New("key not found")
	ErrCacheClosed = errors.New("cache is closed")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string, dest interface{}) error
	Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
	Delete(ctx context.Context, key string) error
	Exists(ctx context.Context, key string) bool
	Clear(ctx context.Context) error
	Stats() CacheStats
	Close() error
}

Cache 缓存接口

type CacheStats

type CacheStats struct {
	Hits        int64   `json:"hits"`
	Misses      int64   `json:"misses"`
	Sets        int64   `json:"sets"`
	Deletes     int64   `json:"deletes"`
	Errors      int64   `json:"errors"`
	HitRatio    float64 `json:"hit_ratio"`
	TotalKeys   int     `json:"total_keys"`
	MemoryUsage int64   `json:"memory_usage_bytes"`
}

CacheStats 缓存统计信息

type MemoryCache

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

MemoryCache 内存缓存实现

func NewMemoryCache

func NewMemoryCache(config MemoryCacheConfig) *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) Delete

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

Delete 删除缓存值

func (*MemoryCache) Exists

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

Exists 检查key是否存在

func (*MemoryCache) Get

func (c *MemoryCache) Get(ctx context.Context, key string, dest interface{}) error

Get 获取缓存值

func (*MemoryCache) Set

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

Set 设置缓存值

func (*MemoryCache) Stats

func (c *MemoryCache) Stats() CacheStats

Stats 获取统计信息

type MemoryCacheConfig

type MemoryCacheConfig struct {
	MaxSize         int64         // 最大内存使用量(字节)
	CleanupInterval time.Duration // 清理间隔
}

MemoryCacheConfig 内存缓存配置

Jump to

Keyboard shortcuts

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