#ifndef MAPCACHE_H #define MAPCACHE_H #include #include #include #include #include /* Thread-Safe Key-Value store */ template class MapCache { private: std::map cache; mutable std::shared_mutex sharedMutex; public: std::optional find(const std::string &key) const { std::shared_lock lock(this->sharedMutex); auto it = this->cache.find(key); if(it != this->cache.end()) { return it->second; } return {}; } void set(const std::string &key, const T &val) { std::lock_guard lock{sharedMutex}; this->cache[key] = val; } void clear() { std::lock_guard lock{sharedMutex}; this->cache.clear(); } }; #endif // MAPCACHE_H