mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-02-03 22:55:55 +01:00
Alos cleaned up FileCache.
This commit is contained in:
parent
ba1ea3c62a
commit
3c78c29c17
@ -1,8 +1,7 @@
|
|||||||
#include "file_cache.h"
|
#include "file_cache.h"
|
||||||
|
|
||||||
#include "core/os/directory.h"
|
#include "core/os/dir_access.h"
|
||||||
|
#include "core/os/file_access.h"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
void FileCache::wwwroot_register_file(const String &file_path) {
|
void FileCache::wwwroot_register_file(const String &file_path) {
|
||||||
registered_files.insert(file_path);
|
registered_files.insert(file_path);
|
||||||
@ -13,7 +12,7 @@ void FileCache::wwwroot_deregister_file(const String &file_path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool FileCache::wwwroot_has_file(const String &file_path) {
|
bool FileCache::wwwroot_has_file(const String &file_path) {
|
||||||
return registered_files.find(file_path) != registered_files.end();
|
return registered_files.has(file_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileCache::wwwroot_refresh_cache() {
|
void FileCache::wwwroot_refresh_cache() {
|
||||||
@ -23,30 +22,33 @@ void FileCache::wwwroot_refresh_cache() {
|
|||||||
|
|
||||||
wwwroot.path_clean_end_slash();
|
wwwroot.path_clean_end_slash();
|
||||||
|
|
||||||
wwwroot_evaluate_dir(wwwroot.c_str());
|
wwwroot_evaluate_dir(wwwroot);
|
||||||
|
|
||||||
_lock.write_unlock();
|
_lock.write_unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) {
|
void FileCache::wwwroot_evaluate_dir(const String &path, const bool should_exist) {
|
||||||
Ref<Directory> dir;
|
DirAccess *da = DirAccess::open(path);
|
||||||
dir.instance();
|
|
||||||
|
|
||||||
ERR_FAIL_COND_MSG(dir->open_dir(path) != OK, "Error opening wwwroot! folder: " + String(path));
|
ERR_FAIL_COND_MSG(!da, "Error opening wwwroot! folder: " + path);
|
||||||
|
|
||||||
while (dir->next()) {
|
da->list_dir_begin();
|
||||||
if (dir->current_is_file()) {
|
String f = da->get_next();
|
||||||
String np = dir->current_get_path_cstr();
|
|
||||||
|
|
||||||
|
while (f != String()) {
|
||||||
|
if (!da->current_is_dir()) {
|
||||||
|
String np = path + "/" + f;
|
||||||
np = np.substr(wwwroot.size(), np.size() - wwwroot.size());
|
np = np.substr(wwwroot.size(), np.size() - wwwroot.size());
|
||||||
|
|
||||||
registered_files.insert(np);
|
registered_files.insert(np);
|
||||||
} else {
|
} else {
|
||||||
wwwroot_evaluate_dir(dir->current_get_path_cstr());
|
wwwroot_evaluate_dir(path + "/" + f);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dir->close_dir();
|
f = da->get_next();
|
||||||
|
}
|
||||||
|
da->list_dir_end();
|
||||||
|
|
||||||
|
memdelete(da);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileCache::get_cached_body(const String &path, String *body) {
|
bool FileCache::get_cached_body(const String &path, String *body) {
|
||||||
@ -68,7 +70,7 @@ bool FileCache::get_cached_body(const String &path, String *body) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
body->append_str(e->body);
|
body->operator+=(e->body);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -96,8 +98,8 @@ void FileCache::clear() {
|
|||||||
|
|
||||||
registered_files.clear();
|
registered_files.clear();
|
||||||
|
|
||||||
for (std::map<String, CacheEntry *>::iterator E = cache_map.begin(); E != cache_map.end(); E++) {
|
for (Map<String, CacheEntry *>::Element *E = cache_map.front(); E; E++) {
|
||||||
CacheEntry * ce = E->second;
|
CacheEntry *ce = E->get();
|
||||||
|
|
||||||
if (ce) {
|
if (ce) {
|
||||||
delete ce;
|
delete ce;
|
||||||
@ -109,27 +111,10 @@ void FileCache::clear() {
|
|||||||
_lock.write_unlock();
|
_lock.write_unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
FileCache::FileCache(bool singleton) {
|
FileCache::FileCache() {
|
||||||
if (singleton) {
|
|
||||||
if (_instance) {
|
|
||||||
printf("FileCache: Filecache instance is set as singleton, but an another FileCache instance is already set up as singleton! Ignoring setting!\n");
|
|
||||||
} else {
|
|
||||||
_instance = this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cache_invalidation_time = 1;
|
cache_invalidation_time = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileCache::~FileCache() {
|
FileCache::~FileCache() {
|
||||||
registered_files.clear();
|
registered_files.clear();
|
||||||
|
|
||||||
if (_instance == this)
|
|
||||||
_instance = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FileCache *FileCache::get_singleton() {
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
FileCache *FileCache::_instance = nullptr;
|
|
||||||
|
@ -1,18 +1,16 @@
|
|||||||
#ifndef FILE_CACHE_H
|
#ifndef FILE_CACHE_H
|
||||||
#define FILE_CACHE_H
|
#define FILE_CACHE_H
|
||||||
|
|
||||||
#include "core/string.h"
|
#include "core/map.h"
|
||||||
|
#include "core/os/os.h"
|
||||||
|
#include "core/os/rw_lock.h"
|
||||||
|
#include "core/set.h"
|
||||||
|
#include "core/ustring.h"
|
||||||
|
|
||||||
#include <set>
|
#include "core/reference.h"
|
||||||
#include <map>
|
|
||||||
#include <chrono>
|
|
||||||
#include "core/threading/rw_lock.h"
|
|
||||||
|
|
||||||
#include "core/object.h"
|
class FileCache : public Reference {
|
||||||
|
GDCLASS(FileCache, Reference);
|
||||||
|
|
||||||
class FileCache : public Object {
|
|
||||||
RCPP_OBJECT(FileCache, Object);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
String wwwroot;
|
String wwwroot;
|
||||||
@ -24,22 +22,19 @@ public:
|
|||||||
void wwwroot_deregister_file(const String &file_path);
|
void wwwroot_deregister_file(const String &file_path);
|
||||||
bool wwwroot_has_file(const String &file_path);
|
bool wwwroot_has_file(const String &file_path);
|
||||||
void wwwroot_refresh_cache();
|
void wwwroot_refresh_cache();
|
||||||
void wwwroot_evaluate_dir(const char *path, const bool should_exist = true);
|
void wwwroot_evaluate_dir(const String &path, const bool should_exist = true);
|
||||||
|
|
||||||
bool get_cached_body(const String &path, String *body);
|
bool get_cached_body(const String &path, String *body);
|
||||||
void set_cached_body(const String &path, const String &body);
|
void set_cached_body(const String &path, const String &body);
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
FileCache(bool singleton = false);
|
FileCache();
|
||||||
virtual ~FileCache();
|
~FileCache();
|
||||||
|
|
||||||
static FileCache *get_singleton();
|
Set<String> registered_files;
|
||||||
|
|
||||||
std::set<String> registered_files;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
struct CacheEntry {
|
struct CacheEntry {
|
||||||
int64_t timestamp;
|
int64_t timestamp;
|
||||||
String body;
|
String body;
|
||||||
@ -50,10 +45,7 @@ protected:
|
|||||||
};
|
};
|
||||||
|
|
||||||
RWLock _lock;
|
RWLock _lock;
|
||||||
std::map<String, CacheEntry *> cache_map;
|
Map<String, CacheEntry *> cache_map;
|
||||||
|
|
||||||
private:
|
|
||||||
static FileCache *_instance;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user