Skip to content

Commit 8f69ae9

Browse files
author
Sebastiano Merlino
committed
Solved a bug that implied cache impossibility to update
1 parent e1a8af8 commit 8f69ae9

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/webserver.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,27 +1623,47 @@ cache_entry* webserver::put_in_cache(const std::string& key, http_response* valu
16231623
pthread_rwlock_wrlock(&cache_guard);
16241624
map<string, cache_entry>::iterator it(cache_m->response_cache.find(key));
16251625
cache_entry* to_ret;
1626-
bool unlock = false;
1626+
bool already_in = false;
16271627
if(it != cache_m->response_cache.end())
16281628
{
16291629
(*it).second.lock(true);
1630-
unlock = true;
1630+
already_in = true;
16311631
}
16321632
if(validity == -1)
16331633
{
1634-
pair<map<string, cache_entry>::iterator, bool> res = cache_m->response_cache.insert(pair<string, cache_entry>(key, cache_entry(value)));
1635-
to_ret = &((*res.first).second);
1636-
*new_elem = res.second;
1634+
if(already_in)
1635+
{
1636+
(*it).second.response = value;
1637+
to_ret = &(*it).second;
1638+
*new_elem = false;
1639+
}
1640+
else
1641+
{
1642+
pair<map<string, cache_entry>::iterator, bool> res = cache_m->response_cache.insert(pair<string, cache_entry>(key, cache_entry(value)));
1643+
to_ret = &((*res.first).second);
1644+
*new_elem = res.second;
1645+
}
16371646
}
16381647
else
16391648
{
16401649
timeval now;
16411650
gettimeofday(&now, NULL);
1642-
pair<map<string, cache_entry>::iterator, bool> res = cache_m->response_cache.insert(pair<string, cache_entry>(key, cache_entry(value, now.tv_sec, validity)));
1643-
to_ret = &((*res.first).second);
1644-
*new_elem = res.second;
1651+
if(already_in)
1652+
{
1653+
(*it).second.response = value;
1654+
(*it).second.ts = now.tv_sec;
1655+
(*it).second.validity = validity;
1656+
to_ret = &(*it).second;
1657+
*new_elem = false;
1658+
}
1659+
else
1660+
{
1661+
pair<map<string, cache_entry>::iterator, bool> res = cache_m->response_cache.insert(pair<string, cache_entry>(key, cache_entry(value, now.tv_sec, validity)));
1662+
to_ret = &((*res.first).second);
1663+
*new_elem = res.second;
1664+
}
16451665
}
1646-
if(unlock)
1666+
if(already_in)
16471667
(*it).second.unlock();
16481668
if(lock)
16491669
to_ret->lock(write);

0 commit comments

Comments
 (0)