123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package service
- import (
- "strings"
- "time"
- "unicode/utf8"
- "github.com/eryajf/chatgpt-dingtalk/config"
- "github.com/patrickmn/go-cache"
- )
- type UserServiceInterface interface {
- GetUserSessionContext(userId string) string
- SetUserSessionContext(userId string, question, reply string)
- ClearUserSessionContext(userId string, msg string) bool
- }
- var _ UserServiceInterface = (*UserService)(nil)
- type UserService struct {
-
- cache *cache.Cache
- }
- func (s *UserService) ClearUserSessionContext(userId string, msg string) bool {
- if strings.Contains(msg, "我要问下一个问题") && utf8.RuneCountInString(msg) < 20 {
- s.cache.Delete(userId)
- return true
- }
- return false
- }
- func NewUserService() UserServiceInterface {
- return &UserService{cache: cache.New(time.Second*config.LoadConfig().SessionTimeout, time.Minute*10)}
- }
- func (s *UserService) GetUserSessionContext(userId string) string {
- sessionContext, ok := s.cache.Get(userId)
- if !ok {
- return ""
- }
- return sessionContext.(string)
- }
- func (s *UserService) SetUserSessionContext(userId string, question, reply string) {
- value := question + "\n" + reply
- s.cache.Set(userId, value, time.Second*config.LoadConfig().SessionTimeout)
- }
|