44 lines
1023 B
Go
44 lines
1023 B
Go
package service
|
|
|
|
import (
|
|
"Blog/internal/client"
|
|
"Blog/internal/consts"
|
|
"Blog/internal/repository"
|
|
"context"
|
|
)
|
|
|
|
var ViewRecordService *viewRecordService = newViewRecordService()
|
|
|
|
type viewRecordService struct {
|
|
}
|
|
|
|
func newViewRecordService() *viewRecordService {
|
|
//go initViewRecordData()
|
|
|
|
return &viewRecordService{}
|
|
}
|
|
|
|
func (*viewRecordService) GetRecord(id string) int64 {
|
|
var count int64 = 0
|
|
ctx := context.Background()
|
|
// exists := client.RedisClient().Exists(ctx, consts.REDIS_BLOG_VIEW_RECORD).Val()
|
|
exists := client.RedisClient().HExists(ctx, consts.REDIS_BLOG_VIEW_RECORD, id).Val()
|
|
if exists {
|
|
i, err := client.RedisClient().HGet(ctx, consts.REDIS_BLOG_VIEW_RECORD, id).Int64()
|
|
if err != nil {
|
|
return i
|
|
}
|
|
|
|
}
|
|
|
|
err := repository.ViewRecordRep().Table(consts.TABLE_BLOG_VIEW_RECORD).Where("rel_id = ?", id).Count(&count).Error
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
err = client.RedisClient().HIncrBy(ctx, consts.REDIS_BLOG_VIEW_RECORD, id, count).Err()
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return count
|
|
}
|