storage

package
v0.8.2 Latest Latest
Warning

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

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

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

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

func WithMaxEntries(n int) Option

WithMaxEntries 设置最大对象数,超出 1.1 倍时按 updateTime 删最旧(默认 10000)。

type Storage

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

Storage 内存对象存储,带懒淘汰索引。

func New

func New(opts ...Option) *Storage

New 创建存储。

func (*Storage) Count

func (s *Storage) Count() int

Count 当前对象总数。

func (*Storage) Delete

func (s *Storage) Delete(owner, collection, key, callerID string) error

Delete 删除对象。只读对象(WriteAccess=0)拒绝。

func (*Storage) List

func (s *Storage) List(collection, callerID string) []*Object

List 列出某 collection 下的所有对象(拷贝)。callerID 用于权限过滤。

func (*Storage) Read

func (s *Storage) Read(owner, collection, key, callerID string) (*Object, error)

Read 读取单个对象。owner="" 表示查公共对象。 readAccess 校验:私有(0)仅 owner 可读;1 仅自己;2 任意。

func (*Storage) Write

func (s *Storage) Write(op WriteOp, now int64) (*Object, error)

Write 写入单个对象。

func (*Storage) WriteBatch

func (s *Storage) WriteBatch(ops []WriteOp, now int64) ([]*Object, error)

WriteBatch 批量写。按 collection→key→owner 排序后依次应用,避免死锁。 全部成功才提交;任一失败则已应用的回滚(事务语义)。

type WriteMode

type WriteMode int

WriteMode 写语义。

const (
	// WriteIfMatch:仅当传入 version 与现存版本一致时才写(OCC 乐观锁)。
	// version="" 等价于 IfNotExist(仅当对象不存在)。
	WriteIfMatch WriteMode = iota
	// WriteIfNotExist:仅当对象不存在时才写。version 参数忽略。
	WriteIfNotExist
	// WriteLastWriteWins:无条件覆盖(upsert)。
	WriteLastWriteWins
)

type WriteOp

type WriteOp struct {
	OwnerID     string
	Collection  string
	Key         string
	Value       []byte
	Mode        WriteMode
	Version     string // IfMatch 时的期望版本
	ReadAccess  int
	WriteAccess int
}

WriteOp 是一次批量写操作。

Jump to

Keyboard shortcuts

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