-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <Infrastructure/Cache/Cache.h> | ||
#include <format> | ||
|
||
namespace evento { | ||
|
||
std::string CacheManager::generateKey(http::verb verb, | ||
urls::url_view url, | ||
const std::initializer_list<urls::param>& params) { | ||
std::string key = std::format("{}|{}", url.data(), static_cast<int>(verb)); | ||
for (const auto& param : params) { | ||
key += std::format("|{}={}", param.key, param.value); | ||
} | ||
return key; | ||
} | ||
|
||
bool CacheManager::isExpired(const CacheEntry& entry) { | ||
using namespace std::chrono_literals; | ||
return std::chrono::steady_clock::now() - entry.insertTime >= 1h; | ||
} | ||
|
||
void CacheManager::insert(const std::string& key, const CacheEntry& entry) { | ||
auto it = _cacheMap.find(key); | ||
if (it != _cacheMap.end()) { | ||
_cacheList.erase(it->second); | ||
currentCacheSize -= it->second->second.size; | ||
} | ||
|
||
_cacheList.emplace_front(key, entry); | ||
_cacheMap[key] = _cacheList.begin(); | ||
currentCacheSize += entry.size; | ||
|
||
while (currentCacheSize > MAX_CACHE_SIZE) { | ||
auto last = _cacheList.end(); | ||
--last; | ||
currentCacheSize -= last->second.size; | ||
_cacheMap.erase(last->first); | ||
_cacheList.pop_back(); | ||
} | ||
} | ||
|
||
std::optional<CacheEntry> CacheManager::get(std::string const& key) { | ||
if (isExpired(_cacheMap[key]->second)) { | ||
currentCacheSize -= _cacheMap[key]->second.size; | ||
_cacheList.erase(_cacheMap[key]); | ||
_cacheMap.erase(key); | ||
return std::nullopt; | ||
} | ||
auto it = _cacheMap.find(key); | ||
if (it == _cacheMap.end()) { | ||
return std::nullopt; | ||
} | ||
return it->second->second; | ||
} | ||
|
||
void CacheManager::load() {} | ||
|
||
void CacheManager::save() {} | ||
|
||
void CacheManager::saveToDisk() {} | ||
|
||
std::string CacheManager::generateFilename(urls::url_view url) { | ||
return ""; | ||
} | ||
|
||
} // namespace evento |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#pragma once | ||
|
||
#include <boost/beast/http.hpp> | ||
#include <boost/url.hpp> | ||
#include <boost/url/url_view.hpp> | ||
#include <chrono> | ||
#include <list> | ||
#include <nlohmann/json.hpp> | ||
#include <optional> | ||
#include <unordered_map> | ||
|
||
namespace evento { | ||
|
||
namespace beast = boost::beast; | ||
namespace http = beast::http; | ||
namespace urls = boost::urls; | ||
|
||
struct CacheEntry { | ||
nlohmann::basic_json<> result; | ||
std::chrono::steady_clock::time_point insertTime; | ||
std::size_t size; //cache size | ||
}; | ||
|
||
class CacheManager { | ||
public: | ||
CacheManager() { load(); } | ||
~CacheManager() { save(); } | ||
|
||
static std::string generateKey(http::verb verb, | ||
urls::url_view url, | ||
const std::initializer_list<urls::param>& params); | ||
|
||
static std::string generateFilename(urls::url_view url); | ||
|
||
static bool isExpired(const CacheEntry& entry); | ||
|
||
void insert(const std::string& key, const CacheEntry& entry); | ||
|
||
std::optional<CacheEntry> get(std::string const& key); | ||
|
||
static constexpr size_t MAX_CACHE_SIZE = 64 * 1024 * 1024; | ||
|
||
private: | ||
void load(); | ||
void save(); | ||
|
||
void saveToDisk(); | ||
|
||
std::list<std::pair<std::string, CacheEntry>> _cacheList; | ||
std::unordered_map<std::string, decltype(_cacheList.begin())> _cacheMap; | ||
inline static size_t currentCacheSize = 0; | ||
}; | ||
|
||
} // namespace evento |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.