[Cache] Added mutex

[ci skip]
This commit is contained in:
/dev/root 2016-11-21 11:09:42 +01:00
parent c969f5054d
commit 98d9ad402f
2 changed files with 16 additions and 7 deletions

View File

@ -10,6 +10,7 @@ namespace Utils
*/ */
}; };
std::string Cache::validUrl; std::string Cache::validUrl;
std::mutex Cache::CacheMutex;
Cache::Cache(std::string path) Cache::Cache(std::string path)
{ {
@ -31,6 +32,9 @@ namespace Utils
std::string Cache::GetFile(int timeout, std::string useragent) std::string Cache::GetFile(int timeout, std::string useragent)
{ {
if (Cache::validUrl.empty())
{
std::lock_guard<std::mutex> _(Cache::CacheMutex);
for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++) for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++)
{ {
std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get(); std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get();
@ -40,6 +44,10 @@ namespace Utils
return result; return result;
} }
} }
return ""; }
else
{
return Utils::WebIO(useragent, this->GetUrl(Cache::validUrl, this->Path)).setTimeout(timeout)->get();
}
} }
} }

View File

@ -7,7 +7,8 @@ namespace Utils
std::string GetUrl(); std::string GetUrl();
std::string GetFile(int timeout = 5000, std::string useragent = "IW4x"); std::string GetFile(int timeout = 5000, std::string useragent = "IW4x");
//private: private:
static std::mutex CacheMutex;
static const char* urls[]; static const char* urls[];
static std::string validUrl; static std::string validUrl;
std::string Path; std::string Path;