cache: Introduce StringCache, switch to unordered_map, default to memory cache if fs cache dir not given

Cette révision appartient à :
Albert S. 2024-10-13 15:14:19 +02:00
Parent bfeacb0510
révision 79d69f4b65
3 fichiers modifiés avec 55 ajouts et 3 suppressions

50
cache/mapcache.h externe
Voir le fichier

@ -5,12 +5,13 @@
#include <shared_mutex>
#include <optional>
#include <string>
#include "icache.h"
/* Thread-Safe Key-Value store */
template <class T> class MapCache
{
private:
std::map<std::string, T> cache;
std::unordered_map<std::string, T> cache;
mutable std::shared_mutex sharedMutex;
public:
@ -34,6 +35,53 @@ template <class T> class MapCache
std::lock_guard<std::shared_mutex> lock{sharedMutex};
this->cache.clear();
}
void remove(const std::string &key)
{
std::lock_guard<std::shared_mutex> lock{sharedMutex};
this->cache.erase(key);
}
void removePrefix(const std::string &key)
{
std::lock_guard<std::shared_mutex> 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<std::string>, public ICache
{
virtual std::optional<std::string> get(std::string_view key) const override
{
return MapCache<std::string>::find(std::string(key));
}
virtual void put(std::string_view key, std::string val) override
{
MapCache<std::string>::set(std::string(key), val);
}
virtual void remove(std::string_view key) override
{
MapCache<std::string>::remove(std::string(key));
}
virtual void removePrefix(std::string_view prefix)
{
MapCache<std::string>::removePrefix(std::string(prefix));
}
virtual void clear() override
{
MapCache<std::string>::clear();
}
};
#endif // MAPCACHE_H

Voir le fichier

@ -72,7 +72,7 @@ std::unique_ptr<ICache> createCache(const ConfigVariableResolver &resolver)
std::string path = resolver.getConfig("cache_fs_dir");
if(path == "")
{
return std::make_unique<NoCache>(path);
return std::make_unique<StringCache>();
}
return std::make_unique<FsCache>(path);
}

Voir le fichier

@ -49,7 +49,11 @@ bool SandboxLinux::enable(std::vector<std::string> 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;