From d356d383194bd503ff9adf99eefd9cebb7fedd22 Mon Sep 17 00:00:00 2001 From: antao Date: Tue, 27 Nov 2018 09:40:20 +0800 Subject: [PATCH] Add the findAndFetch method to the CacheMap class --- lib/inc/drogon/CacheMap.h | 21 +++++++++++++++++++++ lib/inc/drogon/utils/Utilities.h | 8 +++++++- lib/src/Utilities.cc | 3 --- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/inc/drogon/CacheMap.h b/lib/inc/drogon/CacheMap.h index c0566c4a..fe92d1ac 100755 --- a/lib/inc/drogon/CacheMap.h +++ b/lib/inc/drogon/CacheMap.h @@ -203,6 +203,7 @@ class CacheMap } return _map[key].value; } + bool find(const T1 &key) { int timeout = 0; @@ -221,6 +222,26 @@ class CacheMap return flag; } + + bool findAndFetch(const T1 &key, T2 &value) + { + int timeout = 0; + bool flag = false; + std::lock_guard 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) { //in this case,we don't evoke the timeout callback; diff --git a/lib/inc/drogon/utils/Utilities.h b/lib/inc/drogon/utils/Utilities.h index 663913a7..6901a804 100755 --- a/lib/inc/drogon/utils/Utilities.h +++ b/lib/inc/drogon/utils/Utilities.h @@ -63,7 +63,13 @@ int gzipCompress(const char *data, const size_t ndata, int gzipDecompress(const char *zdata, const size_t nzdata, 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); /// Get a formatted string diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index 185158c8..74ebf7a8 100755 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -405,9 +405,6 @@ int gzipDecompress(const char *zdata, const size_t nzdata, } 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 char lastTimeString[128] = {0}; auto nowSecond = date.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC;