cache

package module
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 13 Imported by: 7

README

cache

cache 是 infrago 的模块

包定位

  • 类型:模块
  • 作用:统一缓存模块,负责缓存读写与多后端抽象。

主要功能

  • 对上提供统一模块接口
  • 对下通过驱动接口接入具体后端
  • 支持按配置切换驱动实现
  • 支持命中/未命中/错误等模块级和实例级基础统计
  • 支持单个或批量自动序列号
  • 支持统一 key 拼接,避免业务里散落手写 key 规则

快速接入

import _ "github.com/infrago/cache"
[cache]
driver = "default"
prefix = "app:"
expire = "10m"
allow_clear_all = false
key := cache.Key("user", 123, "profile") // user:123:profile

驱动实现接口列表

以下接口由驱动实现(来自模块 driver.go):

Driver
  • Connect(*Instance) (Connect, error)
Connect
  • Open() error
  • Close() error
  • Read(string) ([]byte, error)
  • Write(key string, val []byte, expire time.Duration) error
  • Exists(key string) (bool, error)
  • Delete(key string) error
  • Sequence(key string, start, step int64, expire time.Duration) (int64, error)
  • Keys(prefix string) ([]string, error)
  • Clear(prefix string) error
ManyConnect

ManyConnect 是可选接口。驱动实现后,SequenceMany 会走驱动原生批量能力;未实现时模块会自动循环调用 Sequence

  • SequenceMany(key string, start, step, count int64, expire time.Duration) ([]int64, error)

全局配置项(所有配置键)

配置段:[cache][cache.xxx]

  • driver:驱动名,默认 default
  • weight:分布权重,默认 1;小于 0 表示打开实例但不参与默认 key 分布
  • prefix:真实缓存 key 前缀
  • codec:Map 编解码器,默认 json
  • expire / ttl:默认过期时间,支持 "10s" 这类 duration 字符串,也支持数字秒
  • allow_clear_all / clear_all:是否允许 Clear() 空前缀全清,默认 false
  • setting:驱动专用配置

管理操作

  • Keys / Clear 是管理操作,不建议在高频业务路径中调用。
  • Clear("") / Clear() 默认会返回 ErrUnsafeClear,避免误删整个实例。
  • 确认需要全清时使用 ClearAll() / ClearAllFrom(conn),或者配置 allow_clear_all=true 兼容旧调用。
  • Redis 驱动内部使用 SCAN 避免 KEYS 阻塞,但大范围扫描仍然有成本。

说明

  • setting 一般用于向具体驱动透传专用参数
  • 多实例配置请参考模块源码中的 Config/configure 处理逻辑
  • Stats() 返回当前进程内基础计数,并在 Instances 中包含实例级统计
  • StatsFrom(conn) 返回指定实例统计;ResetStats() 清空模块和实例计数

Documentation

Index

Constants

View Source
const NAME = "CACHE"

Variables

View Source
var ErrInvalidConnection = errors.New("Invalid cache connection.")
View Source
var ErrUnsafeClear = errors.New("Unsafe cache clear blocked.")

Functions

func Clear

func Clear(prefixs ...string) error

func ClearAll added in v0.26.0

func ClearAll() error

func ClearAllFrom added in v0.26.0

func ClearAllFrom(conn string) error

func ClearFrom

func ClearFrom(conn string, prefixs ...string) error

func Delete

func Delete(key string) error

func DeleteFrom

func DeleteFrom(conn, key string) error

func Exists

func Exists(key string) (bool, error)

func ExistsIn

func ExistsIn(conn, key string) (bool, error)

func Key added in v0.26.0

func Key(parts ...any) string

func KeyWith added in v0.26.0

func KeyWith(sep string, parts ...any) string

func Keys

func Keys(prefixs ...string) ([]string, error)

func KeysFrom

func KeysFrom(conn string, prefixs ...string) ([]string, error)

func Read

func Read(key string) (Map, error)

func ReadData

func ReadData(key string) ([]byte, error)

func ReadDataFrom

func ReadDataFrom(conn, key string) ([]byte, error)

func ReadFrom

func ReadFrom(conn, key string) (Map, error)

func ResetStats added in v0.26.0

func ResetStats()

func Sequence

func Sequence(key string, start, step int64, expires ...time.Duration) (int64, error)

func SequenceMany added in v0.26.0

func SequenceMany(key string, start, step, count int64, expires ...time.Duration) ([]int64, error)

func SequenceManyOn added in v0.26.0

func SequenceManyOn(conn, key string, start, step, count int64, expires ...time.Duration) ([]int64, error)

func SequenceOn

func SequenceOn(conn, key string, start, step int64, expires ...time.Duration) (int64, error)

func Write

func Write(key string, value Map, expires ...time.Duration) error

func WriteData

func WriteData(key string, data []byte, expires ...time.Duration) error

func WriteDataTo

func WriteDataTo(conn, key string, data []byte, expires ...time.Duration) error

func WriteTo

func WriteTo(conn, key string, value Map, expires ...time.Duration) error

Types

type Config

type Config struct {
	Driver        string
	Weight        int
	Prefix        string
	Codec         string
	Expire        time.Duration
	AllowClearAll bool
	Setting       Map
}

type Configs

type Configs map[string]Config

type Connect

type Connect interface {
	Open() error
	Close() error

	Read(string) ([]byte, error)
	Write(key string, val []byte, expire time.Duration) error
	Exists(key string) (bool, error)
	Delete(key string) error
	Sequence(key string, start, step int64, expire time.Duration) (int64, error)
	Keys(prefix string) ([]string, error)
	Clear(prefix string) error
}

type Driver

type Driver interface {
	Connect(*Instance) (Connect, error)
}

type Instance

type Instance struct {
	Name    string
	Config  Config
	Setting Map
	// contains filtered or unexported fields
}

type ManyConnect added in v0.26.0

type ManyConnect interface {
	SequenceMany(key string, start, step, count int64, expire time.Duration) ([]int64, error)
}

type Module

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

func (*Module) Clear

func (m *Module) Clear(prefixs ...string) error

func (*Module) ClearAll added in v0.26.0

func (m *Module) ClearAll() error

func (*Module) ClearAllFrom added in v0.26.0

func (m *Module) ClearAllFrom(conn string) error

func (*Module) ClearFrom

func (m *Module) ClearFrom(conn string, prefixs ...string) error

func (*Module) Close added in v0.7.0

func (m *Module) Close()

func (*Module) Config

func (m *Module) Config(global Map)

func (*Module) Delete

func (m *Module) Delete(key string) error

func (*Module) DeleteFrom

func (m *Module) DeleteFrom(conn, key string) error

func (*Module) Exists

func (m *Module) Exists(key string) (bool, error)

func (*Module) ExistsIn

func (m *Module) ExistsIn(conn, key string) (bool, error)

func (*Module) Keys

func (m *Module) Keys(prefixs ...string) ([]string, error)

func (*Module) KeysFrom

func (m *Module) KeysFrom(conn string, prefixs ...string) ([]string, error)

func (*Module) Open added in v0.7.0

func (m *Module) Open()

func (*Module) Read

func (m *Module) Read(key string) (Map, error)

func (*Module) ReadData

func (m *Module) ReadData(key string) ([]byte, error)

func (*Module) ReadDataFrom

func (m *Module) ReadDataFrom(conn, key string) ([]byte, error)

func (*Module) ReadFrom

func (m *Module) ReadFrom(conn, key string) (Map, error)

func (*Module) Register

func (m *Module) Register(name string, value Any)

func (*Module) RegisterConfig added in v0.7.0

func (m *Module) RegisterConfig(name string, config Config)

func (*Module) RegisterConfigs added in v0.7.0

func (m *Module) RegisterConfigs(configs Configs)

func (*Module) RegisterDriver added in v0.7.0

func (m *Module) RegisterDriver(name string, driver Driver)

func (*Module) ResetStats added in v0.26.0

func (m *Module) ResetStats()

func (*Module) Sequence

func (m *Module) Sequence(key string, start, step int64, expires ...time.Duration) (int64, error)

func (*Module) SequenceMany added in v0.26.0

func (m *Module) SequenceMany(key string, start, step, count int64, expires ...time.Duration) ([]int64, error)

func (*Module) SequenceManyOn added in v0.26.0

func (m *Module) SequenceManyOn(conn, key string, start, step, count int64, expires ...time.Duration) ([]int64, error)

func (*Module) SequenceOn

func (m *Module) SequenceOn(conn, key string, start, step int64, expires ...time.Duration) (int64, error)

func (*Module) Setup added in v0.7.0

func (m *Module) Setup()

func (*Module) Start added in v0.7.0

func (m *Module) Start()

func (*Module) Stats added in v0.26.0

func (m *Module) Stats() Statistics

func (*Module) StatsFrom added in v0.26.0

func (m *Module) StatsFrom(conn string) (Statistics, error)

func (*Module) Stop added in v0.7.0

func (m *Module) Stop()

func (*Module) Write

func (m *Module) Write(key string, val Map, expires ...time.Duration) error

func (*Module) WriteData

func (m *Module) WriteData(key string, data []byte, expires ...time.Duration) error

func (*Module) WriteDataTo

func (m *Module) WriteDataTo(conn, key string, data []byte, expires ...time.Duration) error

func (*Module) WriteTo

func (m *Module) WriteTo(conn string, key string, val Map, expires ...time.Duration) error

type Statistics added in v0.26.0

type Statistics struct {
	Read   uint64
	Write  uint64
	Delete uint64
	Exists uint64
	Keys   uint64
	Clear  uint64

	Sequence     uint64
	SequenceMany uint64

	Hit   uint64
	Miss  uint64
	Error uint64

	Instances map[string]Statistics
}

func Stats added in v0.26.0

func Stats() Statistics

func StatsFrom added in v0.26.0

func StatsFrom(conn string) (Statistics, error)

Jump to

Keyboard shortcuts

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