Add response cache

This commit is contained in:
antao 2018-10-11 11:20:40 +08:00
parent 4171f54c41
commit 16f7af90d3
10 changed files with 149 additions and 60 deletions

View File

@ -5,5 +5,6 @@ void TestController::asyncHandleHttpRequest(const HttpRequestPtr& req,const std:
//write your application logic here
auto resp=HttpResponse::newHttpResponse();
resp->setBody("<p>Hello, world!</p>");
resp->setExpiredTime(0);
callback(resp);
}

View File

@ -23,4 +23,11 @@ void ApiTest::your_method_name(const HttpRequestPtr& req,const std::function<voi
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
callback(res);
}
void ApiTest::staticApi(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)>&callback)
{
auto resp=HttpResponse::newHttpResponse();
resp->setBody("staticApi,hello!!");
resp->setExpiredTime(0);//cache the response forever;
callback(resp);
}

View File

@ -12,11 +12,13 @@ namespace api
//use METHOD_ADD to add your custom processing function here;
METHOD_ADD(ApiTest::get,"/get/{2}/{1}","drogon::GetFilter");//path will be /api/v1/apitest/get/{arg2}/{arg1}
METHOD_ADD(ApiTest::your_method_name,"/{1}/list?p2={2}","drogon::GetFilter");//path will be /api/v1/apitest/{arg1}/list
METHOD_ADD(ApiTest::staticApi,"/static");
METHOD_LIST_END
//your declaration of processing function maybe like this:
void get(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)>&callback,int p1,std::string &&p2);
void your_method_name(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)>&callback,double p1,int p2) const;
void staticApi(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)>&callback);
};
}
}

View File

@ -133,13 +133,14 @@ public:
//If timeout>0,the value will be erased
//within the 'timeout' seconds after the last access
void insert(const T1& key,T2&& value,size_t timeout=0,std::function<void()> timeoutCallback=std::function<void()>())
template <typename V>
void insert(const T1& key,V&& value,size_t timeout=0,std::function<void()> timeoutCallback=std::function<void()>())
{
if(timeout>0)
{
std::lock_guard<std::mutex> lock(mtx_);
_map[key].value=std::forward<T2>(value);
_map[key].value=std::forward<V>(value);
_map[key].timeout=timeout;
_map[key]._timeoutCallback=std::move(timeoutCallback);
@ -148,7 +149,7 @@ public:
else
{
std::lock_guard<std::mutex> lock(mtx_);
_map[key].value=std::forward<T2>(value);
_map[key].value=std::forward<V>(value);
_map[key].timeout=timeout;
_map[key]._timeoutCallback=std::function<void()>();
_map[key]._weakEntryPtr=WeakCallbackEntryPtr();

View File

@ -144,6 +144,8 @@ namespace drogon
virtual void addCookie(const Cookie &cookie)=0;
virtual void removeCookie(const std::string& key)=0;
virtual void setBody(const std::string& body)=0;
virtual void setBody(std::string&& body)=0;
@ -152,6 +154,9 @@ namespace drogon
virtual void clear()=0;
virtual void setExpiredTime(ssize_t expiredTime)=0;
virtual ssize_t expiredTime() const=0;
virtual const std::string & getBody() const=0;
virtual std::string & getBody() = 0;

View File

@ -106,7 +106,7 @@ void HttpAppFrameworkImpl::setFileTypes(const std::vector<std::string> &types)
}
void HttpAppFrameworkImpl::initRegex() {
std::string regString;
for(auto binder:_apiCtrlVector)
for(auto &binder:_apiCtrlVector)
{
std::regex reg("\\(\\[\\^/\\]\\*\\)");
std::string tmp=std::regex_replace(binder.pathParameterPattern,reg,"[^/]*");
@ -430,6 +430,8 @@ void HttpAppFrameworkImpl::run()
{
_sessionMapPtr=std::unique_ptr<CacheMap<std::string,SessionPtr>>(new CacheMap<std::string,SessionPtr>(&_loop));
}
_responseCacheMap=std::unique_ptr<CacheMap<std::string,HttpResponsePtr>>
(new CacheMap<std::string,HttpResponsePtr>(&_loop,1.0,4,50));//Max timeout up to about 70 days;
_loop.loop();
}
@ -658,7 +660,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
LOG_TRACE << "new request:"<<req->peerAddr().toIpPort()<<"->"<<req->localAddr().toIpPort();
LOG_TRACE << "Headers " << req->methodString() << " " << req->path();
#if 1
#if 0
const std::map<std::string, std::string>& headers = req->headers();
for (std::map<std::string, std::string>::const_iterator it = headers.begin();
it != headers.end();
@ -751,10 +753,12 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
doFilters(filters,req,callback,needSetJsessionid,session_id,[=](){
const std::string &ctrlName = _simpCtrlMap[pathLower].controllerName;
std::shared_ptr<HttpSimpleControllerBase> controller;
HttpResponsePtr responsePtr;
{
//maybe update controller,so we use lock_guard to protect;
std::lock_guard<std::mutex> guard(_simpCtrlMap[pathLower]._mutex);
controller=_simpCtrlMap[pathLower].controller;
responsePtr=_simpCtrlMap[pathLower].responsePtr.lock();
if(!controller)
{
auto _object = std::shared_ptr<DrObjectBase>(DrClassMap::newObject(ctrlName));
@ -765,11 +769,27 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
if(controller) {
controller->asyncHandleHttpRequest(req, [=](const HttpResponsePtr& resp){
if(needSetJsessionid)
resp->addCookie("JSESSIONID",session_id);
callback(resp);
});
if(responsePtr&&!needSetJsessionid)
{
callback(responsePtr);
}
else
{
controller->asyncHandleHttpRequest(req, [=](const HttpResponsePtr& resp){
if(needSetJsessionid)
resp->addCookie("JSESSIONID",session_id);
callback(resp);
if(resp->expiredTime()>=0&&!needSetJsessionid)
{
//cache the response;
std::lock_guard<std::mutex> guard(_simpCtrlMap[pathLower]._mutex);
_responseCacheMap->insert(pathLower,resp,resp->expiredTime());
_simpCtrlMap[pathLower].responsePtr=resp;
}
});
}
return;
} else {
@ -795,6 +815,23 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
LOG_TRACE<<"got api access,regex="<<binder.pathParameterPattern;
auto &filters=binder.filtersName;
doFilters(filters,req,callback,needSetJsessionid,session_id,[=](){
auto &binder=_apiCtrlVector[ctlIndex];
if(!needSetJsessionid)
{
HttpResponsePtr responsePtr;
{
std::lock_guard<std::mutex> guard(*(binder.binderMtx));
responsePtr=binder.responsePtr.lock();
}
if(responsePtr)
{
//use cached response!
LOG_TRACE<<"Use cached response";
callback(responsePtr);
return;
}
}
std::vector<std::string> params(binder.parameterPlaces.size());
std::smatch r;
if(std::regex_match(req->path(),r,std::regex(binder.pathParameterPattern)))
@ -835,6 +872,14 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
if(needSetJsessionid)
resp->addCookie("JSESSIONID",session_id);
callback(resp);
if(resp->expiredTime()>=0&&!needSetJsessionid)
{
//cache the response;
std::lock_guard<std::mutex> guard(*(_apiCtrlVector[ctlIndex].binderMtx));
_responseCacheMap->insert(_apiCtrlVector[ctlIndex].pathParameterPattern,resp,resp->expiredTime());
_apiCtrlVector[ctlIndex].responsePtr=resp;
}
});
return;
});

View File

@ -100,6 +100,8 @@ namespace drogon
typedef std::shared_ptr<Session> SessionPtr;
std::unique_ptr<CacheMap<std::string,SessionPtr>> _sessionMapPtr;
std::unique_ptr<CacheMap<std::string,HttpResponsePtr>> _responseCacheMap;
void doFilters(const std::vector<std::string> &filters,
const HttpRequestPtr& req,
const std::function<void (const HttpResponsePtr &)> & callback,
@ -118,6 +120,7 @@ namespace drogon
std::string controllerName;
std::vector<std::string> filtersName;
std::shared_ptr<HttpSimpleControllerBase> controller;
std::weak_ptr<HttpResponse> responsePtr;
std::mutex _mutex;
};
std::unordered_map<std::string,ControllerAndFiltersName>_simpCtrlMap;
@ -137,6 +140,8 @@ namespace drogon
std::map<std::string,size_t> queryParametersPlaces;
HttpApiBinderBasePtr binderPtr;
std::vector<std::string> filtersName;
std::unique_ptr <std::mutex> binderMtx=std::unique_ptr<std::mutex>(new std::mutex);
std::weak_ptr<HttpResponse> responsePtr;
};
//std::unordered_map<std::string,ApiBinder>_apiCtrlMap;
std::vector<ApiBinder>_apiCtrlVector;

View File

@ -312,65 +312,72 @@ std::string HttpResponseImpl::web_response_code_to_string(int code)
return "Undefined Error";
}
}
void HttpResponseImpl::appendToBuffer(MsgBuffer* output) const
{
char buf[64];
snprintf(buf, sizeof buf, "HTTP/1.1 %d ", _statusCode);
output->append(buf);
output->append(_statusMessage);
output->append("\r\n");
if(_sendfileName.empty())
void HttpResponseImpl::appendToBuffer(MsgBuffer* output) const {
if(_fullHeaderString.empty())
{
snprintf(buf, sizeof buf, "Content-Length: %lu\r\n", _body.size());
char buf[64];
snprintf(buf, sizeof buf, "HTTP/1.1 %d ", _statusCode);
output->append(buf);
output->append(_statusMessage);
output->append("\r\n");
if (_sendfileName.empty()) {
snprintf(buf, sizeof buf, "Content-Length: %lu\r\n", _body.size());
} else {
struct stat filestat;
if (stat(_sendfileName.c_str(), &filestat) < 0) {
LOG_SYSERR << _sendfileName << " stat error";
return;
}
#ifdef __linux__
snprintf(buf, sizeof buf, "Content-Length: %ld\r\n",filestat.st_size);
#else
snprintf(buf, sizeof buf, "Content-Length: %lld\r\n", filestat.st_size);
#endif
}
output->append(buf);
if (_headers.find("Connection") == _headers.end()) {
if (_closeConnection) {
output->append("Connection: close\r\n");
} else {
//output->append("Connection: Keep-Alive\r\n");
}
}
for (auto it = _headers.begin();
it != _headers.end();
++it) {
output->append(it->first);
output->append(": ");
output->append(it->second);
output->append("\r\n");
}
output->append("Server: drogon/");
output->append(drogon::getVersion());
output->append("\r\n");
if(_cookies.size() > 0) {
for(auto it = _cookies.begin(); it != _cookies.end(); it++) {
output->append(it->second.cookieString());
}
}
if(_expriedTime>=0)
_fullHeaderString=std::string(output->peek(),output->readableBytes());
}
else
{
struct stat filestat;
if(stat(_sendfileName.c_str(), &filestat)<0)
{
LOG_SYSERR<<_sendfileName<<" stat error";
return;
}
#ifdef __linux__
snprintf(buf, sizeof buf, "Content-Length: %ld\r\n",filestat.st_size);
#else
snprintf(buf, sizeof buf, "Content-Length: %lld\r\n",filestat.st_size);
#endif
}
output->append(buf);
if(_headers.find("Connection")==_headers.end())
{
if (_closeConnection) {
output->append("Connection: close\r\n");
} else {
//output->append("Connection: Keep-Alive\r\n");
}
output->append(_fullHeaderString);
}
for (auto it = _headers.begin();
it != _headers.end();
++it) {
output->append(it->first);
output->append(": ");
output->append(it->second);
output->append("\r\n");
}
//output Date header
output->append("Date: ");
output->append(getHttpFullDate(trantor::Date::date()));
output->append("\r\n");
if(_cookies.size() > 0) {
for(auto it = _cookies.begin(); it != _cookies.end(); it++) {
output->append(it->second.cookieString());
}
}
output->append("Server: drogon/");
output->append(drogon::getVersion());
output->append("\r\n\r\n");
LOG_TRACE<<"reponse(no body):"<<output->peek();

View File

@ -193,6 +193,11 @@ namespace drogon
_cookies.insert(std::make_pair(cookie.key(),cookie));
}
virtual void removeCookie(const std::string& key) override
{
_cookies.erase(key);
}
virtual void setBody(const std::string& body) override
{
_body = body;
@ -220,6 +225,13 @@ namespace drogon
_current_chunk_length = 0;
}
virtual void setExpiredTime(ssize_t expiredTime) override
{
_expriedTime=expiredTime;
}
virtual ssize_t expiredTime() const override {return _expriedTime;}
// void setReceiveTime(trantor::Date t)
// {
// receiveTime_ = t;
@ -294,8 +306,11 @@ namespace drogon
size_t _current_chunk_length;
uint8_t _contentType=CT_TEXT_HTML;
ssize_t _expriedTime=-1;
std::string _sendfileName;
mutable std::shared_ptr<Json::Value> _jsonPtr;
mutable std::string _fullHeaderString;
//trantor::Date receiveTime_;
void setContentType(const std::string& contentType)

View File

@ -182,7 +182,8 @@ void HttpServer::onRequest(const TcpConnectionPtr& conn, const HttpRequestPtr& r
if(sendfileName.empty()&&
response->getContentTypeCode()<CT_APPLICATION_OCTET_STREAM&&
response->getBody().length()>1024&&
req->getHeader("Accept-Encoding").find("gzip")!=std::string::npos)
req->getHeader("Accept-Encoding").find("gzip")!=std::string::npos&&
response->getHeader("Content-Encoding")!="")
{
//use gzip
LOG_TRACE<<"Use gzip to compress the body";