storage

package
v0.0.28 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultBodyFieldName = "Data"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Data added in v0.0.20

type Data[Body any] struct {
	// contains filtered or unexported fields
}

Data 数据存储

  • 数据存储默认拥有一个 Body 字段

func (*Data[Body]) Handle added in v0.0.20

func (slf *Data[Body]) Handle(handler func(data Body))

Handle 处理数据

type Set added in v0.0.20

type Set[PrimaryKey generic.Ordered, Body any] struct {
	// contains filtered or unexported fields
}

Set 数据集合

func NewSet added in v0.0.20

func NewSet[PrimaryKey generic.Ordered, Body any](zero Body, getIndex func(data Body) PrimaryKey, options ...SetOption[PrimaryKey, Body]) *Set[PrimaryKey, Body]

func (*Set[PrimaryKey, Body]) Get added in v0.0.20

func (slf *Set[PrimaryKey, Body]) Get(index PrimaryKey) (*Data[Body], error)

Get 通过主键获取数据

  • 优先从内存中加载,如果数据不存在,则尝试从存储中加载

func (*Set[PrimaryKey, Body]) New added in v0.0.20

func (slf *Set[PrimaryKey, Body]) New() *Data[Body]

New 创建一份新数据

  • 这份数据不会被存储

func (*Set[PrimaryKey, Body]) Save added in v0.0.20

func (slf *Set[PrimaryKey, Body]) Save(data *Data[Body]) error

Save 保存数据

  • 该方法会将数据存储到存储器中

func (*Set[PrimaryKey, Body]) Set added in v0.0.20

func (slf *Set[PrimaryKey, Body]) Set(data *Data[Body])

Set 设置数据

  • 该方法会将数据存储到内存中

func (*Set[PrimaryKey, Body]) Struct added in v0.0.20

func (slf *Set[PrimaryKey, Body]) Struct(data *Data[Body]) (any, error)

Struct 将数据存储转换为结构体

type SetOption added in v0.0.20

type SetOption[PrimaryKey generic.Ordered, Body any] func(set *Set[PrimaryKey, Body])

func WithBodyName added in v0.0.20

func WithBodyName[PrimaryKey generic.Ordered, Body any](name string) SetOption[PrimaryKey, Body]

WithBodyName 设置 Body 字段名称

  • 默认字段名称为 DefaultBodyFieldName

func WithBodyTag added in v0.0.20

func WithBodyTag[PrimaryKey generic.Ordered, Body any](tags ...string) SetOption[PrimaryKey, Body]

WithBodyTag 设置 Body 字段标签

  • 如果有多个标签,将会以空格分隔,例如:`json:"data" yaml:"data"`

func WithIndex added in v0.0.20

func WithIndex[PrimaryKey generic.Ordered, Index generic.Ordered, Body any](name string, getValue func(data Body) Index) SetOption[PrimaryKey, Body]

WithIndex 添加一个索引

  • 索引将会在数据结构体中创建一个字段,这个字段必须可以在 Body 内部找到,用于对查找功能的拓展

func WithTagIndex added in v0.0.20

func WithTagIndex[PrimaryKey generic.Ordered, Index generic.Ordered, Body any](name string, tags []string, getValue func(data Body) Index) SetOption[PrimaryKey, Body]

WithTagIndex 添加一个带 tag 的索引

  • 同 WithIndex,但是可以自定义索引的 tag

type Storage added in v0.0.20

type Storage[PrimaryKey generic.Ordered, Body any] interface {
	// Load 加载数据
	Load(index PrimaryKey) (Body, error)

	// Save 保存数据
	Save(set *Set[PrimaryKey, Body], index PrimaryKey, data any) error
}

Jump to

Keyboard shortcuts

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