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.1 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. MasterKey = "master:"
  12. MasterExpire = 30 * time.Minute
  13. )
  14. type masterCache struct{}
  15. var MasterCache = new(masterCache)
  16. // Get 获取用户缓存
  17. func (c *masterCache) Get(masterId int64) (*model.Master, error) {
  18. var master model.Master
  19. err := db.RedisUtil.Get(MasterKey+strconv.FormatInt(masterId, 10), &master)
  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 &master, nil
  27. }
  28. // Set 设置用户缓存
  29. func (c *masterCache) Set(master model.Master) error {
  30. err := db.RedisUtil.Set(MasterKey+strconv.FormatInt(master.MasterId, 10), master, MasterExpire)
  31. if err != nil {
  32. return gerrors.WrapError(err)
  33. }
  34. return nil
  35. }
  36. // Del 删除用户缓存
  37. func (c *masterCache) Del(masterId int64) error {
  38. _, err := db.RedisCli.Del(MasterKey + strconv.FormatInt(masterId, 10)).Result()
  39. if err != nil {
  40. return gerrors.WrapError(err)
  41. }
  42. return nil
  43. }