Skip to content

Commit

Permalink
修改内部调用
Browse files Browse the repository at this point in the history
  • Loading branch information
ygxiaobai111 committed Aug 27, 2023
2 parents a9d168f + 92ebd87 commit 96c6b89
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 161 deletions.
27 changes: 8 additions & 19 deletions pkg/pb/relation_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 8 additions & 19 deletions pkg/pb/user_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 17 additions & 42 deletions server/user-center/cache/relationCache.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
// cache.go

package cache

import (
"context"
"github.com/go-redis/redis/v8"
"strconv"
"user-center/pkg/util"
)
import "time"

func NewRelationCache(ctx context.Context) *RedisCache {
return &RedisCache{NewRedisClient(ctx)}
type Cache struct {
data map[string]interface{}
ttl map[string]time.Time
}

// CacheChangeUserCount 更新缓存中用户的关注或粉丝数量
func CacheChangeUserCount(userID int64, count int, category string) {
cache := NewRelationCache(context.Background())

// 根据 category 构建缓存键
key := CacheChangeUserCountKey(userID, category)

// 获取当前缓存中的数量
currentCountStr, err := cache.Get(context.Background(), key).Result()
if err == redis.Nil {
// 如果缓存中不存在该键,则默认设置为 0
currentCountStr = "0"
} else if err != nil {
// 处理其他 Redis 错误
util.LogrusObj.Error("<relationCache> : ", err)
return
func NewCache() *Cache {
return &Cache{
data: make(map[string]interface{}),
ttl: make(map[string]time.Time),
}
}

// 将字符串转换为整数
currentCount := util.StrToInt64(currentCountStr)

// 更新数量
newCount := int(currentCount) + count

// 更新到缓存中
err = cache.Set(context.Background(), key, strconv.Itoa(newCount), 0).Err()
if err != nil {
util.LogrusObj.Error("<relationCache> : ", err)
return
}
func (c *Cache) Get(key string) (interface{}, bool) {
// 实现同上
}

// 取关时从缓存删除Key
func DelCacheFollow(uId, followId uint) error {
cache := NewRelationCache(context.Background())
return cache.
Del(context.Background(), GenFollowUserCacheKey(uId, followId)).
Err()
func (c *Cache) Set(key string, val interface{}, expire time.Duration) {
// 实现同上
}

// 其他方法
Loading

0 comments on commit 96c6b89

Please sign in to comment.