From f6f7dce1298440e4b01bdfe60e0ffe43bdcb3b13 Mon Sep 17 00:00:00 2001 From: psychon Date: Wed, 10 Mar 2010 17:53:57 +0000 Subject: [PATCH] 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 --- WebModules.cpp | 17 ++++++++++------- WebModules.h | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/WebModules.cpp b/WebModules.cpp index 4358eb5f..aec6f25e 100644 --- a/WebModules.cpp +++ b/WebModules.cpp @@ -11,7 +11,8 @@ #include "znc.h" #include -map > CWebSock::m_mspSessions; +// Sessions are valid for a day, (24h, ...) +TCacheMap > CWebSock::m_mspSessions(24 * 60 * 60 * 1000); CZNCTagHandler::CZNCTagHandler(CWebSock& WebSock) : CTemplateTagHandler(), m_WebSock(WebSock) { } @@ -601,11 +602,13 @@ CSmartPtr CWebSock::GetSession() { return m_spSession; } - map >::const_iterator it = m_mspSessions.find(GetCookie("SessionId")); + CSmartPtr *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 CWebSock::GetSession() { sSessionID = sSessionID.SHA256(); DEBUG("Auto generated session: [" + sSessionID + "]"); - } while (m_mspSessions.find(sSessionID) != m_mspSessions.end()); + } while (m_mspSessions.HasItem(sSessionID)); CSmartPtr spSession(new CWebSession(sSessionID)); - m_mspSessions.insert(make_pair(spSession->GetId(), spSession)); + m_mspSessions.AddItem(spSession->GetId(), spSession); return spSession; } diff --git a/WebModules.h b/WebModules.h index d3065a05..b5929b40 100644 --- a/WebModules.h +++ b/WebModules.h @@ -157,7 +157,7 @@ private: CString m_sPage; // Gets filled by ResolveModule() CSmartPtr m_spSession; - static map > m_mspSessions; + static TCacheMap > m_mspSessions; }; #endif // !_WEBMODULES_H