Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
longpi1 committed Apr 14, 2024
1 parent 5a066dc commit e023949
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 54 deletions.
29 changes: 29 additions & 0 deletions relation/relation-service/httpserver/controller/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package controller

import (
"relation-service/libary/conf"
"relation-service/libary/log"
)

func VaildTypeAndPlatform(relationType int, platform int) bool {
mapConfig := conf.GetMapConfig()
var hasType, hasPlatform bool
for _, value := range mapConfig.TypeMap {
if value == relationType {
hasType = true
}
}

for _, value := range mapConfig.PlatformMap {
if value == platform {
hasPlatform = true
}
}
if !hasType {
log.Error("类型不存在")
}
if !hasPlatform {
log.Error("平台不存在")
}
return hasType && hasPlatform
}
4 changes: 3 additions & 1 deletion relation/relation-service/httpserver/controller/following.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controller

import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
)

func Following(c *gin.Context) {

Expand Down
11 changes: 1 addition & 10 deletions relation/relation-service/httpserver/controller/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"encoding/json"
"relation-service/libary/conf"
"relation-service/libary/constant"
"relation-service/libary/log"
"relation-service/libary/utils"
Expand Down Expand Up @@ -33,15 +32,7 @@ func validateRelationParams(params model.RelationParams) bool {
log.Error("操作类型错误")
return false
}
mapConfig := conf.GetMapConfig()
_, hasType := mapConfig.TypeMap[params.Type]
if !hasType {
log.Error("类型不存在")
return false
}
_, hasPlatform := mapConfig.PlatformMap[params.Platform]
if !hasPlatform {
log.Error("平台不存在")
if VaildTypeAndPlatform(utils.ConvertType(params.Type), utils.ConvertPlatform(params.Platform)) {
return false
}
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
package controller

import "github.com/gin-gonic/gin"
import (
"encoding/json"
"relation-service/libary/constant"
"relation-service/libary/utils"
"relation-service/model/dao/db/model"
"relation-service/model/service"

"github.com/gin-gonic/gin"
)

func RelationCount(c *gin.Context) {
var params model.RelationCountParams
err := json.NewDecoder(c.Request.Body).Decode(&params)
// 校验参数是否正确
if err != nil || !validateRelationCountParams(params) {
utils.RespError(c, constant.InvalidParam)
return
}
relationCountResponse, err := service.RelationCount(params)
if err != nil {
utils.RespError(c, err.Error())
return
}
utils.RespData(c, "获取关注数成功", relationCountResponse)
}

func validateRelationCountParams(params model.RelationCountParams) bool {
if VaildTypeAndPlatform(params.Type, params.Platform) {
return false
}
return true
}
1 change: 1 addition & 0 deletions relation/relation-service/libary/constant/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package constant

const (
WhereByID = "id = ?"
WhereByPlatform = "Platform = ?"
WhereByName = "name = ?"
WhereByUserID = "uid = ?"
WhereByType = "type = ?"
Expand Down
15 changes: 15 additions & 0 deletions relation/relation-service/libary/utils/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

import "relation-service/libary/conf"

// ConvertType 将string类型的类型转换为int
func ConvertType(relationType string) int {
typeMap := conf.GetMapConfig().TypeMap
return typeMap[relationType]
}

// ConvertPlatform 将string类型的平台转换为int
func ConvertPlatform(Platform string) int {
platform := conf.GetMapConfig().PlatformMap
return platform[Platform]
}
14 changes: 11 additions & 3 deletions relation/relation-service/model/dao/cache/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ package cache
import "fmt"

const (
RelationListRedisKey = "relation_list_%d_%s_%s"
// RelationListRedisKey 关注列表缓存
RelationListRedisKey = "relation_list_%d_%d_%d_%d"
//RelationCountRedisKey 关注数缓存
RelationCountRedisKey = "relation_count_%d_%d_%d"
)

func GetRelationListKey(UID int64, platform string, relationType string) string {
key := fmt.Sprintf(RelationListRedisKey, UID, platform, relationType)
func GetRelationListKey(UID int64, platform int, relationType int, status int) string {
key := fmt.Sprintf(RelationListRedisKey, UID, platform, relationType, status)
return key
}

func GetRelationCountKey(ResourceID int64, platform int, relationType int) string {
key := fmt.Sprintf(RelationCountRedisKey, ResourceID, platform, relationType)
return key
}
52 changes: 52 additions & 0 deletions relation/relation-service/model/dao/cache/relation_count.go
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
package cache

import (
"relation-service/libary/log"
localcache "relation-service/model/dao/cache/local_cache"
"relation-service/model/dao/cache/redis"
"relation-service/model/dao/db/model"
"time"
)

func GetRelationCountFromLocalCache(key string) (response model.RelationCountResponse, err error) {
if err = localcache.Get(key, response); err != nil {
log.Error("获取localcache评论数失败: ", key)
return response, err
}
return response, nil
}

// SetRelationCountToLocalCache 将关注数信息存入本地缓存
func SetRelationCountToLocalCache(key string, response model.RelationCountResponse) {
err := localcache.Set(key, response, time.Minute*120)
if err != nil {
log.Error("评论数存入localcache失败: ", key)
}
}

func GetRelationCountFromRedisCache(key string) (response model.RelationCountResponse, err error) {
if err = redis.Get(key, response); err != nil {
log.Error("获取redis评论数失败: ", key)
return response, err
}
return response, nil
}

// SetRelationCountToRedisCache 将关注数信息存入redis
func SetRelationCountToRedisCache(key string, response model.RelationCountResponse) {
err := redis.Set(key, response, time.Minute*120)
if err != nil {
log.Error("关注数存入redis 失败: ", key)
}
}

// DeleteRelationCountCache 删除相关关注数缓存
func DeleteRelationCountCache(key string) {
err := localcache.Delete(key)
if err != nil {
log.Error("删除localcache缓存失败", key)
}
err = redis.Del(key)
if err != nil {
log.Error("删除redis缓存失败", key)
}
}
16 changes: 8 additions & 8 deletions relation/relation-service/model/dao/db/model/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"gorm.io/gorm"
)

// Relation 关注信息表
// Relation 关系信息表
type Relation struct {
gorm.Model
Source string `gorm:"size:255;uniqueIndex:idx_relation_platform_source_uid;comment:'来源'"` //来源
UID int64 `gorm:"uniqueIndex:idx_relation_platform_source_uid;comment:'用户id,也就是发起关注行为的用户id'"` // 用户id,也就是发起关注行为的用户id
ResourceID int64 `gorm:"index;comment:'被关注的资源或者人id'"` // 被关注的资源或者人id
Platform int `gorm:"uniqueIndex:idx_relation_platform_source_uid;comment:'相关的平台'"` // 相关的平台
Status int `gorm:"comment:'状态'"` // 状态
Type int `gorm:"comment:'类型'"` // 类型
Ext string `gorm:"comment:'额外信息'"` // 额外信息
Source string `gorm:"size:255;comment:'来源'"` //来源
UID int64 `gorm:"uniqueIndex:idx_relation_uid_platform_type_status;comment:'用户id,也就是发起关注行为的用户id'"` // 用户id,也就是发起关注行为的用户id
ResourceID int64 `gorm:"index;comment:'被关注的资源或者人id'"` // 被关注的资源或者人id
Platform int `gorm:"uniqueIndex:idx_relation_uid_platform_type_status;comment:'相关的平台'"` // 相关的平台
Status int `gorm:"uniqueIndex:idx_relation_uid_platform_type_status;comment:'状态, 0关注,1互相关注, 2拉黑'"` // 状态 0关注,1互相关注, 2拉黑
Type int `gorm:"uniqueIndex:idx_relation_uid_platform_type_status;comment:'类型'"` // 类型
Ext string `gorm:"comment:'额外信息'"` // 额外信息
}

// TableName 自定义表名
Expand Down
23 changes: 15 additions & 8 deletions relation/relation-service/model/dao/db/model/relation_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
// RelationCount 用户关注数/粉丝数表
type RelationCount struct {
gorm.Model
ResourceId int64 `json:"resource_id"` // 资源/用户id
FansCount int64 `json:"fans_count"` // 粉丝数
FollowCount int64 `json:"follow_count"` // 关注数
Platform int64 `json:"platform"` // 相关的平台
Type int64 `json:"type"` // 资源类型
Ext string `json:"ext"` // 额外信息`
ResourceId int64 `gorm:"uniqueIndex:idx_relation_count_resource_id_platform_type;comment:'资源/用户id'"` // 资源/用户id
FansCount int64 `gorm:"comment:'粉丝数'"` // 粉丝数
FollowCount int64 `gorm:"comment:'关注数'"` // 关注数
Platform int64 `gorm:"uniqueIndex:idx_relation_count_resource_id_platform_type;comment:'相关的平台d'"` // 相关的平台
Type int64 `gorm:"uniqueIndex:idx_relation_count_resource_id_platform_type;comment:'资源类型'"` // 资源类型
Ext string `gorm:"comment:'额外信息'"` // 额外信息`
}

// TableName 自定义表名
Expand Down Expand Up @@ -48,9 +48,16 @@ func DeleteRelationCountWithTx(tx *gorm.DB, id uint) error {
return err
}

func FindRelationCountById(id int) (RelationCount, error) {
func FindRelationCountByParams(params RelationCountParams) (RelationCount, error) {
var relationCount RelationCount
err := db.GetClient().Where(constant.WhereByID, id).First(&relationCount).Error
client := db.GetClient()
if params.Platform >= 0 {
client.Where(constant.WhereByPlatform, params.Platform)
}
if params.Type >= 0 {
client.Where(constant.WhereByType, params.Type)
}
err := db.GetClient().Where(constant.WhereByResourceID, params.ResourceId).First(&relationCount).Error
return relationCount, err
}

Expand Down
10 changes: 3 additions & 7 deletions relation/relation-service/model/dao/db/model/relation_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ type RelationListParams struct {
}

type RelationCountParams struct {
Source string `json:"source"` //来源
UID int64 `json:"uid"` // 用户id,也就是发起关注行为的用户id
Type string `json:"type"` // 资源类型
ResourceID int64 `json:"resource_id"` // 被关注的资源或者人
Platform string `json:"platform"` // 相关的平台
Status int `json:"status"` // 状态
OpType string `json:"op_type"` // 操作类型
ResourceId int64 `json:"resource_id"` // 资源/用户id
Type int `json:"type"` // 资源类型
Platform int `json:"platform"` // 相关的平台
Ext string `json:"ext"` // 额外信息
}
16 changes: 12 additions & 4 deletions relation/relation-service/model/dao/db/model/relation_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ type RelationResponse struct {
Source string `json:"source"` //来源
UID int64 `json:"uid"` // 用户id,也就是发起关注行为的用户id
UserName string `json:"userName"`
Type string `json:"type"` // 资源类型
Type int `json:"type"` // 资源类型
ResourceID int64 `json:"resource_id"` // 被关注的资源或者人
ResourceName string `json:"resource_name"`
Platform string `json:"platform"` // 相关的平台
Platform int `json:"platform"` // 相关的平台
Status int `json:"status"` // 状态
Ext string `json:"ext"` // 额外信息
}

type RelationListResponse struct {
RelationResponse []RelationResponse
SubscribeNum int // 关注数
FansNum int // 粉丝数
FollowCount int // 关注数
FansCount int // 粉丝数
}

type RelationCountResponse struct {
ResourceID int64 `json:"uid"` // 用户/资源id
FollowCount int // 关注数
FansCount int // 粉丝数
Platform int `json:"platform"` // 相关的平台
Type int `json:"type"` // 资源类型
}
41 changes: 41 additions & 0 deletions relation/relation-service/model/data/get_relation_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package data

import (
"fmt"
"relation-service/libary/log"
"relation-service/model/dao/cache"
"relation-service/model/dao/db/model"
)

func GetRelationCount(params model.RelationCountParams) (countResponse model.RelationCountResponse, err error) {
key := cache.GetRelationCountKey(params.ResourceId, params.Platform, params.Type)
// 首先从缓存中获取对应数据
if countResponse, err = cache.GetRelationCountFromLocalCache(key); err == nil {
return countResponse, nil
}
if countResponse, err = cache.GetRelationCountFromRedisCache(key); err == nil {
return countResponse, nil
}
// 缓存中获取失败则从数据库中获取
relationCount, err := model.FindRelationCountByParams(params)
if err != nil {
log.Error("数据库获取关注数、粉丝数失败: %v", err)
return countResponse, fmt.Errorf("数据库获取关注数、粉丝数失败")
}
countResponse = formatRelationCountResponse(relationCount)

// 更新缓存
cache.SetRelationCountToLocalCache(key, countResponse)
cache.SetRelationCountToRedisCache(key, countResponse)
return
}

func formatRelationCountResponse(relationCount model.RelationCount) model.RelationCountResponse {
return model.RelationCountResponse{
ResourceID: relationCount.ResourceId,
Platform: int(relationCount.Platform),
Type: int(relationCount.Type),
FollowCount: int(relationCount.FollowCount),
FansCount: int(relationCount.FansCount),
}
}
5 changes: 3 additions & 2 deletions relation/relation-service/model/data/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"relation-service/libary/conf"
"relation-service/libary/log"
"relation-service/libary/utils"
"relation-service/model/dao/cache"
"relation-service/model/dao/db"
"relation-service/model/dao/db/model"
Expand All @@ -19,7 +20,7 @@ func Follow(params model.RelationParams) error {
return fmt.Errorf("数据库插入失败")
}
// 删除原有缓存
key := cache.GetRelationListKey(params.UID, params.Platform, params.Type)
key := cache.GetRelationListKey(params.UID, relation.Platform, relation.Type, params.Status)
cache.DeleteRelationCache(key)
return nil
}
Expand All @@ -32,7 +33,7 @@ func UnFollow(params model.RelationParams) error {
return fmt.Errorf("数据库删除失败")
}
// 删除原有缓存
key := cache.GetRelationListKey(params.UID, params.Platform, params.Type)
key := cache.GetRelationListKey(params.UID, utils.ConvertPlatform(params.Platform), utils.ConvertType(params.Type), params.Status)
cache.DeleteRelationCache(key)
return nil
}
Expand Down
7 changes: 0 additions & 7 deletions relation/relation-service/model/data/relation_count.go

This file was deleted.

1 change: 1 addition & 0 deletions relation/relation-service/model/service/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package service
Loading

0 comments on commit e023949

Please sign in to comment.