CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
filecache.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_CACHE_FILECACHE_H
10#define CPPCOMMON_CACHE_FILECACHE_H
11
13#include "filesystem/file.h"
14#include "filesystem/path.h"
15#include "time/timespan.h"
16#include "time/timestamp.h"
17
18#include <functional>
19#include <map>
20#include <mutex>
21#include <shared_mutex>
22#include <string>
23#include <string_view>
24#include <tuple>
25#include <unordered_map>
26
27namespace CppCommon {
28
30
36{
37public:
39 typedef std::function<bool (FileCache& cache, const std::string& key, const std::string& value, const Timespan& timeout)> InsertHandler;
40
41 FileCache() = default;
42 FileCache(const FileCache&) = delete;
43 FileCache(FileCache&&) = delete;
44 ~FileCache() = default;
45
46 FileCache& operator=(const FileCache&) = delete;
48
50 explicit operator bool() const { return !empty(); }
51
53 bool empty() const;
54
56 size_t size() const;
57
59
65 bool emplace(std::string&& key, std::string&& value, const Timespan& timeout = Timespan(0));
66
68
74 bool insert(const std::string& key, const std::string& value, const Timespan& timeout = Timespan(0));
75
77
81 std::pair<bool, std::string_view> find(const std::string& key);
83
88 std::pair<bool, std::string_view> find(const std::string& key, Timestamp& timeout);
89
91
95 bool remove(const std::string& key);
96
98
105 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); });
106
108
112 bool find_path(const CppCommon::Path& path);
114
119 bool find_path(const CppCommon::Path& path, Timestamp& timeout);
120
122
126 bool remove_path(const CppCommon::Path& path);
127
129 void clear();
130
132 void watchdog(const UtcTimestamp& utc = UtcTimestamp());
133
135 void swap(FileCache& cache) noexcept;
136 friend void swap(FileCache& cache1, FileCache& cache2) noexcept;
137
138private:
139 mutable std::shared_mutex _lock;
140 Timestamp _timestamp;
141
142 struct MemCacheEntry
143 {
144 std::string value;
145 Timestamp timestamp;
146 Timespan timespan;
147
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) {}
151 };
152
153 struct FileCacheEntry
154 {
155 std::string prefix;
156 InsertHandler handler;
157 Timestamp timestamp;
158 Timespan timespan;
159
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) {}
162 };
163
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;
168
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);
171 bool remove_path_internal(const CppCommon::Path& path);
172};
173
176} // namespace CppCommon
177
178#include "filecache.inl"
179
180#endif // CPPCOMMON_CACHE_FILECACHE_H
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.
Definition filecache.cpp:34
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.
Definition filecache.cpp:55
friend void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition filecache.inl:23
bool remove(const std::string &key)
Remove the cache value with the given key from the file cache.
Definition filecache.cpp:80
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.
Definition filecache.cpp:13
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.
Definition filecache.inl:17
FileCache & operator=(FileCache &&)=delete
void clear()
Clear the memory cache.
FileCache & operator=(const FileCache &)=delete
std::function< bool(FileCache &cache, const std::string &key, const std::string &value, const Timespan &timeout)> InsertHandler
File cache insert handler type.
Definition filecache.h:39
bool empty() const
Is the file cache empty?
Definition filecache.inl:11
FileCache(FileCache &&)=delete
Filesystem path.
Definition path.h:90
Filesystem directory definition.
Filesystem file definition.
File cache inline implementation.
C++ Common project definitions.
Filesystem path definition.
Timespan definition.
Timestamp definition.