Use TCacheMap for saving the WebModules session

With this change, sessions are automatically "garbage collected" 24h after the
last request using this session.


git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1823 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2010-03-10 17:53:57 +00:00
parent 7bff1c4d7c
commit f6f7dce129
2 changed files with 11 additions and 8 deletions

View File

@@ -11,7 +11,8 @@
#include "znc.h"
#include <sstream>
map<CString, CSmartPtr<CWebSession> > CWebSock::m_mspSessions;
// Sessions are valid for a day, (24h, ...)
TCacheMap<CString, CSmartPtr<CWebSession> > CWebSock::m_mspSessions(24 * 60 * 60 * 1000);
CZNCTagHandler::CZNCTagHandler(CWebSock& WebSock) : CTemplateTagHandler(), m_WebSock(WebSock) {
}
@@ -601,11 +602,13 @@ CSmartPtr<CWebSession> CWebSock::GetSession() {
return m_spSession;
}
map<CString, CSmartPtr<CWebSession> >::const_iterator it = m_mspSessions.find(GetCookie("SessionId"));
CSmartPtr<CWebSession> *pSession = m_mspSessions.GetItem(GetCookie("SessionId"));
if (it != m_mspSessions.end()) {
DEBUG("Found existing session from cookie: [" + GetCookie("SessionId") + "] IsLoggedIn(" + CString(it->second->IsLoggedIn() ? "true" : "false") + ")");
return it->second;
if (pSession != NULL) {
// Refresh the timeout
m_mspSessions.AddItem((*pSession)->GetId(), *pSession);
DEBUG("Found existing session from cookie: [" + GetCookie("SessionId") + "] IsLoggedIn(" + CString((*pSession)->IsLoggedIn() ? "true" : "false") + ")");
return *pSession;
}
CString sSessionID;
@@ -617,10 +620,10 @@ CSmartPtr<CWebSession> CWebSock::GetSession() {
sSessionID = sSessionID.SHA256();
DEBUG("Auto generated session: [" + sSessionID + "]");
} while (m_mspSessions.find(sSessionID) != m_mspSessions.end());
} while (m_mspSessions.HasItem(sSessionID));
CSmartPtr<CWebSession> spSession(new CWebSession(sSessionID));
m_mspSessions.insert(make_pair(spSession->GetId(), spSession));
m_mspSessions.AddItem(spSession->GetId(), spSession);
return spSession;
}