Simplify the response caching logic

This commit is contained in:
an-tao 2018-10-11 22:09:59 +08:00
parent 5317982625
commit 9322fcbc54
4 changed files with 47 additions and 34 deletions

View File

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

View File

@ -781,7 +781,6 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
auto newResp=std::make_shared<HttpResponseImpl>
(*std::dynamic_pointer_cast<HttpResponseImpl>(responsePtr));
newResp->addCookie("JSESSIONID",session_id);
newResp->setExpiredTime(-1);
callback(newResp);
}
return;
@ -798,7 +797,6 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
if(needSetJsessionid)
{
resp->removeCookie("JSESSIONID");
std::dynamic_pointer_cast<HttpResponseImpl>(resp)->makeHeaderString();
}
std::lock_guard<std::mutex> guard(_simpCtrlMap[pathLower]._mutex);
_responseCacheMap->insert(pathLower,resp,resp->expiredTime());
@ -852,7 +850,6 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
auto newResp=std::make_shared<HttpResponseImpl>
(*std::dynamic_pointer_cast<HttpResponseImpl>(responsePtr));
newResp->addCookie("JSESSIONID",session_id);
newResp->setExpiredTime(-1);
callback(newResp);
}
return;
@ -901,10 +898,8 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr& req,const std::f
if(resp->expiredTime()>=0)
{
//cache the response;
if(needSetJsessionid)
{
if(needSetJsessionid) {
resp->removeCookie("JSESSIONID");
std::dynamic_pointer_cast<HttpResponseImpl>(resp)->makeHeaderString();
}
std::lock_guard<std::mutex> guard(*(_apiCtrlVector[ctlIndex].binderMtx));
_responseCacheMap->insert(_apiCtrlVector[ctlIndex].pathParameterPattern,resp,resp->expiredTime());

View File

@ -358,22 +358,10 @@ void HttpResponseImpl::makeHeaderString(MsgBuffer* output) const
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());
}
void HttpResponseImpl::makeHeaderString() const
{
assert(_expriedTime>=0);
MsgBuffer buffer;
makeHeaderString(&buffer);
}
void HttpResponseImpl::appendToBuffer(MsgBuffer* output) const {
if(_fullHeaderString.empty())
{
@ -384,6 +372,13 @@ void HttpResponseImpl::appendToBuffer(MsgBuffer* output) const {
output->append(_fullHeaderString);
}
//output cookies
if(_cookies.size() > 0) {
for(auto it = _cookies.begin(); it != _cookies.end(); it++) {
output->append(it->second.cookieString());
}
}
//output Date header
output->append("Date: ");

View File

@ -186,19 +186,16 @@ namespace drogon
virtual void addCookie(const std::string& key, const std::string& value) override
{
_cookies.insert(std::make_pair(key,Cookie(key,value)));
_fullHeaderString.clear();
}
virtual void addCookie(const Cookie &cookie) override
{
_cookies.insert(std::make_pair(cookie.key(),cookie));
_fullHeaderString.clear();
}
virtual void removeCookie(const std::string& key) override
{
_cookies.erase(key);
_fullHeaderString.clear();
}
virtual void setBody(const std::string& body) override
@ -262,6 +259,7 @@ namespace drogon
std::swap(_current_chunk_length,that._current_chunk_length);
std::swap(_contentType,that._contentType);
_jsonPtr.swap(that._jsonPtr);
_fullHeaderString.swap(that._fullHeaderString);
}
void parseJson() const
{
@ -290,7 +288,6 @@ namespace drogon
void setSendfile(const std::string &filename){
_sendfileName=filename;
}
void makeHeaderString() const;
protected:
static std::string web_content_type_to_string(uint8_t contenttype);
static const std::string web_content_type_and_charset_to_string(uint8_t contenttype,