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.

group_cache.go 1.6 KiB

2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // GetLimit 获取群组缓存
  26. func (c *groupCache) GetLimit(limit string, groupId int64) (*model.Group, error) {
  27. var user model.Group
  28. err := db.RedisUtil.Get(GroupKey+limit+strconv.FormatInt(groupId, 10), &user)
  29. if err != nil && err != redis.Nil {
  30. return nil, gerrors.WrapError(err)
  31. }
  32. if err == redis.Nil {
  33. return nil, nil
  34. }
  35. return &user, nil
  36. }
  37. // Set 设置群组缓存
  38. func (c *groupCache) Set(group *model.Group) error {
  39. err := db.RedisUtil.Set(GroupKey+strconv.FormatInt(group.Id, 10), group, 24*time.Hour)
  40. if err != nil {
  41. return gerrors.WrapError(err)
  42. }
  43. return nil
  44. }
  45. // SetLimit 设置群组缓存
  46. func (c *groupCache) SetLimit(limit string, group *model.Group) error {
  47. err := db.RedisUtil.Set(GroupKey+limit+strconv.FormatInt(group.Id, 10), group, 24*time.Hour)
  48. if err != nil {
  49. return gerrors.WrapError(err)
  50. }
  51. return nil
  52. }
  53. // Del 删除群组缓存
  54. func (c *groupCache) Del(groupId int64) error {
  55. _, err := db.RedisCli.Del(GroupKey + strconv.FormatInt(groupId, 10)).Result()
  56. if err != nil {
  57. return gerrors.WrapError(err)
  58. }
  59. return nil
  60. }