component

package
v0.0.0-...-1eef698 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LinkCollectionName = "links"
)

Variables

View Source
var DefaultMongoDB = &MongoDB{}
View Source
var DefaultRedis = &Redis{}

Functions

func Shutdown

func Shutdown()

Shutdown 项目停止运行,对组件连接等关闭、资源清理

func Startup

func Startup() error

Startup 项目启动,初始化所有组件

Types

type CacheItf

type CacheItf interface {
	Lifespan

	Set(ctx context.Context, key string, value string, ttl int) error
	Get(ctx context.Context, key string) (string, error)
}
var Cache CacheItf

type DatabaseItf

type DatabaseItf interface {
	Lifespan

	Create(ctx context.Context, link *Link) (id string, codeExisted bool, err error)
	Get(ctx context.Context, params map[string]any) (*Link, error)
}
var Database DatabaseItf

type Lifespan

type Lifespan interface {
	Startup() error
	Shutdown() error
}

Lifespan 约束组件(缓存、数据库等),必须实现初始化和停用的方法

type Link struct {
	Id        string    `bson:"_id,omitempty"`
	UserId    string    `bson:"user_id"`
	Code      string    `bson:"code"`
	Salt      string    `bson:"salt"`
	LongUrl   string    `bson:"long_url"`
	Deadline  time.Time `bson:"deadline"`
	TtlTime   time.Time `bson:"ttl_time"`
	CreatedAt time.Time `bson:"created_at"`
	UpdatedAt time.Time `bson:"updated_at"`
}

Link

对于添加索引操作,官方go驱动不能在结构体tag赋值完成 需要在该collection创建了,并包含至少一个document,才能添加索引

code唯一索引: db.links.createIndex({"code": 1}, {"unique": true}) user_id普通索引: db.links.createIndex({"user_id": 1}) long_url普通索引: db.links.createIndex({"long_url": 1}) ttl_time ttl索引: db.links.createIndex({"ttl_time": 1}, {"expireAfterSeconds": 7200}) // ttl索引会增加数据库负载。如果不使用ttl索引,可以用定时脚本任务删除无用数据

func (*Link) Expired

func (l *Link) Expired() bool

type MongoDB

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

func (*MongoDB) Create

func (m *MongoDB) Create(ctx context.Context, link *Link) (id string, existed bool, err error)

func (*MongoDB) Get

func (m *MongoDB) Get(ctx context.Context, params map[string]any) (*Link, error)

func (*MongoDB) Shutdown

func (m *MongoDB) Shutdown() error

func (*MongoDB) Startup

func (m *MongoDB) Startup() error

type Redis

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

func (*Redis) Get

func (r *Redis) Get(ctx context.Context, key string) (string, error)

func (*Redis) Set

func (r *Redis) Set(ctx context.Context, key string, value string, ttl int) error

func (*Redis) Shutdown

func (r *Redis) Shutdown() error

func (*Redis) Startup

func (r *Redis) Startup() error

Jump to

Keyboard shortcuts

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