From ba9770ce95e7990f79ba30dde77f52fe2514626e Mon Sep 17 00:00:00 2001 From: Lemongrass3110 Date: Tue, 20 Sep 2022 04:22:09 +0200 Subject: [PATCH] Fixed caching of databases during runtime (#7275) * Fixes #7268 Thanks to @jmsngls --- src/common/database.hpp | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/common/database.hpp b/src/common/database.hpp index 6ef08d83d7..c79414ff34 100644 --- a/src/common/database.hpp +++ b/src/common/database.hpp @@ -111,7 +111,7 @@ public: } } - void put( keytype key, std::shared_ptr ptr ){ + virtual void put( keytype key, std::shared_ptr ptr ){ this->data[key] = ptr; } @@ -135,7 +135,7 @@ public: return rathena::util::umap_random( this->data ); } - void erase(keytype key) { + virtual void erase(keytype key) { this->data.erase(key); } }; @@ -143,20 +143,22 @@ public: template class TypesafeCachedYamlDatabase : public TypesafeYamlDatabase{ private: std::vector> cache; + bool loaded; public: TypesafeCachedYamlDatabase( const std::string& type_, uint16 version_, uint16 minimumVersion_ ) : TypesafeYamlDatabase( type_, version_, minimumVersion_ ){ - + this->loaded = false; } TypesafeCachedYamlDatabase( const std::string& type_, uint16 version_ ) : TypesafeYamlDatabase( type_, version_, version_ ){ - + this->loaded = false; } void clear() override{ TypesafeYamlDatabase::clear(); cache.clear(); cache.shrink_to_fit(); + this->loaded = false; } std::shared_ptr find( keytype key ) override{ @@ -206,6 +208,26 @@ public: break; } } + + this->loaded = true; + } + + void erase( keytype key ) override{ + TypesafeYamlDatabase::erase( key ); + + // Prevent excessive usage during loading + if( this->loaded ){ + this->cache[this->calculateCacheKey( key )] = nullptr; + } + } + + void put( keytype key, std::shared_ptr ptr ) override{ + TypesafeYamlDatabase::put( key, ptr ); + + // Prevent excessive usage during loading + if( this->loaded ){ + this->cache[this->calculateCacheKey( key )] = ptr; + } } };