From 602d53ee9d5a45c25546cf840f232ffb290d9af4 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Thu, 11 Oct 2018 22:18:19 +0800 Subject: [PATCH] Modify CacheMap --- lib/inc/drogon/CacheMap.h | 47 ++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/inc/drogon/CacheMap.h b/lib/inc/drogon/CacheMap.h index 32bd6124..d5adac78 100755 --- a/lib/inc/drogon/CacheMap.h +++ b/lib/inc/drogon/CacheMap.h @@ -133,27 +133,54 @@ 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 timeoutCallback=std::function()) + void insert(const T1& key,T2 && value,size_t timeout=0,std::function timeoutCallback=std::function()) { if(timeout>0) { - + MapValue v; + v.value=std::move(value); + v.timeout=timeout; + v._timeoutCallback=std::move(timeoutCallback); std::lock_guard lock(mtx_); - _map[key].value=std::forward(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(); + v._weakEntryPtr=WeakCallbackEntryPtr(); std::lock_guard lock(mtx_); - _map[key].value=std::forward(value); - _map[key].timeout=timeout; - _map[key]._timeoutCallback=std::function(); - _map[key]._weakEntryPtr=WeakCallbackEntryPtr(); + _map[key]=std::move(v); } } + + void insert(const T1& key,const T2 & value,size_t timeout=0,std::function timeoutCallback=std::function()) + { + if(timeout>0) + { + MapValue v; + v.value=value; + v.timeout=timeout; + v._timeoutCallback=std::move(timeoutCallback); + std::lock_guard lock(mtx_); + _map[key]=std::move(v); + eraseAfter(timeout,key); + } + else + { + MapValue v; + v.value=value; + v.timeout=timeout; + v._timeoutCallback=std::function(); + v._weakEntryPtr=WeakCallbackEntryPtr(); + std::lock_guard lock(mtx_); + _map[key]=std::move(v); + } + } + T2& operator [](const T1& key){ int timeout=0;