save caches
This commit is contained in:
parent
b870befa91
commit
e2f2442aa1
@ -7,13 +7,20 @@ import (
|
|||||||
"mt"
|
"mt"
|
||||||
"q5"
|
"q5"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FriendsMgr struct {
|
type FriendsMgr struct {
|
||||||
cs.MsgHandlerImpl
|
cs.MsgHandlerImpl
|
||||||
users map[string]*User
|
users map[string]*User
|
||||||
friendships map[string][]*Friendship
|
searchCaches map[string]SearchCache
|
||||||
pendingReqs map[string]map[string]bool
|
friendships map[string][]*Friendship
|
||||||
|
pendingReqs map[string]map[string]bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type SearchCache struct {
|
||||||
|
Users []*User
|
||||||
|
LastModified time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
var gameDBStore *q5.Mysql
|
var gameDBStore *q5.Mysql
|
||||||
@ -44,6 +51,7 @@ func (fm *FriendsMgr) init() {
|
|||||||
fm.loadFriendshipsFromDB(friendDBStore)
|
fm.loadFriendshipsFromDB(friendDBStore)
|
||||||
// 加载等待验证好友请求 列表
|
// 加载等待验证好友请求 列表
|
||||||
fm.loadPendingRequestsFromDB(friendDBStore)
|
fm.loadPendingRequestsFromDB(friendDBStore)
|
||||||
|
fm.searchCaches = make(map[string]SearchCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fm *FriendsMgr) unInit() {
|
func (fm *FriendsMgr) unInit() {
|
||||||
@ -52,8 +60,12 @@ func (fm *FriendsMgr) unInit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fm *FriendsMgr) searchFriends(searchKeyword string) []*User {
|
func (fm *FriendsMgr) searchFriends(searchKeyword string) []*User {
|
||||||
// By default, Search result save to caches..., key: search keyword, value: serial(search result),
|
if cachedResult, ok := fm.searchCaches[searchKeyword]; ok {
|
||||||
// And,cache expired: 2days
|
if time.Since(cachedResult.LastModified) <= 10*time.Hour {
|
||||||
|
return cachedResult.Users
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
listFriend := make([]*User, 10)
|
listFriend := make([]*User, 10)
|
||||||
lowercaseQuery := strings.ToLower(searchKeyword)
|
lowercaseQuery := strings.ToLower(searchKeyword)
|
||||||
for _, u := range fm.users {
|
for _, u := range fm.users {
|
||||||
@ -65,6 +77,14 @@ func (fm *FriendsMgr) searchFriends(searchKeyword string) []*User {
|
|||||||
listFriend = append(listFriend, uEntity)
|
listFriend = append(listFriend, uEntity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add to caches
|
||||||
|
fm.searchCaches[searchKeyword] = SearchCache{
|
||||||
|
Users: listFriend,
|
||||||
|
LastModified: time.Now(),
|
||||||
|
}
|
||||||
|
fm.clearExpiredCaches()
|
||||||
|
|
||||||
return listFriend
|
return listFriend
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,6 +250,15 @@ func (fm *FriendsMgr) addFriendshipToMap(accountID string, friendship *Friendshi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fm *FriendsMgr) RegisterUser(accountId string, username string) {
|
func (fm *FriendsMgr) registerUser(accountId string, username string) {
|
||||||
fm.users[accountId] = &User{AccountId: accountId, Username: username}
|
fm.users[accountId] = &User{AccountId: accountId, Username: username}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fm *FriendsMgr) clearExpiredCaches() {
|
||||||
|
expirationTime := time.Now().Add(-24 * time.Hour)
|
||||||
|
for key, entry := range fm.searchCaches {
|
||||||
|
if entry.LastModified.Before(expirationTime) {
|
||||||
|
delete(fm.searchCaches, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user