Documentation
¶
Overview ¶
Package storage 提供版本化的对象存储原语:owner + collection + key + value + version, 通过乐观并发控制(OCC)保证并发写不覆盖,支持三种写语义与懒淘汰索引。
设计要点:
- 对象的 version = value 的 MD5 摘要,写时 If-Match 校验(乐观锁);
- 三种写模式:IfMatch(version 匹配才写,否则冲突)、IfNotExist(version="" 仅当不存在)、 LastWriteWins(version="*" 无条件 upsert);
- 批量写按 collection→key→owner 排序,避免循环等待死锁;
- 懒淘汰索引:超过 maxEntries*1.1 时按 update_time 删最旧。
适用场景:游戏存档、用户配置、任意需要"版本化 + 并发安全"的 KV 持久层缓存。 本包为内存实现,生产可替换 StorageBackend 接入 DB。
零值不可用,用 New 构造。Storage 并发安全。
Index ¶
- Variables
- type Object
- type Option
- type Storage
- func (s *Storage) Count() int
- func (s *Storage) Delete(owner, collection, key, callerID string) error
- func (s *Storage) List(collection, callerID string) []*Object
- func (s *Storage) Read(owner, collection, key, callerID string) (*Object, error)
- func (s *Storage) Write(op WriteOp, now int64) (*Object, error)
- func (s *Storage) WriteBatch(ops []WriteOp, now int64) ([]*Object, error)
- type WriteMode
- type WriteOp
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrConflict = errors.New("storage: conflict (already exists)")
ErrConflict IfNotExist 时对象已存在。
View Source
var ErrReadOnly = errors.New("storage: read-only object")
ErrReadOnly 对象只读(WriteAccess=0)。
View Source
var ErrVersionMismatch = errors.New("storage: version mismatch")
ErrVersionMismatch OCC 版本冲突(他人已先写)。
Functions ¶
This section is empty.
Types ¶
type Object ¶
type Object struct {
OwnerID string // 所属用户(空=公共)
Collection string // 集合名(逻辑分组)
Key string // 集合内 key
Value []byte // 负载
Version string // 当前版本(value 的 MD5;空=不存在)
ReadAccess int // 0=私有 1=自己可读 2=公开可读
WriteAccess int // 0=只读 1=可写
UpdateTime int64 // unix nano
}
Object 是一个存储对象。
type Option ¶
type Option func(*config)
Option 配置 Storage。
func WithMaxEntries ¶
WithMaxEntries 设置最大对象数,超出 1.1 倍时按 updateTime 删最旧(默认 10000)。
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage 内存对象存储,带懒淘汰索引。
Click to show internal directories.
Click to hide internal directories.