Modify CacheMap

This commit is contained in:
an-tao 2018-10-11 22:18:19 +08:00
parent 4171f54c41
commit 602d53ee9d
1 changed files with 37 additions and 10 deletions

View File

@ -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<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<T2>(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<T2>(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;