39
Server/SessionStore.h
Normal file
39
Server/SessionStore.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef __SESSIONSTORE_H__
|
||||
#define __SESSIONSTORE_H__
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
#include <shared_mutex>
|
||||
|
||||
namespace Older {
|
||||
|
||||
struct SessionData {
|
||||
int userId;
|
||||
std::string refreshToken;
|
||||
std::chrono::system_clock::time_point expireTime;
|
||||
std::chrono::system_clock::time_point refreshTime;
|
||||
};
|
||||
|
||||
class SessionStore {
|
||||
public:
|
||||
SessionStore(boost::asio::io_context &ioContext);
|
||||
std::string addSession(int userId, std::chrono::minutes sessionLifetime = std::chrono::minutes{1440}, // 24小时
|
||||
std::chrono::minutes refreshInterval = std::chrono::minutes{15});
|
||||
void removeSession(const std::string &token);
|
||||
std::pair<bool, std::string> validateAndRefresh(const std::string &token);
|
||||
SessionData at(const std::string &token);
|
||||
|
||||
protected:
|
||||
void startCleanupTask();
|
||||
void cleanupExpiredSessions();
|
||||
|
||||
private:
|
||||
boost::asio::io_context &m_ioContext;
|
||||
boost::asio::steady_timer m_cleanupTimer;
|
||||
std::shared_mutex m_mutex;
|
||||
std::unordered_map<std::string, SessionData> m_sessions;
|
||||
std::unordered_map<std::string, std::string> m_refreshMap;
|
||||
};
|
||||
} // namespace Older
|
||||
|
||||
#endif // __SESSIONSTORE_H__
|
Reference in New Issue
Block a user