blog/internal/async/ViewRecord.go

44 lines
901 B
Go

package async
import (
"blog/internal/consts"
"blog/third_party/database"
"context"
"log"
"math"
"github.com/google/uuid"
)
var viewRecordChannel chan ViewRecord
type ViewRecord struct {
Id string `json:"id"`
ViewTime int64 `json:"viewTime"`
IpAddr string `json:"ipAddr"`
RelId string `json:"relId"`
Title string `json:"title" gorm:"-"`
}
func init() {
f := math.Pow(2, 5)
viewRecordChannel = make(chan ViewRecord, int(f))
go func() {
for record := range viewRecordChannel {
log.Println("IP:", record.IpAddr, "点击了:", record.Title)
relId := record.RelId
record.Id = uuid.NewString()
database.GormTemplate.Table("blog_view_record").Create(&record)
ctx := context.Background()
database.RedisTemplate.HIncrBy(ctx, consts.REDIS_BLOG_VIEW_RECORD, relId, 1)
}
}()
}
func PublishViewRecord(record ViewRecord) {
viewRecordChannel <- record
}