golang-im聊天
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.
 
 
 
 

52 lines
1.0 KiB

  1. package repo
  2. import (
  3. "gim/internal/business/domain/user/model"
  4. "gim/pkg/db"
  5. "gim/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. }