remove multi result and just use single result

This commit is contained in:
HJfod 2024-03-10 21:39:24 +02:00
parent ff5af54b07
commit b21fa5bc8a
2 changed files with 12 additions and 74 deletions

View file

@ -113,75 +113,18 @@ namespace server {
using Extract = decltype(ExtractServerReqParams(F));
using Result = Extract::Result;
using Query = Extract::Query;
using Cached = std::variant<ServerPromise<Result>, Result>;
private:
std::mutex m_cachedMutex;
// The result and the query used for cached value
std::optional<std::pair<Result, Query>> m_cached;
ServerPromise<Result> fetch(Query&& query) {
return ServerPromise<Result>([this, query = std::move(query)](auto resolve, auto reject, auto progress, auto cancelled) {
F(Query(query))
.then([this, resolve, query = std::move(query)](auto res) {
std::unique_lock lock(m_cachedMutex);
m_cached = { res, query };
lock.unlock();
resolve(res);
})
.expect([reject](auto err) {
reject(err);
})
.progress([progress](auto prog) {
progress(prog);
})
.link(cancelled);
});
}
public:
ServerPromise<Result> get(Query&& query) {
std::unique_lock lock(m_cachedMutex);
// Return cached value if there is one and the query matches
if (m_cached && m_cached->second == query) {
auto cached = m_cached->first;
lock.unlock();
return ServerPromise<Result>([cached = std::move(cached)](auto resolve, auto) {
resolve(std::move(cached));
});
}
lock.unlock();
return this->fetch(std::move(query));
}
ServerPromise<Result> refetch(Query&& query) {
// Clear cache
std::unique_lock lock(m_cachedMutex);
m_cached.reset();
lock.unlock();
// Fetch new value
return this->fetch(std::move(query));
}
};
template <auto F>
class ServerMultiResultCache final {
public:
using Extract = decltype(ExtractServerReqParams(F));
using Result = Extract::Result;
using Query = Extract::Query;
private:
std::mutex m_queriesMutex;
std::map<Query, Result> m_queries;
std::map<Query, Result> m_cached;
ServerPromise<Result> fetch(Query const& query) {
return ServerPromise<Result>([this, query = Query(query)](auto resolve, auto reject, auto progress, auto cancelled) {
F(Query(query))
.then([this, resolve, query = std::move(query)](auto res) {
std::unique_lock lock(m_queriesMutex);
m_queries[std::move(query)] = res;
std::unique_lock lock(m_cachedMutex);
m_cached[std::move(query)] = res;
lock.unlock();
resolve(res);
@ -198,10 +141,10 @@ namespace server {
public:
ServerPromise<Result> get(Query const& query) {
std::unique_lock lock(m_queriesMutex);
std::unique_lock lock(m_cachedMutex);
// Return cached value if there is one and the query matches
if (m_queries.contains(query)) {
auto cached = m_queries.at(query);
if (m_cached.contains(query)) {
auto cached = m_cached.at(query);
lock.unlock();
return ServerPromise<Result>([cached = std::move(cached)](auto resolve, auto) {
resolve(std::move(cached));
@ -213,8 +156,8 @@ namespace server {
ServerPromise<Result> refetch(Query const& query) {
// Clear cache for this query only
std::unique_lock lock (m_queriesMutex);
m_queries.erase(query);
std::unique_lock lock (m_cachedMutex);
m_cached.erase(query);
lock.unlock();
// Fetch new value
@ -223,14 +166,9 @@ namespace server {
// Clear all caches
void invalidateAll() {
std::unique_lock _(m_queriesMutex);
m_queries.clear();
std::unique_lock _(m_cachedMutex);
m_cached.clear();
}
};
}
// todo: default shared caching for endpoints for all users
// todo: (so it can be automatically cleared for example after leaving the geode layer)
// template <auto F>
// impl_cache::ServerResultCache<F> sharedCache() {}
}

View file

@ -71,7 +71,7 @@ protected:
CCNode* m_sprite = nullptr;
EventListener<PromiseEventFilter<ByteVector, server::ServerError>> m_listener;
// todo: use shared cache once impl'd
static inline server::impl_cache::ServerMultiResultCache<&server::getModLogo> s_cache = {};
static inline server::impl_cache::ServerResultCache<&server::getModLogo> s_cache = {};
bool init(std::string const& id, bool fetch) {
if (!CCNode::init())