| 17 | using namespace CppServer::HTTP; |
| 18 | |
| 19 | class Cache : public CppCommon::Singleton<Cache> |
| 20 | { |
| 21 | friend CppCommon::Singleton<Cache>; |
| 22 | |
| 23 | public: |
| 24 | std::string GetAllCache() |
| 25 | { |
| 26 | std::scoped_lock locker(_cache_lock); |
| 27 | std::string result; |
| 28 | result += "[\n"; |
| 29 | for (const auto& item : _cache) |
| 30 | { |
| 31 | result += " {\n"; |
| 32 | result += " \"key\": \"" + item.first + "\",\n"; |
| 33 | result += " \"value\": \"" + item.second + "\",\n"; |
| 34 | result += " },\n"; |
| 35 | } |
| 36 | result += "]\n"; |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | bool GetCacheValue(std::string_view key, std::string& value) |
| 41 | { |
| 42 | std::scoped_lock locker(_cache_lock); |
| 43 | auto it = _cache.find(key); |
| 44 | if (it != _cache.end()) |
| 45 | { |
| 46 | value = it->second; |
| 47 | return true; |
| 48 | } |
| 49 | else |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | void PutCacheValue(std::string_view key, std::string_view value) |
| 54 | { |
| 55 | std::scoped_lock locker(_cache_lock); |
| 56 | auto it = _cache.emplace(key, value); |
| 57 | if (!it.second) |
| 58 | it.first->second = value; |
| 59 | } |
| 60 | |
| 61 | bool DeleteCacheValue(std::string_view key, std::string& value) |
| 62 | { |
| 63 | std::scoped_lock locker(_cache_lock); |
| 64 | auto it = _cache.find(key); |
| 65 | if (it != _cache.end()) |
| 66 | { |
| 67 | value = it->second; |
| 68 | _cache.erase(it); |
| 69 | return true; |
| 70 | } |
| 71 | else |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | std::mutex _cache_lock; |
nothing calls this directly
no outgoing calls
no test coverage detected