diff --git a/src/Utils/Cache.cpp b/src/Utils/Cache.cpp index e19b4d88..eadea30a 100644 --- a/src/Utils/Cache.cpp +++ b/src/Utils/Cache.cpp @@ -10,6 +10,7 @@ namespace Utils */ }; std::string Cache::validUrl; + std::mutex Cache::CacheMutex; Cache::Cache(std::string path) { @@ -31,15 +32,22 @@ namespace Utils std::string Cache::GetFile(int timeout, std::string useragent) { - for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++) + if (Cache::validUrl.empty()) { - std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get(); - if (!result.empty()) + std::lock_guard _(Cache::CacheMutex); + for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++) { - Cache::validUrl = Cache::urls[i]; - return result; + std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get(); + if (!result.empty()) + { + Cache::validUrl = Cache::urls[i]; + return result; + } } } - return ""; + else + { + return Utils::WebIO(useragent, this->GetUrl(Cache::validUrl, this->Path)).setTimeout(timeout)->get(); + } } } diff --git a/src/Utils/Cache.hpp b/src/Utils/Cache.hpp index d546a112..bc2e6090 100644 --- a/src/Utils/Cache.hpp +++ b/src/Utils/Cache.hpp @@ -7,7 +7,8 @@ namespace Utils std::string GetUrl(); std::string GetFile(int timeout = 5000, std::string useragent = "IW4x"); - //private: + private: + static std::mutex CacheMutex; static const char* urls[]; static std::string validUrl; std::string Path;