-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheStorage.hpp
48 lines (37 loc) · 978 Bytes
/
CacheStorage.hpp
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
#ifndef ML_CACHESTORAGE_HPP
#define ML_CACHESTORAGE_HPP
#include <cstddef>
#include <memory>
#include <unordered_map>
#include <vector>
class CacheStorage {
public:
using Id = std::size_t;
struct CacheData {
Id id;
std::size_t size;
std::unique_ptr<std::uint8_t[]> data;
};
private:
using TimeCount = unsigned long;
struct CacheInfo {
Id id;
std::size_t size;
TimeCount lastUsed;
};
TimeCount mTimeCount;
Id mLastId;
std::size_t mTotalSize;
std::size_t mMaxStorageSize;
std::size_t mMaxStorageData;
std::unordered_map<Id, CacheData> mCacheDataMap;
std::vector<CacheInfo> mCacheInfoHeap;
static bool CompareCacheInfo(const CacheInfo& a, const CacheInfo& b);
public:
CacheStorage(std::size_t maxStorageSize, std::size_t maxStorageData);
Id Remove();
Id Add(std::unique_ptr<std::uint8_t[]>&& data, std::size_t size);
Id Add(const std::uint8_t* data, std::size_t size);
const CacheData* Get(Id id);
};
#endif