-
Notifications
You must be signed in to change notification settings - Fork 2
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
7 changed files
with
199 additions
and
161 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -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) { | ||
// 实现同上 | ||
} | ||
|
||
// 其他方法 |
Oops, something went wrong.