43 lines
866 B
Go
43 lines
866 B
Go
package async
|
|
|
|
import (
|
|
"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, "blog:view:record", relId, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
}
|
|
|
|
func PublishViewRecord(record ViewRecord) {
|
|
viewRecordChannel <- record
|
|
}
|