-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcached_file.h
61 lines (47 loc) · 1.29 KB
/
cached_file.h
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
55
56
57
58
59
60
61
#ifndef CACHED_FILE_H_
#define CACHED_FILE_H_
#include <functional>
#include <mutex>
#include <string>
#include <unordered_map>
#include "disallow.h"
#include "scoped_fd.h"
class Cache {
public:
// Holds mmap result and frees it if necessary.
class Memory {
public:
Memory(void* m, size_t s);
// Move constructor.
Memory(Memory&& m);
// Move assignment.
Memory& operator=(Memory&& m);
~Memory();
const char* memory_charp() const;
size_t size() const;
// For debugging.
std::string get_copy() const;
private:
void* memory_;
size_t size_;
DISALLOW_COPY_AND_ASSIGN(Memory);
};
explicit Cache(const std::string& cache_dir);
~Cache();
// Get sha1 hash, and use fetch method to fetch if not available already.
const Memory* get(const std::string& name,
std::function<bool(std::string*)> fetch);
bool release(const std::string& name, const Memory* item);
// Garbage collect old cache items.
bool Gc();
void dump() const;
private:
void GetFileName(const std::string& key, std::string*, std::string*) const;
std::unordered_map<std::string, Memory> mapped_files_{};
std::mutex mutex_{};
const std::string cache_dir_;
// for directory.
ScopedFd file_lock_;
DISALLOW_COPY_AND_ASSIGN(Cache);
};
#endif