mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-02-03 14:45:57 +01:00
Alos cleaned up FileCache.
This commit is contained in:
parent
ba1ea3c62a
commit
3c78c29c17
@ -1,8 +1,7 @@
|
||||
#include "file_cache.h"
|
||||
|
||||
#include "core/os/directory.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "core/os/dir_access.h"
|
||||
#include "core/os/file_access.h"
|
||||
|
||||
void FileCache::wwwroot_register_file(const String &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) {
|
||||
return registered_files.find(file_path) != registered_files.end();
|
||||
return registered_files.has(file_path);
|
||||
}
|
||||
|
||||
void FileCache::wwwroot_refresh_cache() {
|
||||
@ -23,30 +22,33 @@ void FileCache::wwwroot_refresh_cache() {
|
||||
|
||||
wwwroot.path_clean_end_slash();
|
||||
|
||||
wwwroot_evaluate_dir(wwwroot.c_str());
|
||||
wwwroot_evaluate_dir(wwwroot);
|
||||
|
||||
_lock.write_unlock();
|
||||
}
|
||||
|
||||
void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) {
|
||||
Ref<Directory> dir;
|
||||
dir.instance();
|
||||
void FileCache::wwwroot_evaluate_dir(const String &path, const bool should_exist) {
|
||||
DirAccess *da = DirAccess::open(path);
|
||||
|
||||
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()) {
|
||||
if (dir->current_is_file()) {
|
||||
String np = dir->current_get_path_cstr();
|
||||
da->list_dir_begin();
|
||||
String f = da->get_next();
|
||||
|
||||
while (f != String()) {
|
||||
if (!da->current_is_dir()) {
|
||||
String np = path + "/" + f;
|
||||
np = np.substr(wwwroot.size(), np.size() - wwwroot.size());
|
||||
|
||||
registered_files.insert(np);
|
||||
} 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) {
|
||||
@ -68,7 +70,7 @@ bool FileCache::get_cached_body(const String &path, String *body) {
|
||||
return false;
|
||||
}
|
||||
|
||||
body->append_str(e->body);
|
||||
body->operator+=(e->body);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -96,40 +98,23 @@ void FileCache::clear() {
|
||||
|
||||
registered_files.clear();
|
||||
|
||||
for (std::map<String, CacheEntry *>::iterator E = cache_map.begin(); E != cache_map.end(); E++) {
|
||||
CacheEntry * ce = E->second;
|
||||
for (Map<String, CacheEntry *>::Element *E = cache_map.front(); E; E++) {
|
||||
CacheEntry *ce = E->get();
|
||||
|
||||
if (ce) {
|
||||
delete ce;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cache_map.clear();
|
||||
|
||||
_lock.write_unlock();
|
||||
}
|
||||
|
||||
FileCache::FileCache(bool singleton) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
FileCache::FileCache() {
|
||||
cache_invalidation_time = 1;
|
||||
}
|
||||
|
||||
FileCache::~FileCache() {
|
||||
registered_files.clear();
|
||||
|
||||
if (_instance == this)
|
||||
_instance = nullptr;
|
||||
}
|
||||
|
||||
FileCache *FileCache::get_singleton() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
FileCache *FileCache::_instance = nullptr;
|
||||
|
@ -1,59 +1,51 @@
|
||||
#ifndef 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 <map>
|
||||
#include <chrono>
|
||||
#include "core/threading/rw_lock.h"
|
||||
#include "core/reference.h"
|
||||
|
||||
#include "core/object.h"
|
||||
|
||||
|
||||
class FileCache : public Object {
|
||||
RCPP_OBJECT(FileCache, Object);
|
||||
class FileCache : public Reference {
|
||||
GDCLASS(FileCache, Reference);
|
||||
|
||||
public:
|
||||
String wwwroot;
|
||||
int cache_invalidation_time;
|
||||
String wwwroot;
|
||||
int cache_invalidation_time;
|
||||
|
||||
//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
|
||||
void wwwroot_register_file(const String &file_path);
|
||||
void wwwroot_deregister_file(const String &file_path);
|
||||
bool wwwroot_has_file(const String &file_path);
|
||||
void wwwroot_refresh_cache();
|
||||
void wwwroot_evaluate_dir(const char *path, const bool should_exist = true);
|
||||
//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
|
||||
void wwwroot_register_file(const String &file_path);
|
||||
void wwwroot_deregister_file(const String &file_path);
|
||||
bool wwwroot_has_file(const String &file_path);
|
||||
void wwwroot_refresh_cache();
|
||||
void wwwroot_evaluate_dir(const String &path, const bool should_exist = true);
|
||||
|
||||
bool get_cached_body(const String &path, String *body);
|
||||
void set_cached_body(const String &path, const String &body);
|
||||
bool get_cached_body(const String &path, String *body);
|
||||
void set_cached_body(const String &path, const String &body);
|
||||
|
||||
void clear();
|
||||
void clear();
|
||||
|
||||
FileCache(bool singleton = false);
|
||||
virtual ~FileCache();
|
||||
FileCache();
|
||||
~FileCache();
|
||||
|
||||
static FileCache *get_singleton();
|
||||
|
||||
std::set<String> registered_files;
|
||||
Set<String> registered_files;
|
||||
|
||||
protected:
|
||||
struct CacheEntry {
|
||||
int64_t timestamp;
|
||||
String body;
|
||||
|
||||
struct CacheEntry {
|
||||
int64_t timestamp;
|
||||
String body;
|
||||
CacheEntry() {
|
||||
timestamp = 0;
|
||||
}
|
||||
};
|
||||
|
||||
CacheEntry() {
|
||||
timestamp = 0;
|
||||
}
|
||||
};
|
||||
|
||||
RWLock _lock;
|
||||
std::map<String, CacheEntry *> cache_map;
|
||||
|
||||
private:
|
||||
static FileCache *_instance;
|
||||
RWLock _lock;
|
||||
Map<String, CacheEntry *> cache_map;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user