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
62
63
64
65
66
67
|
#pragma once
#include <string>
#include <vector>
#include <chrono>
#include <functional>
/** @brief cache storage manager */
class CacheManager {
private:
/** @brief currently opened cache location */
std::string cache_path;
/** @brief pointers to open file handles */
std::vector<std::fstream*> files;
/** @brief max amount of attempts for `retry` */
const unsigned int max_tries = 100;
/**
* @brief retry `action` as long as `retry_if` returns false, until `max_tries`
*
* this function runs `action` repeatedly until `retry_if` returns true, or
* `action` has been called `max_tries` times. between each try, there is a
* blocking 1ms delay, so this function should ideally only be used in
* threads, or when `action` has a high probability of succeeding.
*
* @param label this label gets printed if retry_count exceeds max_tries
* @param check_pre `true` = check `retry_if` before `action`, `false` = check `retry_if` after `action`
* @param action this gets executed as long as `retry_if` returns false
* @param retry_if this returns whether `action` is successful (boolean)
*/
virtual void retry(std::string label, bool check_pre, std::function<void()> action, std::function<bool()> retry_if);
/** @brief alias for `retry` with `check_pre` = false (check after action) */
virtual void retry_while(std::string label, std::function<void()> action, std::function<bool()> retry_if) { retry(label, false, action, retry_if); };
/** @brief alias for `retry` with `check_pre` = true (check before action) */
virtual void retry_if(std::string label, std::function<void()> action, std::function<bool()> retry_if) { retry(label, true, action, retry_if); };
public:
/** @brief initialize CacheManager class at `cache_path` */
CacheManager(const char* cache_path);
CacheManager(std::string cache_path);
/** @brief close cache */
virtual ~CacheManager();
/** @brief create cache folder structure */
virtual void init_cache();
/** @brief update cache date */
virtual void update_cache();
/**
* @brief check cache file structure
*
* automatically creates cache when non-existant
*/
virtual void verify_cache();
/** @brief get fstream for file in cache or create file */
virtual std::fstream* cache_get(const char* filename);
virtual std::fstream* cache_get(std::string filename) { return cache_get(filename.c_str()); };
/** @brief check if file exists in cache */
virtual bool cache_exists(const char* filename);
virtual bool cache_exists(std::string filename) { return cache_exists(filename.c_str()); };
/** @brief add cache path before filename */
virtual std::string prefix_cache_path(const char* filename);
/** @brief currently opened cache update unix timestamp */
uint64_t age;
};
|