mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-01-09 20:39:37 +01:00
Added bindings to FileCache, also smaller tweaks.
This commit is contained in:
parent
3c78c29c17
commit
663cd9d3a2
@ -3,6 +3,20 @@
|
||||
#include "core/os/dir_access.h"
|
||||
#include "core/os/file_access.h"
|
||||
|
||||
String FileCache::get_wwwroot() {
|
||||
return wwwroot;
|
||||
}
|
||||
void FileCache::set_wwwroot(const String &val) {
|
||||
wwwroot = val;
|
||||
}
|
||||
|
||||
int FileCache::get_cache_invalidation_time() {
|
||||
return cache_invalidation_time;
|
||||
}
|
||||
void FileCache::set_cache_invalidation_time(const int &val) {
|
||||
cache_invalidation_time = val;
|
||||
}
|
||||
|
||||
void FileCache::wwwroot_register_file(const String &file_path) {
|
||||
registered_files.insert(file_path);
|
||||
}
|
||||
@ -62,30 +76,77 @@ bool FileCache::get_cached_body(const String &path, String *body) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t current_timestamp = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
|
||||
int64_t diff = current_timestamp - e->timestamp;
|
||||
if (cache_invalidation_time > 0) {
|
||||
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
||||
uint64_t diff = current_timestamp - e->timestamp;
|
||||
|
||||
if (diff > cache_invalidation_time) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
body->operator+=(e->body);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileCache::has_cached_body(const String &path) {
|
||||
//TODO ERROR MACRO body == null
|
||||
|
||||
_lock.read_lock();
|
||||
CacheEntry *e = cache_map[path];
|
||||
_lock.read_unlock();
|
||||
|
||||
if (!e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cache_invalidation_time > 0) {
|
||||
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
||||
uint64_t diff = current_timestamp - e->timestamp;
|
||||
|
||||
if (diff > cache_invalidation_time) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
String FileCache::get_cached_body_bind(const String &path) {
|
||||
//TODO ERROR MACRO body == null
|
||||
|
||||
_lock.read_lock();
|
||||
CacheEntry *e = cache_map[path];
|
||||
_lock.read_unlock();
|
||||
|
||||
if (!e) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (cache_invalidation_time > 0) {
|
||||
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
||||
uint64_t diff = current_timestamp - e->timestamp;
|
||||
|
||||
if (diff > cache_invalidation_time) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
return e->body;
|
||||
}
|
||||
|
||||
void FileCache::set_cached_body(const String &path, const String &body) {
|
||||
_lock.write_lock();
|
||||
|
||||
CacheEntry *e = cache_map[path];
|
||||
|
||||
if (!e) {
|
||||
e = new CacheEntry();
|
||||
e = memnew(CacheEntry());
|
||||
cache_map[path] = e;
|
||||
}
|
||||
|
||||
int64_t current_timestamp = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
||||
|
||||
e->timestamp = current_timestamp;
|
||||
e->body = body;
|
||||
@ -102,7 +163,7 @@ void FileCache::clear() {
|
||||
CacheEntry *ce = E->get();
|
||||
|
||||
if (ce) {
|
||||
delete ce;
|
||||
memdelete(ce);
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,9 +173,31 @@ void FileCache::clear() {
|
||||
}
|
||||
|
||||
FileCache::FileCache() {
|
||||
cache_invalidation_time = 1;
|
||||
cache_invalidation_time = 0;
|
||||
}
|
||||
|
||||
FileCache::~FileCache() {
|
||||
registered_files.clear();
|
||||
}
|
||||
|
||||
void FileCache::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_wwwroot"), &FileCache::get_wwwroot);
|
||||
ClassDB::bind_method(D_METHOD("set_wwwroot", "val"), &FileCache::set_wwwroot);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "wwwroot"), "set_wwwroot", "get_wwwroot");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_cache_invalidation_time"), &FileCache::get_cache_invalidation_time);
|
||||
ClassDB::bind_method(D_METHOD("set_cache_invalidation_time", "val"), &FileCache::set_cache_invalidation_time);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "cache_invalidation_time"), "set_cache_invalidation_time", "get_cache_invalidation_time");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("wwwroot_register_file", "file_path"), &FileCache::wwwroot_register_file);
|
||||
ClassDB::bind_method(D_METHOD("wwwroot_deregister_file", "file_path"), &FileCache::wwwroot_deregister_file);
|
||||
ClassDB::bind_method(D_METHOD("wwwroot_has_file", "file_path"), &FileCache::wwwroot_has_file);
|
||||
ClassDB::bind_method(D_METHOD("wwwroot_refresh_cache"), &FileCache::wwwroot_refresh_cache);
|
||||
ClassDB::bind_method(D_METHOD("wwwroot_evaluate_dir", "file_path", "should_exist "), &FileCache::wwwroot_evaluate_dir, true);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_cached_body", "path"), &FileCache::get_cached_body_bind);
|
||||
ClassDB::bind_method(D_METHOD("has_cached_body", "path"), &FileCache::has_cached_body);
|
||||
ClassDB::bind_method(D_METHOD("set_cached_body", "path", "body"), &FileCache::set_cached_body);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("clear"), &FileCache::clear);
|
||||
}
|
||||
|
@ -13,8 +13,11 @@ class FileCache : public Reference {
|
||||
GDCLASS(FileCache, Reference);
|
||||
|
||||
public:
|
||||
String wwwroot;
|
||||
int cache_invalidation_time;
|
||||
String get_wwwroot();
|
||||
void set_wwwroot(const String &val);
|
||||
|
||||
int get_cache_invalidation_time();
|
||||
void set_cache_invalidation_time(const int &val);
|
||||
|
||||
//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
|
||||
@ -25,6 +28,8 @@ public:
|
||||
void wwwroot_evaluate_dir(const String &path, const bool should_exist = true);
|
||||
|
||||
bool get_cached_body(const String &path, String *body);
|
||||
bool has_cached_body(const String &path);
|
||||
String get_cached_body_bind(const String &path);
|
||||
void set_cached_body(const String &path, const String &body);
|
||||
|
||||
void clear();
|
||||
@ -32,11 +37,16 @@ public:
|
||||
FileCache();
|
||||
~FileCache();
|
||||
|
||||
String wwwroot;
|
||||
int cache_invalidation_time;
|
||||
|
||||
Set<String> registered_files;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
struct CacheEntry {
|
||||
int64_t timestamp;
|
||||
uint64_t timestamp;
|
||||
String body;
|
||||
|
||||
CacheEntry() {
|
||||
|
Loading…
Reference in New Issue
Block a user