[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,15 +32,22 @@ namespace Utils
std::string Cache::GetFile(int timeout, std::string useragent) 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(); std::lock_guard<std::mutex> _(Cache::CacheMutex);
if (!result.empty()) for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++)
{ {
Cache::validUrl = Cache::urls[i]; std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get();
return result; 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();
}
} }
} }

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;