-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
230 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 28 additions & 1 deletion
29
relation/relation-service/httpserver/controller/relation_count.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¶ms) | ||
// 校验参数是否正确 | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
relation/relation-service/model/dao/cache/relation_count.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions
41
relation/relation-service/model/data/get_relation_count.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package service |
Oops, something went wrong.