39 lines
841 B
Go
39 lines
841 B
Go
package async
|
|
|
|
import (
|
|
"blog/internal/consts"
|
|
"blog/internal/model/blog"
|
|
"blog/third_party/database"
|
|
"context"
|
|
"log"
|
|
"math"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var articleChannel chan string
|
|
|
|
func init() {
|
|
f := math.Pow(2, 5)
|
|
articleChannel = make(chan string, int(f))
|
|
|
|
go func() {
|
|
ctx := context.Background()
|
|
for id := range articleChannel {
|
|
log.Println(id)
|
|
time := time.Now().UnixMilli()
|
|
|
|
var article *blog.BlogArticle
|
|
database.GormTemplate.Table("blog_articles").Where("id = ?", id).UpdateColumn("publish_time", time)
|
|
database.GormTemplate.Table("blog_articles").Where("id = ?", id).First(article)
|
|
|
|
database.RedisTemplate.ZAdd(ctx, consts.REDIS_BLOG_ARTICLE_LATEST, redis.Z{Score: float64(article.PublishTime), Member: article})
|
|
}
|
|
}()
|
|
}
|
|
|
|
func PublishArticle(id string) {
|
|
articleChannel <- id
|
|
}
|