You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_cache.go 1.0 KiB

1 week ago
6 days ago
1 week ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package repo
  2. import (
  3. "egg-im/internal/business/domain/user/model"
  4. "egg-im/pkg/db"
  5. "egg-im/pkg/gerrors"
  6. "strconv"
  7. "time"
  8. "github.com/go-redis/redis"
  9. )
  10. const (
  11. UserKey = "user:"
  12. UserExpire = 2 * time.Hour
  13. )
  14. type userCache struct{}
  15. var UserCache = new(userCache)
  16. // Get 获取用户缓存
  17. func (c *userCache) Get(userId int64) (*model.User, error) {
  18. var user model.User
  19. err := db.RedisUtil.Get(UserKey+strconv.FormatInt(userId, 10), &user)
  20. if err != nil && err != redis.Nil {
  21. return nil, gerrors.WrapError(err)
  22. }
  23. if err == redis.Nil {
  24. return nil, nil
  25. }
  26. return &user, nil
  27. }
  28. // Set 设置用户缓存
  29. func (c *userCache) Set(user model.User) error {
  30. err := db.RedisUtil.Set(UserKey+strconv.FormatInt(user.Id, 10), user, UserExpire)
  31. if err != nil {
  32. return gerrors.WrapError(err)
  33. }
  34. return nil
  35. }
  36. // Del 删除用户缓存
  37. func (c *userCache) Del(userId int64) error {
  38. _, err := db.RedisCli.Del(UserKey + strconv.FormatInt(userId, 10)).Result()
  39. if err != nil {
  40. return gerrors.WrapError(err)
  41. }
  42. return nil
  43. }