From 4ac5544a54f946b7a7b8439a957ecc46c08609c5 Mon Sep 17 00:00:00 2001 From: antao Date: Mon, 15 Oct 2018 15:58:11 +0800 Subject: [PATCH] Optimize files caching --- drogon_ctl/templates/config.csp | 2 +- lib/src/HttpAppFrameworkImpl.cc | 83 ++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/drogon_ctl/templates/config.csp b/drogon_ctl/templates/config.csp index af61e473..3e0585dc 100644 --- a/drogon_ctl/templates/config.csp +++ b/drogon_ctl/templates/config.csp @@ -87,7 +87,7 @@ "use_sendfile": true, //use_gzip:true by default,use gzip to compress the response body's content; "use_gzip": true, - //static_files_cache_time:5 by default,the time in which static file response is cached, + //static_files_cache_time:5 (seconds) by default,the time in which static file response is cached, //0 means cache forever,the negative value means no cache "static_files_cache_time":5 } diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 5a32b695..ae43bede 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -710,39 +710,6 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std:: LOG_INFO << "file query!"; std::string filePath = _rootPath + path; std::shared_ptr resp = std::make_shared(); - - //check last modified time,rfc2616-14.25 - //If-Modified-Since: Mon, 15 Oct 2018 06:26:33 GMT - - if (_enableLastModify) - { - struct stat fileStat; - LOG_TRACE << "enabled LastModify"; - if (stat(filePath.c_str(), &fileStat) >= 0) - { - LOG_TRACE << "last modify time:" << fileStat.st_mtime; - struct tm tm1; - gmtime_r(&fileStat.st_mtime, &tm1); - std::string timeStr; - timeStr.resize(64); - auto len=strftime((char *)timeStr.data(), timeStr.size() , "%a, %d %b %Y %T GMT", &tm1); - timeStr.resize(len); - std::string modiStr = req->getHeader("If-Modified-Since"); - if (modiStr == timeStr && !modiStr.empty()) - { - LOG_TRACE << "not Modified!"; - resp->setStatusCode(HttpResponse::k304NotModified); - if (needSetJsessionid) - { - resp->addCookie("JSESSIONID", session_id); - } - callback(resp); - return; - } - resp->addHeader("Last-Modified", timeStr); - resp->addHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT"); - } - } //find cached response HttpResponsePtr cachedResp; { @@ -756,6 +723,56 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std:: } } } + + //check last modified time,rfc2616-14.25 + //If-Modified-Since: Mon, 15 Oct 2018 06:26:33 GMT + + if (_enableLastModify) + { + if (cachedResp) + { + if (cachedResp->getHeader("Last-Modified") == req->getHeader("If-Modified-Since")) + { + resp->setStatusCode(HttpResponse::k304NotModified); + if (needSetJsessionid) + { + resp->addCookie("JSESSIONID", session_id); + } + callback(resp); + return; + } + } + else + { + struct stat fileStat; + LOG_TRACE << "enabled LastModify"; + if (stat(filePath.c_str(), &fileStat) >= 0) + { + LOG_TRACE << "last modify time:" << fileStat.st_mtime; + struct tm tm1; + gmtime_r(&fileStat.st_mtime, &tm1); + std::string timeStr; + timeStr.resize(64); + auto len = strftime((char *)timeStr.data(), timeStr.size(), "%a, %d %b %Y %T GMT", &tm1); + timeStr.resize(len); + std::string modiStr = req->getHeader("If-Modified-Since"); + if (modiStr == timeStr && !modiStr.empty()) + { + LOG_TRACE << "not Modified!"; + resp->setStatusCode(HttpResponse::k304NotModified); + if (needSetJsessionid) + { + resp->addCookie("JSESSIONID", session_id); + } + callback(resp); + return; + } + resp->addHeader("Last-Modified", timeStr); + resp->addHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT"); + } + } + } + if (cachedResp) { if (needSetJsessionid)