FileManager v1.7.0
Manage access to files with async model
Loading...
Searching...
No Matches
Cache.h
1#pragma once
2
3#include <unordered_map>
4#include <map>
5#include <mutex>
6#include <atomic>
7#include <vector>
8
9#include "Utility.h"
10
11namespace file_manager
12{
13 class FileManager;
14
18 class FILE_MANAGER_API Cache
19 {
20 public:
23 {
24 noError,
25 fileDoesNotExist,
26 notEnoughCacheSize
27 };
28
29 private:
30 std::unordered_map<std::filesystem::path, std::string, utility::pathHash> cacheData;
31 uint64_t cacheSize;
32 std::atomic<uint64_t> currentCacheSize;
33 mutable std::mutex cacheDataMutex;
34
35 private:
36 void updateCache();
37
38 static Cache& getCache();
39
40 private:
41 Cache();
42
43 ~Cache() = default;
44
45 public:
49 CacheResultCodes addCache(const std::filesystem::path& filePath, std::ios_base::openmode mode);
50
57 CacheResultCodes appendCache(const std::filesystem::path& filePath, const std::vector<char>& data);
58
65 CacheResultCodes appendCache(const std::filesystem::path& filePath, std::string_view data);
66
70 bool contains(const std::filesystem::path& filePath) const;
71
73 void clear();
74
77 void clear(const std::filesystem::path& filePath);
78
81 void setCacheSize(uint64_t sizeInBytes);
82
86 const std::string& getCacheData(const std::filesystem::path& filePath) const;
87
90 uint64_t getCacheSize() const;
91
94 uint64_t getCurrentCacheSize() const;
95
101 const std::string& operator [] (const std::filesystem::path& filePath) const;
102
103 friend void _utility::addCache(std::filesystem::path&& filePath, std::string&& data);
104
105 template<template<typename> typename OperationT> requires _utility::Operation<OperationT<uint64_t>>
106 friend void _utility::changeCurrentCacheSize(uint64_t amount);
107
108 friend class FileManager;
109 };
110
111 namespace _utility
112 {
113 template<template<typename> typename OperationT> requires _utility::Operation<OperationT<uint64_t>>
114 void changeCurrentCacheSize(uint64_t amount)
115 {
116 Cache& cache = Cache::getCache();
117
118 cache.currentCacheSize = OperationT<uint64_t>()(cache.currentCacheSize, amount);
119 }
120 }
121}
Files cache.
Definition Cache.h:19
void clear(const std::filesystem::path &filePath)
Clear cache of specific file.
CacheResultCodes
Result of adding cache.
Definition Cache.h:23
CacheResultCodes appendCache(const std::filesystem::path &filePath, std::string_view data)
Append specific cache.
CacheResultCodes appendCache(const std::filesystem::path &filePath, const std::vector< char > &data)
Append specific cache.
Provides files accessing from multiple threads. Singleton.
Definition FileManager.h:25