From 79d69f4b65c9388b96bb9b99ecf2061efb6c3175 Mon Sep 17 00:00:00 2001 From: "Albert S." Date: Sun, 13 Oct 2024 15:14:19 +0200 Subject: [PATCH] cache: Introduce StringCache, switch to unordered_map, default to memory cache if fs cache dir not given --- cache/mapcache.h | 50 ++++++++++++++++++++++++++++++++++++++- qswiki.cpp | 2 +- sandbox/sandbox-linux.cpp | 6 ++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/cache/mapcache.h b/cache/mapcache.h index 0cd6089..2a9c08b 100644 --- a/cache/mapcache.h +++ b/cache/mapcache.h @@ -5,12 +5,13 @@ #include #include #include +#include "icache.h" /* Thread-Safe Key-Value store */ template class MapCache { private: - std::map cache; + std::unordered_map cache; mutable std::shared_mutex sharedMutex; public: @@ -34,6 +35,53 @@ template class MapCache std::lock_guard lock{sharedMutex}; this->cache.clear(); } + + void remove(const std::string &key) + { + std::lock_guard lock{sharedMutex}; + this->cache.erase(key); + } + + void removePrefix(const std::string &key) + { + std::lock_guard lock{sharedMutex}; + std::erase_if(this->cache, [key](const auto &item) + { + auto const& [k, v] = item; + return k.starts_with(std::string_view(key)); + }); + } + +}; + + +class StringCache : public MapCache, public ICache +{ + virtual std::optional get(std::string_view key) const override + { + return MapCache::find(std::string(key)); + } + + virtual void put(std::string_view key, std::string val) override + { + MapCache::set(std::string(key), val); + } + + virtual void remove(std::string_view key) override + { + MapCache::remove(std::string(key)); + } + + virtual void removePrefix(std::string_view prefix) + { + MapCache::removePrefix(std::string(prefix)); + } + + virtual void clear() override + { + MapCache::clear(); + } + }; #endif // MAPCACHE_H diff --git a/qswiki.cpp b/qswiki.cpp index f55d34f..f60df76 100644 --- a/qswiki.cpp +++ b/qswiki.cpp @@ -72,7 +72,7 @@ std::unique_ptr createCache(const ConfigVariableResolver &resolver) std::string path = resolver.getConfig("cache_fs_dir"); if(path == "") { - return std::make_unique(path); + return std::make_unique(); } return std::make_unique(path); } diff --git a/sandbox/sandbox-linux.cpp b/sandbox/sandbox-linux.cpp index 6038900..e212521 100644 --- a/sandbox/sandbox-linux.cpp +++ b/sandbox/sandbox-linux.cpp @@ -49,7 +49,11 @@ bool SandboxLinux::enable(std::vector fsPaths) } for(unsigned int i = 0; i < fsPaths.size(); i++) { - exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ | EXILE_FS_ALLOW_ALL_WRITE, fsPaths[i].c_str()); + std::string &path = fsPaths[i]; + if(path.size() > 0) + { + exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ | EXILE_FS_ALLOW_ALL_WRITE, path.c_str()); + } } policy->drop_caps = 1; policy->not_dumpable = 1;