Enhancements

Add mutex lock to prevent reading and writing a cached file at the same time.
Optimize codes.
This commit is contained in:
Tindy X 2020-03-01 01:58:59 +08:00
parent 6c2192b4f8
commit 1b6e877724
No known key found for this signature in database
GPG Key ID: C6AD413169968D58
3 changed files with 10 additions and 2 deletions

View File

@ -711,10 +711,11 @@ std::string fileGet(const std::string &path, bool scope_limit)
std::fseek(fp, 0, SEEK_END);
long tot = std::ftell(fp);
char *data = new char[tot + 1];
data[tot] = '\0';
std::rewind(fp);
std::fread(&data[0], 1, tot, fp);
std::fclose(fp);
content.append(data, 0, tot);
content.assign(data, tot);
delete[] data;
}

View File

@ -47,7 +47,7 @@ std::string getFormData(const std::string &raw_data);
void sleep(int interval);
bool regValid(const std::string &target);
bool regFind(const std::string &src, const std::string &target);
bool regFind(const std::string &src, const std::string &match);
std::string regReplace(const std::string &src, const std::string &match, const std::string &rep);
bool regMatch(const std::string &src, const std::string &match);
std::string speedCalc(double speed);

View File

@ -1,6 +1,7 @@
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
#include <mutex>
#include <curl/curl.h>
@ -17,6 +18,9 @@
extern bool print_debug_info;
typedef std::lock_guard<std::mutex> guarded_mutex;
std::mutex cache_rw_lock;
//std::string user_agent_str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
std::string user_agent_str = "subconverter/" + std::string(VERSION) + " cURL/" + std::string(LIBCURL_VERSION);
@ -124,6 +128,7 @@ std::string webGet(std::string url, std::string proxy, std::string &response_hea
if(difftime(now, mtime) <= cache_ttl) // within TTL
{
writeLog(0, "CACHE HIT: '" + url + "', using local cache.");
guarded_mutex guard(cache_rw_lock);
response_headers = fileGet(path_header, true);
return fileGet(path, true);
}
@ -134,6 +139,7 @@ std::string webGet(std::string url, std::string proxy, std::string &response_hea
content = curlGet(url, proxy, response_headers, return_code); // try to fetch data
if(return_code == CURLE_OK) // success, save new cache
{
guarded_mutex guard(cache_rw_lock);
fileWrite(path, content, true);
fileWrite(path_header, response_headers, true);
}
@ -142,6 +148,7 @@ std::string webGet(std::string url, std::string proxy, std::string &response_hea
if(fileExist(path)) // failed, check if cache exist
{
writeLog(0, "Fetch failed. Serving cached content."); // cache exist, serving cache
guarded_mutex guard(cache_rw_lock);
content = fileGet(path, true);
response_headers = fileGet(path_header, true);
}