user.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package service
  2. import (
  3. "time"
  4. "github.com/eryajf/chatgpt-dingtalk/config"
  5. "github.com/patrickmn/go-cache"
  6. )
  7. // UserServiceInterface 用户业务接口
  8. type UserServiceInterface interface {
  9. GetUserMode(userId string) string
  10. SetUserMode(userId string, mode string)
  11. ClearUserMode(userId string)
  12. }
  13. var _ UserServiceInterface = (*UserService)(nil)
  14. // UserService 用戶业务
  15. type UserService struct {
  16. // 缓存
  17. cache *cache.Cache
  18. }
  19. // NewUserService 创建新的业务层
  20. func NewUserService() UserServiceInterface {
  21. return &UserService{cache: cache.New(time.Second*config.LoadConfig().SessionTimeout, time.Minute*10)}
  22. }
  23. // GetUserMode 获取当前对话模式
  24. func (s *UserService) GetUserMode(userId string) string {
  25. sessionContext, ok := s.cache.Get(userId)
  26. if !ok {
  27. return ""
  28. }
  29. return sessionContext.(string)
  30. }
  31. // SetUserMode 设置用户对话模式
  32. func (s *UserService) SetUserMode(userId string, mode string) {
  33. s.cache.Set(userId, mode, time.Second*config.LoadConfig().SessionTimeout)
  34. }
  35. // ClearUserMode 重置用户对话模式
  36. func (s *UserService) ClearUserMode(userId string) {
  37. s.cache.Delete(userId)
  38. }