settings

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package settings 与用户关联的设置信息

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InstallObject

func InstallObject[T any](s *Settings, id string, preset *T) error

InstallObject 向数据表中安装一个设置对象

preset 默认值;

Types

type Object

type Object[T any] struct {
	// contains filtered or unexported fields
}

Object 设置对象的操作接口

T 为实际的设置对象,必须得是一个结构体类型。 如果 T 的指针还实现了 Sanitizer 接口,那么在加载或是写入数据源成功之后会调用该接口对数据进行修正。 用户对 T 的修改应该及时采用 [Object.Put] 写入数据源,否则在重启后这些修改不会生效。

其每一个公开字段在数据库中表示为一条记录。如果要改变在数据库对应的字段名, 可以为每一个字段添加 setting 标签,比如:

struct X {
    Field string `setting:"field"`
}

默认为字段名,如果不需要该字段,可以采用小写或是 setting:"-" 对其忽略。 如果字段是非内置的类型,可以实现 json 的相关接口实现自定义的存储和读取功能。

func LoadObject

func LoadObject[T any](s *Settings, id string, ttl time.Duration) (*Object[T], error)

LoadObject 从 Settings 加载设置对象的数据

id 表示当前设置对象的 ID,每一个设置象需要有一个唯一的 id;

func (*Object[T]) Get

func (obj *Object[T]) Get(uid int64) (*T, error)

Get 加载用户 uid 的配置项

按以下步骤返回:

  • 查看是否有缓存的对象;
  • 从数据库查找数据;
  • 采用默认值;

func (*Object[T]) HandleGet

func (obj *Object[T]) HandleGet(ctx *web.Context, uid int64) web.Responser

HandleGet 用于处理 Get 的 HTTP 请求

func (*Object[T]) HandlePut

func (obj *Object[T]) HandlePut(ctx *web.Context, uid int64) web.Responser

HandlePut 用于处理 Put 的 HTTP 请求

func (*Object[T]) Set

func (obj *Object[T]) Set(uid int64, o *T) error

Set 保存 o 的设置对象

type Sanitizer

type Sanitizer interface {
	SanitizeSettings() error
}

type Settings

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

func Install

func Install(mod *cmfx.Module, tableName string) *Settings

func New

func New(mod *cmfx.Module, tableName string) *Settings

New 声明一张用于保存设置项的表

tableName 为表名的后缀; presetUID 默认用户的 id,该用户的的设置总是存在,当其它用户不存在设置项,会采用该用户的设置项作为默认值。

Jump to

Keyboard shortcuts

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