12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package qznn
- import (
- "fmt"
- "sync"
- "time"
- "github.com/patrickmn/go-cache"
- "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)))
- })
- return onlinUserCache
- }
- func UserIsOnline(userID uint) bool {
- _, exists := getCacheInstance().Get(fmt.Sprintf("%d", userID))
- return exists
- }
- func UserActive(userID uint) {
- getCacheInstance().Set(fmt.Sprintf("%d", userID), 1, cache.DefaultExpiration)
- }
- func UserUnActive(userID uint) {
- getCacheInstance().Delete(fmt.Sprintf("%d", userID))
- }
|