diff --git a/cache/mapcache.cpp b/cache/mapcache.cpp new file mode 100644 index 0000000..19eef95 --- /dev/null +++ b/cache/mapcache.cpp @@ -0,0 +1 @@ +#include "mapcache.h" diff --git a/cache/mapcache.h b/cache/mapcache.h new file mode 100644 index 0000000..4f0d00f --- /dev/null +++ b/cache/mapcache.h @@ -0,0 +1,37 @@ +#ifndef MAPCACHE_H +#define MAPCACHE_H +#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