Add the findAndFetch method to the CacheMap class

This commit is contained in:
antao 2018-11-27 09:40:20 +08:00
parent cd1139cf83
commit d356d38319
3 changed files with 28 additions and 4 deletions

View File

@ -203,6 +203,7 @@ class CacheMap
} }
return _map[key].value; return _map[key].value;
} }
bool find(const T1 &key) bool find(const T1 &key)
{ {
int timeout = 0; int timeout = 0;
@ -221,6 +222,26 @@ class CacheMap
return flag; return flag;
} }
bool findAndFetch(const T1 &key, T2 &value)
{
int timeout = 0;
bool flag = false;
std::lock_guard<std::mutex> lock(mtx_);
auto iter = _map.find(key);
if (iter != _map.end())
{
timeout = iter->second.timeout;
flag = true;
value = iter->second.value;
}
if (timeout > 0)
eraseAfter(timeout, key);
return flag;
}
void erase(const T1 &key) void erase(const T1 &key)
{ {
//in this case,we don't evoke the timeout callback; //in this case,we don't evoke the timeout callback;

View File

@ -63,7 +63,13 @@ int gzipCompress(const char *data, const size_t ndata,
int gzipDecompress(const char *zdata, const size_t nzdata, int gzipDecompress(const char *zdata, const size_t nzdata,
char *data, size_t *ndata); char *data, size_t *ndata);
/// Get the http full date string (RFC 822) /// Get the http full date string
/**
* rfc2616-3.3.1
* Full Date format(RFC 822)
* like this:Sun, 06 Nov 1994 08:49:37 GMT
* Wed, 12 Sep 2018 09:22:40 GMT
*/
char *getHttpFullDate(const trantor::Date &date); char *getHttpFullDate(const trantor::Date &date);
/// Get a formatted string /// Get a formatted string

View File

@ -405,9 +405,6 @@ int gzipDecompress(const char *zdata, const size_t nzdata,
} }
char *getHttpFullDate(const trantor::Date &date) char *getHttpFullDate(const trantor::Date &date)
{ {
//rfc2616-3.3.1
//Full Date format like this:Sun, 06 Nov 1994 08:49:37 GMT
// Wed, 12 Sep 2018 09:22:40 GMT
static __thread int64_t lastSecond = 0; static __thread int64_t lastSecond = 0;
static __thread char lastTimeString[128] = {0}; static __thread char lastTimeString[128] = {0};
auto nowSecond = date.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC; auto nowSecond = date.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC;