online_repostiroy.go 901 B

12345678910111213141516171819202122232425262728293031323334
  1. package repositories
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/redis/go-redis/v9"
  6. "sikey.com/websocket/models"
  7. )
  8. var _ OnlineRepository = (*onlineRepository)(nil)
  9. type OnlineRepository interface {
  10. SetOnline(ctx context.Context, o *models.Online) error
  11. Offline(ctx context.Context, o *models.Online) error
  12. }
  13. type onlineRepository struct {
  14. rdb *redis.Client
  15. }
  16. // Offline implements OnlineRepository.
  17. func (repo *onlineRepository) Offline(ctx context.Context, o *models.Online) error {
  18. return repo.rdb.Del(ctx, fmt.Sprintf("clients.%s.%s", o.ServerId, o.UserId)).Err()
  19. }
  20. // SetOnline implements OnlineRepository.
  21. func (repo *onlineRepository) SetOnline(ctx context.Context, o *models.Online) error {
  22. return repo.rdb.Set(ctx, fmt.Sprintf("clients.%s.%s", o.ServerId, o.UserId), o, 0).Err()
  23. }
  24. func NewOnlineRepository(rdb *redis.Client) OnlineRepository {
  25. return &onlineRepository{rdb: rdb}
  26. }