Documentation ¶
Index ¶
Constants ¶
View Source
const (
LinkCollectionName = "links"
)
Variables ¶
View Source
var DefaultMongoDB = &MongoDB{}
View Source
var DefaultRedis = &Redis{}
Functions ¶
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 Link ¶
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索引,可以用定时脚本任务删除无用数据
type MongoDB ¶
type MongoDB struct {
// contains filtered or unexported fields
}
Click to show internal directories.
Click to hide internal directories.