1
This commit is contained in:
parent
6d196eb844
commit
6d346a3819
@ -1,6 +1,7 @@
|
||||
#include "precompile.h"
|
||||
|
||||
#include <a8/openssl.h>
|
||||
#include <a8/timer.h>
|
||||
|
||||
#include "cachemgr.h"
|
||||
#include "app.h"
|
||||
@ -12,11 +13,22 @@
|
||||
void CacheMgr::Init()
|
||||
{
|
||||
INIT_LIST_HEAD(&friend_list_);
|
||||
curr_entry_ = &friend_list_;
|
||||
a8::Timer::Instance()->AddRepeatTimerAndAttach
|
||||
(
|
||||
1000 * 10,
|
||||
a8::XParams(),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
CacheMgr::Instance()->RemoveTimeoutFriend();
|
||||
},
|
||||
&timer_attacher_.timer_list_
|
||||
);
|
||||
}
|
||||
|
||||
void CacheMgr::UnInit()
|
||||
{
|
||||
|
||||
timer_attacher_.ClearTimerList();
|
||||
}
|
||||
|
||||
void CacheMgr::_SS_IM_UserOnline(f8::MsgHdr& hdr, const ss::SS_IM_UserOnline& msg)
|
||||
@ -47,6 +59,7 @@ void CacheMgr::_SS_IM_UserOnline(f8::MsgHdr& hdr, const ss::SS_IM_UserOnline& ms
|
||||
friend_data->svr_node = svr_node;
|
||||
}
|
||||
}
|
||||
friend_data->last_active_tick = a8::XGetTickCount();
|
||||
TypeConvert::Convert(user_info.base_data(), friend_data->base_data);
|
||||
TypeConvert::Convert(user_info.temp_custom_data(), friend_data->temp_custom_data);
|
||||
}
|
||||
@ -66,6 +79,7 @@ void CacheMgr::_SS_IM_UserOffline(f8::MsgHdr& hdr, const ss::SS_IM_UserOffline&
|
||||
list_del_init(&friend_data->human_entry);
|
||||
}
|
||||
friend_data->svr_node = nullptr;
|
||||
friend_data->last_active_tick = a8::XGetTickCount();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -90,3 +104,30 @@ Friend* CacheMgr::GetFriendData(const std::string& account_id)
|
||||
auto itr = friend_hash_.find(account_id);
|
||||
return itr != friend_hash_.end() ? itr->second : nullptr;
|
||||
}
|
||||
|
||||
void CacheMgr::RemoveTimeoutFriend()
|
||||
{
|
||||
if (friend_hash_.size() < 10000 * 50) {
|
||||
return;
|
||||
}
|
||||
int num = 0;
|
||||
long long tick = a8::XGetTickCount();
|
||||
while (!list_is_last(curr_entry_, &friend_list_) && num++ < 1000) {
|
||||
list_head* p = curr_entry_;
|
||||
curr_entry_ = curr_entry_->next;
|
||||
|
||||
Friend* friend_data = list_entry(p, Friend, cache_entry);
|
||||
long long inactive_tick = tick - friend_data->last_active_tick;
|
||||
if ((friend_data->base_data.online && inactive_tick > 1000 * 3600 * 24 * 2) ||
|
||||
(!friend_data->base_data.online && inactive_tick > 1000 * 3600 * 24)) {
|
||||
if (!list_empty(&friend_data->human_entry)) {
|
||||
list_del_init(&friend_data->human_entry);
|
||||
}
|
||||
list_del_init(&friend_data->cache_entry);
|
||||
friend_hash_.erase(friend_data->base_data.account_id);
|
||||
}
|
||||
}
|
||||
if (list_is_last(curr_entry_, &friend_list_)) {
|
||||
curr_entry_ = &friend_list_;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include <a8/timer_attacher.h>
|
||||
|
||||
#include "ss_proto.pb.h"
|
||||
|
||||
class CacheMgr : public a8::Singleton<CacheMgr>
|
||||
@ -23,7 +27,12 @@ class CacheMgr : public a8::Singleton<CacheMgr>
|
||||
Friend* GetFriendData(const std::string& account_id);
|
||||
|
||||
private:
|
||||
void RemoveTimeoutFriend();
|
||||
|
||||
private:
|
||||
a8::TimerAttacher timer_attacher_;
|
||||
|
||||
std::map<std::string, Friend*> friend_hash_;
|
||||
list_head friend_list_;
|
||||
list_head* curr_entry_ = nullptr;
|
||||
};
|
||||
|
@ -37,6 +37,7 @@ struct Friend
|
||||
BaseUserData base_data;
|
||||
UserTempCustomData temp_custom_data;
|
||||
|
||||
long long last_active_tick = 0;
|
||||
list_head cache_entry;
|
||||
list_head human_entry;
|
||||
struct SvrNode* svr_node = nullptr;
|
||||
|
Loading…
x
Reference in New Issue
Block a user