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.
 
 
 
 

49 lines
1017 B

  1. package repo
  2. import (
  3. "gim/internal/logic/domain/group/model"
  4. "gim/pkg/db"
  5. "gim/pkg/gerrors"
  6. "strconv"
  7. "time"
  8. "github.com/go-redis/redis"
  9. )
  10. const GroupKey = "group:"
  11. type groupCache struct{}
  12. var GroupCache = new(groupCache)
  13. // Get 获取群组缓存
  14. func (c *groupCache) Get(groupId int64) (*model.Group, error) {
  15. var user model.Group
  16. err := db.RedisUtil.Get(GroupKey+strconv.FormatInt(groupId, 10), &user)
  17. if err != nil && err != redis.Nil {
  18. return nil, gerrors.WrapError(err)
  19. }
  20. if err == redis.Nil {
  21. return nil, nil
  22. }
  23. return &user, nil
  24. }
  25. // Set 设置群组缓存
  26. func (c *groupCache) Set(group *model.Group) error {
  27. err := db.RedisUtil.Set(GroupKey+strconv.FormatInt(group.Id, 10), group, 24*time.Hour)
  28. if err != nil {
  29. return gerrors.WrapError(err)
  30. }
  31. return nil
  32. }
  33. // Del 删除群组缓存
  34. func (c *groupCache) Del(groupId int64) error {
  35. _, err := db.RedisCli.Del(GroupKey + strconv.FormatInt(groupId, 10)).Result()
  36. if err != nil {
  37. return gerrors.WrapError(err)
  38. }
  39. return nil
  40. }