xstorage

package
v0.0.0-...-2580d5c Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 17 Imported by: 7

README

提供方便得通用存储,用于实现方便易用的通用存储、缓存,并能快速实现持久化存储。

为了通用起见,所有的存储都是基于key-value的形式,key为string,value为type interface{}。 建议将作为key的字符串以常量形式定义,以便于统一管理。

用于非高性能的存储,随时随地都能存储不需要单独建表,类似于需要访问的新闻关键词等,或者支持大系统的小模块肆意落盘等

类似于vscode,在配置json之上的操作、搜索、显示体系

Documentation

Index

Constants

View Source
const (
	DBBegin keyValueSaveType = iota
	SqlLiteDB
	FileBegin
	Toml // 为保证效率,必须开启UseCache、FullInitLoad
)
View Source
const (
	ValueTypeNormalBegin ValueType = iota
	ValueTypeString
	ValueTypeInt
	ValueTypeFloat
	ValueTypeBool
	ValueTypeNormalEnd
	ValueTypeSliceBegin  = 100
	ValueTypeSliceString = iota + ValueTypeSliceBegin - ValueTypeNormalEnd - 1
	ValueTypeSliceInt
	ValueTypeSliceFloat
	ValueTypeSliceBool
)

可能会被外部调用,所以复杂命名

View Source
const (
	ErrSqliteDBFileAddrEmpty                   = misc.ErrStr("sqlite db file addr is empty")
	ErrSqliteDBFileAddrNotExist                = misc.ErrStr("sqlite db file addr not exist")
	ErrMgrNotInit                              = misc.ErrStr("mgr not init")
	ErrSqliteCoreNotInit                       = misc.ErrStr("sqlite core not init")
	ErrSetValue                                = misc.ErrStr("set value error")
	ErrKeyIsEmpty                              = misc.ErrStr("key is empty")
	ErrNotUseCache                             = misc.ErrStr("not use cache")
	ErrKeyNotExist                             = misc.ErrStr("key not exist")
	ErrValueIsNil                              = misc.ErrStr("value is nil")
	ErrSliceButValueIntIsNil                   = misc.ErrStr("slice but ValueInt is nil")
	ErrValueType                               = misc.ErrStr("value type error")
	ErrOpenSqlite                              = misc.ErrStr("open sqlite error")
	ErrAutoMigrate                             = misc.ErrStr("auto migrate error")
	ErrRecIsNil                                = misc.ErrStr("rec is nil")
	ErrNotUseDb                                = misc.ErrStr("not use db")
	ErrRecordToMap                             = misc.ErrStr("record to map error")
	ErrPoolType                                = misc.ErrStr("pool type error")
	ErrNotUseCacheAndNotUseDb                  = misc.ErrStr("not use cache and not use db")
	ErrGetAllValue                             = misc.ErrStr("get all value error")
	ErrGinEngineRun                            = misc.ErrStr("gin engine run error")
	ErrNotUseCacheOrNotUseDbAndFullInitLoad    = misc.ErrStr("not use cache or not use db and full init load")
	ErrUseJsonButNotUseCacheAndNotFullInitLoad = misc.ErrStr("use json, but not use cache and not full init load")
	ErrNewSqliteCore                           = misc.ErrStr("new sqlite core error")
	ErrNotUseDbAndFullInitLoad                 = misc.ErrStr("not use db and full init load")
	ErrValueUnitIsNil                          = misc.ErrStr("valueUnit is nil")
	ErrValueIsDirty                            = misc.ErrStr("value is dirty")
	ErrRemoveFromMap                           = misc.ErrStr("remove from map error")
	ErrDeleteValue                             = misc.ErrStr("delete value error")
	ErrKeyCanNotContainSquareBrackets          = misc.ErrStr("Key can not contain []")
	ErrSqliteData2Model                        = misc.ErrStr("sqliteData2Model")
	ErrCreateValue                             = misc.ErrStr("create value error")
	ErrRemoveValue                             = misc.ErrStr("remove value error")
	ErrGetSliceValue                           = misc.ErrStr("get slice value error")
	ErrsqliteModel2Data                        = misc.ErrStr("sqliteModel2Data error")
	ErrGet                                     = misc.ErrStr("get error")
)
View Source
const (
	ErrNil               = ErrStr("nil")
	ErrValueTypeNotMatch = ErrStr("value type not match") // auto generated from .\sqlite.go
	ErrCoreIsNil         = ErrStr("core is nil")          // auto generated from .\cfg_pack.go
	ErrNotInitialized    = ErrStr("not initialized")      // auto generated from .\cfg_pack.go
	ErrKeyNotFound       = ErrStr("key not found")        // auto generated from .\cfg_pack.go
	ErrParamIsNil        = ErrStr("param is nil")         // auto generated from .\cfg_pack.go
	ErrParamIsInvalid    = ErrStr("param is invalid")     // auto generated from .\cfg_pack.go
	ErrKeyAlreadyExist   = ErrStr("key already exist")    // auto generated from .\cfg_pack.go
	ErrParamIsEmpty      = ErrStr("param is empty")       // auto generated from .\cfg_pack.go
)

Variables

This section is empty.

Functions

func Compare

func Compare(unit1 *ValueUnit, unit2 *ValueUnit) bool

func Copy

func Copy(srcValue *ValueUnit, newValue *ValueUnit)

func Join

func Join(src ...string) string

func Split

func Split(src string) []string

func ToBase

func ToBase[T IValueType](unit *ValueUnit) T

ToBase 直接转换为基础类型,一方面是为了避免频繁的类型转换,另一方面是为了限制类型

func ToBaseF

func ToBaseF[T IValueType](unit *ValueUnit, err error) T

func UnitToJStruct

func UnitToJStruct[T any](unit *ValueUnit) *T

UnitToJStruct 直接转换为结构体

func UnitToString

func UnitToString(unit *ValueUnit) string

Types

type CfgExt

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

CfgExt 为xstorage增加一个通用的配置机制

func NewCfgExt

func NewCfgExt(core *XStorage) (*CfgExt, error)

func (*CfgExt) AddParam

func (c *CfgExt) AddParam(param *CfgParam) error

func (*CfgExt) Get

func (c *CfgExt) Get(keys ...string) (*ValueUnit, error)

func (*CfgExt) GetAll

func (c *CfgExt) GetAll() (map[string]ValueUnit, error)

func (*CfgExt) GetUser

func (c *CfgExt) GetUser(user string, keys ...string) (*ValueUnit, error)

func (*CfgExt) GetWithFilter

func (c *CfgExt) GetWithFilter(prefix, user string) (map[string]ValueUnit, error)

func (*CfgExt) Init

func (c *CfgExt) Init(core *XStorage) error

func (*CfgExt) Set

func (c *CfgExt) Set(key string, value string) error

func (*CfgExt) SetUser

func (c *CfgExt) SetUser(user, key string, value string) error

type CfgParam

type CfgParam struct {
	Key       string // 与前端协调的key
	ValueType ValueType
	CanUser   bool      // 是否可以用户配置
	RealKey   string    // storage里面的key
	Default   ValueUnit // 如果storage里面没有这个值,就会使用这个值。
}

type EmptyLogger

type EmptyLogger struct {
}

func (EmptyLogger) Error

func (e EmptyLogger) Error(ctx context.Context, s string, i ...interface{})

func (EmptyLogger) Info

func (e EmptyLogger) Info(ctx context.Context, s string, i ...interface{})

func (EmptyLogger) LogMode

func (e EmptyLogger) LogMode(level logger.LogLevel) logger.Interface

func (EmptyLogger) Trace

func (e EmptyLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

func (EmptyLogger) Warn

func (e EmptyLogger) Warn(ctx context.Context, s string, i ...interface{})

type ErrStr

type ErrStr string

func (ErrStr) Error

func (e ErrStr) Error() string

type IDBCore

type IDBCore interface {
	Get(key string, rec *ValueUnit) (bool, error)
	Set(key string, value *ValueUnit) error
	GetAll() (map[string]*ValueUnit, error)
	Delete(key string) error
}

type IFileCore

type IFileCore interface {
	GetAll() (map[string]*ValueUnit, error)
	SaveAll(data map[string]*ValueUnit) error
}

type IValueType

type IValueType interface {
	int | string | float32 | bool | []int | []string | []float32 | []bool
}

type KeyValueModel

type KeyValueModel struct {
	gorm.Model
	// Key 主键、索引
	Key         string `gorm:"primaryKey;index"`
	ValueType   int
	ValueInt    *int
	ValueString *string
	ValueFloat  *float32
}

type KeyValueProperty

type KeyValueProperty uint32
const (
	// MultiSafe 多线程安全,仅针对缓存
	MultiSafe KeyValueProperty = 1 << iota // TODO:
	// UseCache 缓存
	UseCache
	// UseDisk 落盘
	UseDisk
	// FullInitLoad 全量初始化加载,没有的话即懒加载,仅在同时使用缓存和数据库时有效,建议与MultiSafe一起使用,并使用setAsync方法进行set
	FullInitLoad
	// OpenWeb 启用web端口,建议不要使用内置的web服务,而是自己启动一个web服务,然后调用GetGinEngine方法获取gin引擎,然后自己注册路由
	OpenWeb
)

type ParamMap

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

func (*ParamMap) AddParam

func (p *ParamMap) AddParam(param *CfgParam) error

func (*ParamMap) GetParam

func (p *ParamMap) GetParam(key string) *CfgParam

type SqliteCore

type SqliteCore struct {
	misc.InitTag
	// contains filtered or unexported fields
}

func NewSqliteCore

func NewSqliteCore(DbFileAddr string) (*SqliteCore, error)

func (*SqliteCore) Delete

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

func (*SqliteCore) Get

func (m *SqliteCore) Get(key string, rec *ValueUnit) (bool, error)

func (*SqliteCore) GetAll

func (m *SqliteCore) GetAll() (map[string]*ValueUnit, error)

func (*SqliteCore) Have

func (m *SqliteCore) Have(key string) (bool, error)

func (*SqliteCore) Set

func (m *SqliteCore) Set(key string, value *ValueUnit) error

type TomlCore

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

func NewTomlCore

func NewTomlCore(addr string) *TomlCore

func (TomlCore) GetAll

func (j TomlCore) GetAll() (map[string]*ValueUnit, error)

func (TomlCore) SaveAll

func (j TomlCore) SaveAll(data map[string]*ValueUnit) error

type ValueType

type ValueType int

type ValueUnit

type ValueUnit struct {
	Type ValueType
	Data interface{}
	// contains filtered or unexported fields
}

ValueUnit 加入类型,用于在非反射的情况下直接处理类型

func JStructToUnit

func JStructToUnit[T any](value *T) *ValueUnit

func StringToUnit

func StringToUnit(value string, valueType ValueType) *ValueUnit

StringToUnit 将json字符串转为ValueUnit

func ToUnit

func ToUnit[T IValueType](value T, valueType ValueType) *ValueUnit

func (*ValueUnit) Reset

func (v *ValueUnit) Reset()

type WebCode

type WebCode int // web业务码
const (
	WebCodeSuc WebCode = iota
	WebCodeFail
)

可能会被外部调用,所以复杂命名

type WebFailReason

type WebFailReason int // web失败原因
const (
	WebFailReasonNull WebFailReason = iota
	WebFailReasonNoLegalParam
	WebFailReasonInnerError
)

type WebPack

type WebPack struct {
	misc.InitTag
	// contains filtered or unexported fields
}

WebPack 是xstorage的拓展之一,必须绑定xstorage使用 因为是小众需求所以做一下拆分

func NewWebPack

func NewWebPack(setting WebPackSetting, core *XStorage) (*WebPack, error)

func (*WebPack) Init

func (w *WebPack) Init(setting WebPackSetting, core *XStorage) error

func (*WebPack) StartWeb

func (w *WebPack) StartWeb() error

func (*WebPack) WebGet

func (w *WebPack) WebGet(c *gin.Context)

func (*WebPack) WebGetAll

func (w *WebPack) WebGetAll(c *gin.Context)

func (*WebPack) WebSet

func (w *WebPack) WebSet(c *gin.Context)

type WebPackSetting

type WebPackSetting struct {
	LogFrom string
	Log     *xlog.XLog
	WebPort int
}

type XStorage

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

func NewXStorage

func NewXStorage(setting XStorageSetting) (*XStorage, error)

func (*XStorage) Delete

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

func (*XStorage) FromDiskGetAll

func (m *XStorage) FromDiskGetAll() (map[string]*ValueUnit, error)

func (*XStorage) Get

func (m *XStorage) Get(key string) (*ValueUnit, error)

func (*XStorage) GetAll

func (m *XStorage) GetAll() (map[string]*ValueUnit, error)

func (*XStorage) GetAndSetDefault

func (m *XStorage) GetAndSetDefault(key string, defaultValue *ValueUnit) (*ValueUnit, error)

func (*XStorage) GetAndSetDefaultAsync

func (m *XStorage) GetAndSetDefaultAsync(key string, defaultValue *ValueUnit, rec *ValueUnit) (bool, error, chan error)

GetAndSetDefaultAsync get值,如果没有就设置并返回默认值,返回 是否setdefault数据,数据,错误

func (*XStorage) GetAndSetDefaultHP

func (m *XStorage) GetAndSetDefaultHP(key string, defaultValue *ValueUnit, rec *ValueUnit) (bool, error)

GetAndSetDefaultHP get值,如果没有就设置并返回默认值,返回 是否setdefault数据,数据,错误

func (*XStorage) GetHP

func (m *XStorage) GetHP(key string, valueUnit *ValueUnit) (bool, error)

func (*XStorage) Init

func (m *XStorage) Init(setting XStorageSetting) error

func (*XStorage) Set

func (m *XStorage) Set(key string, value *ValueUnit) error

func (*XStorage) SetAsync

func (m *XStorage) SetAsync(key string, value *ValueUnit) (error, chan error)

func (*XStorage) SetDefault

func (m *XStorage) SetDefault(key string, defaultValue *ValueUnit) error

SetDefault 设置默认值,如果已经存在则不设置

func (*XStorage) SetDefaultAsync

func (m *XStorage) SetDefaultAsync(key string, defaultValue *ValueUnit) (error, chan error)

SetDefaultAsync 设置默认值,如果已经存在则不设置

type XStorageSetting

type XStorageSetting struct {
	Property KeyValueProperty
	SaveType keyValueSaveType
	DBAddr   string
	FileAddr string
}

Jump to

Keyboard shortcuts

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