From 605787958db9dee0519a984261ab3cd101213c85 Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Fri, 30 Apr 2021 09:50:33 +0800 Subject: [PATCH] chore: remove some filed in app.yml --- conf/app.yml | 3 -- pkg/cache/cache.go | 15 +++---- pkg/cache/store/mongodb.go | 12 +++--- pkg/cache/store/rdbms.go | 80 ++++++++++++++++++++++++++++++++++++++ pkg/cache/store/store.go | 6 +-- pkg/config/config.go | 3 -- 6 files changed, 97 insertions(+), 22 deletions(-) create mode 100644 pkg/cache/store/rdbms.go diff --git a/conf/app.yml b/conf/app.yml index a1d6625..2e51b39 100644 --- a/conf/app.yml +++ b/conf/app.yml @@ -22,14 +22,11 @@ eiblogapp: descprefix: "Desc:" # 文章描述前缀 identifier: # 截取预览标识 length: 400 # 自动截取预览, 字符数 - trash: -48 # 回收箱保留48小时 - clean: 1 # 定时清理回收箱,每 %d 小时 timezone: Asia/Shanghai # 时区 disqus: # 评论相关 shortname: xxxxxx publickey: wdSgxRm9rdGAlLKFcFdToBe3GT4SibmV7Y8EjJQ0r4GWXeKtxpopMAeIeoI2dTEg accesstoken: 50023908f39f4607957e909b495326af - interval: 5 # 获取评论数量间隔h google: url: https://www.google-analytics.com/collect tid: UA-xxxxxx-1 diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index a7197e2..284a7da 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -86,7 +86,8 @@ func (c *Cache) AddArticle(article *model.Article) error { defer c.lock.Unlock() // store - err := c.InsertArticle(context.Background(), article) + err := c.InsertArticle(context.Background(), article, + config.Conf.EiBlogApp.General.StartID) if err != nil { return err } @@ -417,7 +418,8 @@ func (c *Cache) loadOrInit() error { Slug: "about", CreatedAt: time.Time{}, } - err = c.InsertArticle(context.Background(), about) + err = c.InsertArticle(context.Background(), about, + config.Conf.EiBlogApp.General.StartID) if err != nil { return err } @@ -430,7 +432,8 @@ func (c *Cache) loadOrInit() error { Slug: "blogroll", CreatedAt: time.Time{}.AddDate(0, 0, 7), } - err = c.InsertArticle(context.Background(), blogroll) + err = c.InsertArticle(context.Background(), blogroll, + config.Conf.EiBlogApp.General.StartID) if err != nil { return err } @@ -552,8 +555,7 @@ func (c *Cache) regeneratePages() { // timerClean 定时清理文章 func (c *Cache) timerClean() { - dur := time.Duration(config.Conf.EiBlogApp.General.Clean) - ticker := time.NewTicker(dur * time.Hour) + ticker := time.NewTicker(time.Hour) for range ticker.C { err := c.CleanArticles(context.Background()) @@ -565,8 +567,7 @@ func (c *Cache) timerClean() { // timerDisqus disqus定时操作 func (c *Cache) timerDisqus() { - dur := time.Duration(config.Conf.EiBlogApp.Disqus.Interval) - ticker := time.NewTicker(dur * time.Hour) + ticker := time.NewTicker(5 * time.Hour) for range ticker.C { err := internal.PostsCount(c.ArticlesMap) diff --git a/pkg/cache/store/mongodb.go b/pkg/cache/store/mongodb.go index d988cf5..fc84cea 100644 --- a/pkg/cache/store/mongodb.go +++ b/pkg/cache/store/mongodb.go @@ -6,7 +6,6 @@ import ( "sort" "time" - "github.com/eiblog/eiblog/pkg/config" "github.com/eiblog/eiblog/pkg/model" "go.mongodb.org/mongo-driver/bson" @@ -35,7 +34,7 @@ type mongodb struct { } // Init init mongodb client -func (db *mongodb) Init(source string) (Store, error) { +func (db *mongodb) Init(name, source string) (Store, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -117,7 +116,7 @@ func (db *mongodb) LoadInsertAccount(ctx context.Context, collection := db.Database(mongoDBName).Collection(collectionAccount) - filter := bson.M{"username": config.Conf.EiBlogApp.Account.Username} + filter := bson.M{"username": acct.Username} result := collection.FindOne(ctx, filter) err = result.Err() if err != nil { @@ -207,11 +206,11 @@ func (db *mongodb) LoadAllSerie(ctx context.Context) (model.SortedSeries, error) } // InsertArticle 创建文章 -func (db *mongodb) InsertArticle(ctx context.Context, article *model.Article) error { +func (db *mongodb) InsertArticle(ctx context.Context, article *model.Article, startID int) error { // 可手动分配ID或者分配ID, 占位至起始id for article.ID == 0 { id := db.nextValue(ctx, counterNameArticle) - if id < config.Conf.EiBlogApp.General.StartID { + if id < startID { continue } else { article.ID = id @@ -236,7 +235,8 @@ func (db *mongodb) RemoveArticle(ctx context.Context, id int) error { func (db *mongodb) CleanArticles(ctx context.Context) error { collection := db.Database(mongoDBName).Collection(collectionArticle) - exp := time.Now().Add(time.Duration(config.Conf.EiBlogApp.General.Trash) * time.Hour) + // 超过两天自动删除 + exp := time.Now().Add(-48 * time.Hour) filter := bson.M{"deleted_at": bson.M{"$gt": time.Time{}, "$lt": exp}} _, err := collection.DeleteMany(ctx, filter) return err diff --git a/pkg/cache/store/rdbms.go b/pkg/cache/store/rdbms.go new file mode 100644 index 0000000..63066ce --- /dev/null +++ b/pkg/cache/store/rdbms.go @@ -0,0 +1,80 @@ +// Package store provides ... +package store + +type rdbms struct{} + +// // Init 数据库初始化, 建表, 加索引操作等 +// func (db *rdbms) Init(source string) (Store, error) { +// gorm. +// +// } +// +// // LoadInsertBlogger 读取或创建博客 +// func (db *rdbms) LoadInsertBlogger(ctx context.Context, blogger *model.Blogger) (bool, error) { +// +// } +// +// // UpdateBlogger 更新博客 +// func (db *rdbms) UpdateBlogger(ctx context.Context, fields map[string]interface{}) error { +// +// } +// +// // LoadInsertAccount 读取或创建账户 +// func (db *rdbms) LoadInsertAccount(ctx context.Context, acct *model.Account) (bool, error) { +// +// } +// +// // UpdateAccount 更新账户 +// func (db *rdbms) UpdateAccount(ctx context.Context, name string, fields map[string]interface{}) error { +// +// } +// +// // InsertSerie 创建专题 +// func (db *rdbms) InsertSerie(ctx context.Context, series *model.Serie) error { +// +// } +// +// // RemoveSerie 删除专题 +// func (db *rdbms) RemoveSerie(ctx context.Context, id int) error { +// +// } +// +// // UpdateSerie 更新专题 +// func (db *rdbms) UpdateSerie(ctx context.Context, id int, fields map[string]interface{}) error { +// +// } +// +// // LoadAllSerie 读取所有专题 +// func (db *rdbms) LoadAllSerie(ctx context.Context) (model.SortedSeries, error) { +// +// } +// +// // InsertArticle 创建文章 +// func (db *rdbms) InsertArticle(ctx context.Context, article *model.Article) error { +// +// } +// +// // RemoveArticle 硬删除文章 +// func (db *rdbms) RemoveArticle(ctx context.Context, id int) error { +// +// } +// +// // CleanArticles 清理回收站文章 +// func (db *rdbms) CleanArticles(ctx context.Context) error { +// +// } +// +// // UpdateArticle 更新文章 +// func (db *rdbms) UpdateArticle(ctx context.Context, id int, fields map[string]interface{}) error { +// +// } +// +// // LoadArticle 查找文章 +// func (db *rdbms) LoadArticle(ctx context.Context, id int) (*model.Article, error) { +// +// } +// +// // LoadArticleList 查找文章列表 +// func (db *rdbms) LoadArticleList(ctx context.Context, search SearchArticles) (model.SortedArticles, int, error) { +// +// } diff --git a/pkg/cache/store/store.go b/pkg/cache/store/store.go index 2cf5746..55b17aa 100644 --- a/pkg/cache/store/store.go +++ b/pkg/cache/store/store.go @@ -52,7 +52,7 @@ type Store interface { LoadAllSerie(ctx context.Context) (model.SortedSeries, error) // InsertArticle 创建文章 - InsertArticle(ctx context.Context, article *model.Article) error + InsertArticle(ctx context.Context, article *model.Article, startID int) error // RemoveArticle 硬删除文章 RemoveArticle(ctx context.Context, id int) error // CleanArticles 清理回收站文章 @@ -68,7 +68,7 @@ type Store interface { // Driver 存储驱动 type Driver interface { // Init 数据库初始化, 建表, 加索引操作等 - Init(source string) (Store, error) + Init(name, source string) (Store, error) } // Register 注册驱动 @@ -106,5 +106,5 @@ func NewStore(name string, source string) (Store, error) { return nil, fmt.Errorf("store: unknown driver %q (forgotten import?)", name) } - return driver.Init(source) + return driver.Init(name, source) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 13e4af7..8c2c6b9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -46,8 +46,6 @@ type General struct { DescPrefix string `yaml:"descprefix"` // 文章描述前缀 Identifier string `yaml:"identifier"` // 文章截取标识 Length int `yaml:"length"` // 文章预览长度 - Trash int `yaml:"trash"` // 回收箱文章保留时间 - Clean int `yaml:"clean"` // 清理回收箱频率 Timezone string `yaml:"timezone"` // 时区 } @@ -56,7 +54,6 @@ type Disqus struct { ShortName string `yaml:"shortname"` PublicKey string `yaml:"publickey"` AccessToken string `yaml:"accesstoken"` - Interval int `yaml:"interval"` // 获取评论数量间隔 } // Twitter card