Let's make (git) history!
This commit is contained in:
62
cache/fscache.cpp
vendored
Normal file
62
cache/fscache.cpp
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include "fscache.h"
|
||||
#include "../logger.h"
|
||||
|
||||
FsCache::FsCache(std::string path)
|
||||
{
|
||||
if(!std::filesystem::exists(path))
|
||||
{
|
||||
throw std::runtime_error { "Directory does not exist" };
|
||||
}
|
||||
this->path = path;
|
||||
}
|
||||
|
||||
std::string FsCache::getFilePath(std::string_view path) const
|
||||
{
|
||||
std::filesystem::path ps { path };
|
||||
std::string name = ps.filename();
|
||||
return std::filesystem::path { this->path } / name;
|
||||
}
|
||||
std::optional<std::string> FsCache::get(std::string_view key) const
|
||||
{
|
||||
std::string path = getFilePath(key);
|
||||
if(std::filesystem::exists(path))
|
||||
{
|
||||
return utils::readCompleteFile(path);
|
||||
}
|
||||
return { };
|
||||
}
|
||||
|
||||
void FsCache::put(std::string_view key, std::string val)
|
||||
{
|
||||
std::string path = std::filesystem::path { this->path } / key;
|
||||
std::fstream f1;
|
||||
f1.open(path, std::ios::out);
|
||||
f1 << val;
|
||||
}
|
||||
|
||||
void FsCache::remove(std::string_view key)
|
||||
{
|
||||
std::filesystem::remove_all(std::filesystem::path { this->path} / key);
|
||||
}
|
||||
|
||||
void FsCache::removePrefix(std::string_view prefix)
|
||||
{
|
||||
//TODO: lock dir
|
||||
for(auto &entry : std::filesystem::directory_iterator(std::filesystem::path { this->path }))
|
||||
{
|
||||
if(static_cast<std::string>(entry.path().filename()).find(prefix) == 0)
|
||||
{
|
||||
std::filesystem::remove_all(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FsCache::clear()
|
||||
{
|
||||
for(auto &entry : std::filesystem::directory_iterator(std::filesystem::path { this->path }))
|
||||
{
|
||||
std::filesystem::remove_all(entry);
|
||||
}
|
||||
}
|
20
cache/fscache.h
vendored
Normal file
20
cache/fscache.h
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef FSCACHE_H
|
||||
#define FSCACHE_H
|
||||
#include "icache.h"
|
||||
class FsCache : public ICache
|
||||
{
|
||||
private:
|
||||
std::string path;
|
||||
std::string getFilePath(std::string_view path) const;
|
||||
public:
|
||||
FsCache(std::string directory);
|
||||
std::optional<std::string> get(std::string_view key) const;
|
||||
void put(std::string_view key, std::string val);
|
||||
void remove(std::string_view key);
|
||||
void removePrefix(std::string_view prefix);
|
||||
void clear();
|
||||
using ICache::ICache;
|
||||
~FsCache() { }
|
||||
};
|
||||
|
||||
#endif // FSCACHE_H
|
18
cache/icache.h
vendored
Normal file
18
cache/icache.h
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef ICACHE_H
|
||||
#define ICACHE_H
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <optional>
|
||||
#include "../utils.h"
|
||||
class ICache
|
||||
{
|
||||
public:
|
||||
virtual std::optional<std::string> get(std::string_view key) const = 0;
|
||||
virtual void put(std::string_view key, std::string val) = 0;
|
||||
virtual void remove(std::string_view key) = 0;
|
||||
virtual void removePrefix(std::string_view prefix) = 0;
|
||||
virtual void clear() = 0;
|
||||
virtual ~ICache() { }
|
||||
};
|
||||
|
||||
#endif // ICACHE_H
|
Verwijs in nieuw issue
Block a user