From f91391d6a199b8699cec0426a73fd06055be3052 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 1 Jan 2022 18:53:51 +0000 Subject: Cache allows multiple key parts --- lib/cache.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/cache.h b/lib/cache.h index 2a651f0..cdc0a2e 100644 --- a/lib/cache.h +++ b/lib/cache.h @@ -2,13 +2,15 @@ #define CACHE_H #include "special_members.hpp" +#include #include #include -#include +#include -template class Cache { +template class Cache { public: using Ptr = std::shared_ptr; + using Key = std::tuple; Cache() = default; virtual ~Cache() = default; @@ -16,22 +18,23 @@ public: NO_COPY(Cache); [[nodiscard]] Ptr - get(const std::string & key) + get(const KeyParts &... keyparts) { + auto key = std::tie(keyparts...); if (auto e = cached.find(key); e != cached.end()) { return e->second; } - return cached.emplace(key, construct(key)).first->second; + return cached.emplace(key, construct(keyparts...)).first->second; } [[nodiscard]] virtual Ptr - construct(const std::string & key) const + construct(const KeyParts &... keyparts) const { - return std::make_shared(key); + return std::make_shared(keyparts...); } private: - std::map cached; + std::map> cached; }; // IWYU pragma: no_forward_declare Cache -- cgit v1.2.3