1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package service
- import (
- "time"
- "github.com/patrickmn/go-cache"
- )
- type UserServiceInterface interface {
- GetUserMode(userId string) string
- SetUserMode(userId, mode string)
- ClearUserMode(userId string)
- GetUserSessionContext(userId string) string
- SetUserSessionContext(userId, content string)
- ClearUserSessionContext(userId string)
- }
- var _ UserServiceInterface = (*UserService)(nil)
- type UserService struct {
-
- cache *cache.Cache
- }
- func NewUserService() UserServiceInterface {
- return &UserService{cache: cache.New(time.Hour*2, time.Hour*5)}
- }
- func (s *UserService) GetUserMode(userId string) string {
- sessionContext, ok := s.cache.Get(userId + "_mode")
- if !ok {
- return ""
- }
- return sessionContext.(string)
- }
- func (s *UserService) SetUserMode(userId string, mode string) {
- s.cache.Set(userId+"_mode", mode, cache.DefaultExpiration)
- }
- func (s *UserService) ClearUserMode(userId string) {
- s.cache.Delete(userId + "_mode")
- }
- func (s *UserService) SetUserSessionContext(userId string, content string) {
- s.cache.Set(userId+"_content", content, cache.DefaultExpiration)
- }
- func (s *UserService) GetUserSessionContext(userId string) string {
- sessionContext, ok := s.cache.Get(userId + "_content")
- if !ok {
- return ""
- }
- return sessionContext.(string)
- }
- func (s *UserService) ClearUserSessionContext(userId string) {
- s.cache.Delete(userId + "_content")
- }
|