9 #ifndef CPPCOMMON_CACHE_FILECACHE_H
10 #define CPPCOMMON_CACHE_FILECACHE_H
21 #include <shared_mutex>
23 #include <string_view>
25 #include <unordered_map>
50 explicit operator bool()
const {
return !
empty(); }
81 std::pair<bool, std::string_view>
find(
const std::string& key);
88 std::pair<bool, std::string_view>
find(
const std::string& key,
Timestamp& timeout);
95 bool remove(
const std::string& key);
132 void watchdog(
const UtcTimestamp& utc = UtcTimestamp());
139 mutable std::shared_mutex _lock;
140 Timestamp _timestamp;
148 MemCacheEntry() =
default;
149 MemCacheEntry(
const std::string& v,
const Timestamp& ts = Timestamp(),
const Timespan& tp = Timespan()) : value(v), timestamp(ts), timespan(tp) {}
150 MemCacheEntry(std::string&& v,
const Timestamp& ts = Timestamp(),
const Timespan& tp = Timespan()) : value(v), timestamp(ts), timespan(tp) {}
153 struct FileCacheEntry
160 FileCacheEntry() =
default;
161 FileCacheEntry(
const std::string& pfx,
const InsertHandler& h,
const Timestamp& ts = Timestamp(),
const Timespan& tp = Timespan()) : prefix(pfx), handler(h), timestamp(ts), timespan(tp) {}
164 std::unordered_map<std::string, MemCacheEntry> _entries_by_key;
165 std::map<Timestamp, std::string> _entries_by_timestamp;
166 std::map<CppCommon::Path, FileCacheEntry> _paths_by_key;
167 std::map<Timestamp, CppCommon::Path> _paths_by_timestamp;
169 bool remove_internal(
const std::string& key);
170 bool insert_path_internal(
const CppCommon::Path& path,
const std::string& prefix,
const Timespan& timeout,
const InsertHandler& handler);
bool insert(const std::string &key, const std::string &value, const Timespan &timeout=Timespan(0))
Insert a new cache value with the given timeout into the file cache.
void watchdog(const UtcTimestamp &utc=UtcTimestamp())
Watchdog the file cache.
std::pair< bool, std::string_view > find(const std::string &key)
Try to find the cache value by the given key.
bool remove(const std::string &key)
Remove the cache value with the given key from the file cache.
bool remove_path(const CppCommon::Path &path)
Remove the cache path from the file cache.
FileCache(const FileCache &)=delete
bool emplace(std::string &&key, std::string &&value, const Timespan &timeout=Timespan(0))
Emplace a new cache value with the given timeout into the file cache.
bool insert_path(const CppCommon::Path &path, const std::string &prefix="/", const Timespan &timeout=Timespan(0), const InsertHandler &handler=[](FileCache &cache, const std::string &key, const std::string &value, const Timespan &timeout){ return cache.insert(key, value, timeout);})
Insert a new cache path with the given timeout into the file cache.
bool find_path(const CppCommon::Path &path)
Try to find the cache path.
size_t size() const
Get the file cache size.
FileCache & operator=(const FileCache &)=delete
void swap(FileCache &cache) noexcept
Swap two instances.
void clear()
Clear the memory cache.
FileCache & operator=(FileCache &&)=delete
std::function< bool(FileCache &cache, const std::string &key, const std::string &value, const Timespan &timeout)> InsertHandler
File cache insert handler type.
bool empty() const
Is the file cache empty?
FileCache(FileCache &&)=delete
Filesystem directory definition.
Filesystem file definition.
File cache inline implementation.
C++ Common project definitions.
Filesystem path definition.