cache

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNil = redis.Nil

ErrNil 表示 key 不存在

Functions

This section is empty.

Types

type Cache

type Cache 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) error
	FlushDB(ctx context.Context) error
	DBSize(ctx context.Context) (int64, error)
	Info(ctx context.Context) (string, error)
	Keys(ctx context.Context, pattern string) ([]string, error)
	// Incr 原子自增,常用于计数器/限速窗口。Key 不存在时从 0 开始并返回 1。
	// Incr 本身不设置 TTL;首次调用后请配合 Expire 建立过期窗口。
	Incr(ctx context.Context, key string) (int64, error)
	// Expire 为已存在的 key 设置 TTL;key 不存在时返回 nil(不是错误)。
	Expire(ctx context.Context, key string, ttl time.Duration) error
}

Cache 缓存抽象接口

type MemoryCache

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

func NewMemoryCache

func NewMemoryCache() *MemoryCache

func (*MemoryCache) Close

func (m *MemoryCache) Close()

func (*MemoryCache) DBSize

func (m *MemoryCache) DBSize(_ context.Context) (int64, error)

func (*MemoryCache) Del

func (m *MemoryCache) Del(_ context.Context, keys ...string) error

func (*MemoryCache) Expire added in v1.6.1

func (m *MemoryCache) Expire(_ context.Context, key string, ttl time.Duration) error

Expire 为已存在的 key 设置 TTL;key 不存在返回 nil(与 Redis Expire 命令对齐,仅以 error 为异常)。

func (*MemoryCache) FlushDB

func (m *MemoryCache) FlushDB(_ context.Context) error

func (*MemoryCache) Get

func (m *MemoryCache) Get(_ context.Context, key string) (string, error)

func (*MemoryCache) Incr added in v1.6.1

func (m *MemoryCache) Incr(_ context.Context, key string) (int64, error)

Incr 原子自增;过期后再 Incr 视为从 0 开始。 注意:当前实现不保留 TTL(与 Redis INCR 的语义一致:INCR 不改过期时间); 首次调用后请显式 Expire 建立窗口。

func (*MemoryCache) Info

func (m *MemoryCache) Info(_ context.Context) (string, error)

func (*MemoryCache) Keys

func (m *MemoryCache) Keys(_ context.Context, pattern string) ([]string, error)

func (*MemoryCache) Set

func (m *MemoryCache) Set(_ context.Context, key string, value interface{}, expiration time.Duration) error

type RedisCache

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

func NewRedisCache

func NewRedisCache(client *redis.Client) *RedisCache

func (*RedisCache) DBSize

func (r *RedisCache) DBSize(ctx context.Context) (int64, error)

func (*RedisCache) Del

func (r *RedisCache) Del(ctx context.Context, keys ...string) error

func (*RedisCache) Expire added in v1.6.1

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

Expire 设置 key 的 TTL;key 不存在时 Redis 返回 false,此处仅在网络错误时上报。

func (*RedisCache) FlushDB

func (r *RedisCache) FlushDB(ctx context.Context) error

func (*RedisCache) Get

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

func (*RedisCache) Incr added in v1.6.1

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

Incr 原子自增;key 不存在时返回 1。Incr 不会重置 TTL。

func (*RedisCache) Info

func (r *RedisCache) Info(ctx context.Context) (string, error)

func (*RedisCache) Keys

func (r *RedisCache) Keys(ctx context.Context, pattern string) ([]string, error)

func (*RedisCache) Set

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

func (*RedisCache) SetNX added in v1.6.0

func (r *RedisCache) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error)

SetNX 仅在 key 不存在时设置值并指定 TTL,用于短时分布式互斥(如多实例定时任务)。

Jump to

Keyboard shortcuts

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