xnews

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: 6 Imported by: 1

README

目的

xnews是一个信息管理库,用于缓存和管理信息,包括新闻、公告、通知等。用于解决信息的发布和管理问题。

在普通的kv型数据库上做了一点封装

Documentation

Index

Constants

View Source
const (
	ErrNil               = ErrStr("nil")
	ErrTopicAlreadyExist = ErrStr("topic already exist") // auto generated from .\mgr.go
	ErrInit              = ErrStr("init")                // auto generated from .\mgr.go
	ErrTopicNotExist     = ErrStr("topic not exist")     // auto generated from .\mgr.go
	ErrGetTopicFailed    = ErrStr("get topic failed")    // auto generated from .\mgr.go
	ErrAddMessageFailed  = ErrStr("add message failed")  // auto generated from .\mgr.go
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrStr

type ErrStr string

func (ErrStr) Error

func (e ErrStr) Error() string

type Message

type Message struct {
	CreateTime time.Time
	Content    string
	ExpireTime time.Time
}

func (*Message) Reset

func (m *Message) Reset()

type Topic

type Topic struct {
	misc.InitTag

	TopicSetting
	// contains filtered or unexported fields
}

Topic 一个主题

func NewTopic

func NewTopic(name string, setting TopicSetting, ctx context.Context) (*Topic, error)

func (*Topic) AddMessage

func (t *Topic) AddMessage(content string, remain *TopicTimeRemain) error

func (*Topic) Get

func (t *Topic) Get() ([]string, error)

func (*Topic) Init

func (t *Topic) Init(name string, setting TopicSetting, ctx context.Context) error

Init 初始化一个主题, DefaultRemain 为默认的留存时间,如果不设置则为永久留存

func (*Topic) IsEmpty

func (t *Topic) IsEmpty() bool

func (*Topic) SetTopicSetting

func (t *Topic) SetTopicSetting(setting TopicSetting) error

type TopicClearTime

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

clearTime 用于在某个时间间隔内清理某个topic的信息

type TopicSetting

type TopicSetting struct {
	Limit         []TopicTimeLimit
	Clear         []TopicClearTime
	DefaultRemain *TopicTimeRemain // 默认的留存时间,如果不设置则为永久留存
	// contains filtered or unexported fields
}

func (*TopicSetting) AddClear

func (t *TopicSetting) AddClear(startTime time.Time, duration time.Duration)

func (*TopicSetting) AddForeverLimit

func (t *TopicSetting) AddForeverLimit(num int)

func (*TopicSetting) AddLimit

func (t *TopicSetting) AddLimit(startTime time.Time, duration time.Duration, num int)

AddLimit 用于限制某个topic的时间间隔内允许发送的信息条数,建议只使用一个永久limit,如果添加多条limit,则任意一条达到上限,则都会删除最旧的信息,同时触发多条也只会删除一条,后续如果有别的需求再改改。 用于实现只保存十条日志之类的功能,其他的功能为拓展性,暂时没想好能干嘛。建议如果要很复杂的组合限制、过期逻辑,分离到多个topic中,然后在业务层面组合

func (*TopicSetting) AddNowClear

func (t *TopicSetting) AddNowClear(duration time.Duration)

func (*TopicSetting) AddNowLimit

func (t *TopicSetting) AddNowLimit(duration time.Duration, num int)

func (*TopicSetting) SetDefaultRemain

func (t *TopicSetting) SetDefaultRemain(duration time.Duration)

type TopicTimeLimit

type TopicTimeLimit struct {
	Duration        *time.Duration // 不填的话为永久上限
	Num             int
	LastResetTime   time.Time
	ThisDurationNum int
}

TopicTimeLimit 用于限制某个topic的时间间隔内做多保留的信息数量。 如果不填duration,则为永久上限。 可以将多条组合使用,但是请注意,任意一条如果达到上限,则都会删除最旧的信息。

func (*TopicTimeLimit) Add

func (l *TopicTimeLimit) Add(num int) int

Add 向limit当前周期内增加n个计数,返回淘汰的数量

type TopicTimeRemain

type TopicTimeRemain time.Duration

TopicTimeRemain 用于限制某个topic的单条信息的留存时间

type XNews

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

func NewXNews

func NewXNews(ctx context.Context) (*XNews, error)

func (*XNews) AddMessage

func (x *XNews) AddMessage(topic string, message string) error

func (*XNews) AddMessageWithExpire

func (x *XNews) AddMessageWithExpire(topic string, message string, expire TopicTimeRemain) error

func (*XNews) AddTopic

func (x *XNews) AddTopic(name string, setting TopicSetting) error

func (*XNews) DelTopic

func (x *XNews) DelTopic(name string) error

func (*XNews) GetTopic

func (x *XNews) GetTopic(name string) ([]string, error)

func (*XNews) Init

func (x *XNews) Init(ctx context.Context) error

Jump to

Keyboard shortcuts

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