123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package qznn
- import (
- "fmt"
- "sync"
- "time"
- "github.com/patrickmn/go-cache"
- "github.com/sirupsen/logrus"
- "gogs.daxia.dev/huanan/pkg.daxia.dev/parse"
- )
- var onlinUserCache *cache.Cache
- var onlinUserCacheLocker = sync.Mutex{}
- func getCacheInstance() *cache.Cache {
- onlinUserCacheLocker.Lock()
- defer onlinUserCacheLocker.Unlock()
- if onlinUserCache != nil {
- return onlinUserCache
- }
- onlinUserCache = cache.New(1*time.Minute, 2*time.Minute)
- onlinUserCache.OnEvicted(func(name string, val interface{}) {
- game := GetInstance()
- game.Leave(int32(parse.StringToInt64(name)), true)
- })
- return onlinUserCache
- }
- func UserIsOnline(userID uint) bool {
- _, exists := getCacheInstance().Get(fmt.Sprintf("%d", userID))
- return exists
- }
- func UserActive(userID uint) {
- logrus.Infof("UserActive:%d", userID)
- getCacheInstance().Set(fmt.Sprintf("%d", userID), 1, time.Minute)
- }
- func UserUnActive(userID uint) {
- logrus.Infof("UserUnActive:%d", userID)
- getCacheInstance().Delete(fmt.Sprintf("%d", userID))
- }
|