31 lines
896 B
C
31 lines
896 B
C
|
#pragma once
|
||
|
|
||
|
#include <chrono>
|
||
|
#include "Types.h"
|
||
|
#include <unordered_map>
|
||
|
|
||
|
namespace VWeb {
|
||
|
using ms = std::chrono::duration<double, std::milli>;
|
||
|
struct SessionData {
|
||
|
virtual ~SessionData() = default;
|
||
|
template <class T> T *As() { return reinterpret_cast<T *>(this); }
|
||
|
};
|
||
|
struct Session {
|
||
|
std::string Id;
|
||
|
long TTLSeconds = 1440; // 24 minutes 1440 seconds
|
||
|
bool IsValid();
|
||
|
void Update();
|
||
|
void Remove(const std::string &key);
|
||
|
bool Has(const std::string &key);
|
||
|
Ref<SessionData> &operator[](const std::string &key) { return m_Data[key]; }
|
||
|
void SetSessionData(const std::string &key, const Ref<SessionData> &data) {
|
||
|
m_Data[key] = data;
|
||
|
}
|
||
|
bool ContainsData() { return !m_Data.empty(); }
|
||
|
|
||
|
protected:
|
||
|
std::chrono::time_point<std::chrono::system_clock, ms> m_LastCall =
|
||
|
std::chrono::system_clock::now();
|
||
|
std::unordered_map<std::string, Ref<SessionData>> m_Data;
|
||
|
};
|
||
|
}
|