Documentation
¶
Index ¶
- Variables
- type Cache
- type MemoryCache
- func (m *MemoryCache) Close()
- func (m *MemoryCache) DBSize(_ context.Context) (int64, error)
- func (m *MemoryCache) Del(_ context.Context, keys ...string) error
- func (m *MemoryCache) Expire(_ context.Context, key string, ttl time.Duration) error
- func (m *MemoryCache) FlushDB(_ context.Context) error
- func (m *MemoryCache) Get(_ context.Context, key string) (string, error)
- func (m *MemoryCache) Incr(_ context.Context, key string) (int64, error)
- func (m *MemoryCache) Info(_ context.Context) (string, error)
- func (m *MemoryCache) Keys(_ context.Context, pattern string) ([]string, error)
- func (m *MemoryCache) Set(_ context.Context, key string, value interface{}, expiration time.Duration) error
- type RedisCache
- func (r *RedisCache) DBSize(ctx context.Context) (int64, error)
- func (r *RedisCache) Del(ctx context.Context, keys ...string) error
- func (r *RedisCache) Expire(ctx context.Context, key string, ttl time.Duration) error
- func (r *RedisCache) FlushDB(ctx context.Context) error
- func (r *RedisCache) Get(ctx context.Context, key string) (string, error)
- func (r *RedisCache) Incr(ctx context.Context, key string) (int64, error)
- func (r *RedisCache) Info(ctx context.Context) (string, error)
- func (r *RedisCache) Keys(ctx context.Context, pattern string) ([]string, error)
- func (r *RedisCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- func (r *RedisCache) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error)
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) Expire ¶ added in v1.6.1
Expire 为已存在的 key 设置 TTL;key 不存在返回 nil(与 Redis Expire 命令对齐,仅以 error 为异常)。
func (*MemoryCache) Incr ¶ added in v1.6.1
Incr 原子自增;过期后再 Incr 视为从 0 开始。 注意:当前实现不保留 TTL(与 Redis INCR 的语义一致:INCR 不改过期时间); 首次调用后请显式 Expire 建立窗口。
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
func NewRedisCache ¶
func NewRedisCache(client *redis.Client) *RedisCache
func (*RedisCache) Expire ¶ added in v1.6.1
Expire 设置 key 的 TTL;key 不存在时 Redis 返回 false,此处仅在网络错误时上报。
Click to show internal directories.
Click to hide internal directories.