forked from fuzziqersoftware/phosg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileCache.hh
54 lines (40 loc) · 1.15 KB
/
FileCache.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <mutex>
#include <string>
#include "Filesystem.hh"
#include "LRUSet.hh"
using namespace std;
class FileCache {
public:
struct File {
std::string name;
scoped_fd fd;
File() = delete;
File(const std::string& name, scoped_fd&& fd);
File(const File&) = delete;
File(File&&) = delete;
File& operator=(const File&) = delete;
File& operator=(File&&) = delete;
~File() = default;
};
FileCache() = delete;
explicit FileCache(size_t max_size);
FileCache(const FileCache&) = delete;
FileCache(FileCache&&) = delete;
FileCache& operator=(const FileCache&) = delete;
FileCache& operator=(FileCache&&) = delete;
~FileCache() = default;
size_t get_max_size() const;
size_t set_max_size(size_t max_size);
bool is_open(const std::string& name) const;
size_t size() const;
shared_ptr<const File> lease(const std::string& name, mode_t create_mode = 0644);
void close(const std::string& name);
void clear();
private:
size_t max_size;
LRUSet<std::string> lru;
std::unordered_map<std::string, std::shared_ptr<File>> name_to_file;
mutable std::mutex lock;
void enforce_max_size();
};