2020-11-25 00:20:41 +01:00
|
|
|
#ifndef FILE_CACHE_H
|
|
|
|
#define FILE_CACHE_H
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
#include "core/string.h"
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
#include <set>
|
2020-11-25 14:53:53 +01:00
|
|
|
#include <map>
|
|
|
|
#include <chrono>
|
2022-02-04 22:12:08 +01:00
|
|
|
#include "core/threading/rw_lock.h"
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-02-05 16:52:24 +01:00
|
|
|
#include "core/object.h"
|
|
|
|
|
|
|
|
|
2022-02-05 17:22:34 +01:00
|
|
|
class FileCache : public Object {
|
2022-02-05 16:52:24 +01:00
|
|
|
RCPP_OBJECT(FileCache, Object);
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
public:
|
2021-11-01 19:53:35 +01:00
|
|
|
String wwwroot;
|
2020-11-25 14:53:53 +01:00
|
|
|
int cache_invalidation_time;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-11-25 13:22:43 +01:00
|
|
|
//Note: file path should be the url you want to access the file with, inculding lead slash
|
|
|
|
//e.g. http://127.0.0.1/a/b/d.jpg -> /a/b/d.jpg
|
2021-11-01 19:53:35 +01:00
|
|
|
void wwwroot_register_file(const String &file_path);
|
|
|
|
void wwwroot_deregister_file(const String &file_path);
|
|
|
|
bool wwwroot_has_file(const String &file_path);
|
2020-11-25 14:53:53 +01:00
|
|
|
void wwwroot_refresh_cache();
|
2021-01-06 03:57:18 +01:00
|
|
|
void wwwroot_evaluate_dir(const char *path, const bool should_exist = true);
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
bool get_cached_body(const String &path, String *body);
|
|
|
|
void set_cached_body(const String &path, const String &body);
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-02-04 22:03:22 +01:00
|
|
|
void clear();
|
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
FileCache(bool singleton = false);
|
2020-11-25 00:20:41 +01:00
|
|
|
virtual ~FileCache();
|
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
static FileCache *get_singleton();
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
std::set<String> registered_files;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
protected:
|
|
|
|
|
|
|
|
struct CacheEntry {
|
|
|
|
int64_t timestamp;
|
2021-11-01 19:53:35 +01:00
|
|
|
String body;
|
2020-11-25 14:53:53 +01:00
|
|
|
|
|
|
|
CacheEntry() {
|
|
|
|
timestamp = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-04 22:12:08 +01:00
|
|
|
RWLock _lock;
|
2021-11-01 19:53:35 +01:00
|
|
|
std::map<String, CacheEntry *> cache_map;
|
2020-11-25 14:53:53 +01:00
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
private:
|
|
|
|
static FileCache *_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|