drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
drogon::CacheMap< T1, T2 > Class Template Reference

Cache Map. More...

#include <drogon/CacheMap.h>

Classes

struct  MapValue

Public Member Functions

 CacheMap (trantor::EventLoop *loop, float tickInterval=TICK_INTERVAL, size_t wheelsNum=WHEELS_NUM, size_t bucketsNumPerWheel=BUCKET_NUM_PER_WHEEL, std::function< void(const T1 &)> fnOnInsert=nullptr, std::function< void(const T1 &)> fnOnErase=nullptr)
 constructor
void insert (const T1 &key, T2 &&value, size_t timeout=0, std::function< void()> timeoutCallback=std::function< void()>())
 Insert a key-value pair into the cache.
void insert (const T1 &key, const T2 &value, size_t timeout=0, std::function< void()> timeoutCallback=std::function< void()>())
 Insert a key-value pair into the cache.
T2 operator[] (const T1 &key)
 Return the value of the keyword.
template<typename Callable>
void modify (const T1 &key, Callable &&handler, size_t timeout=0)
 Modify or visit the data identified by the key parameter.
bool find (const T1 &key)
 Check if the value of the keyword exists.
bool findAndFetch (const T1 &key, T2 &value)
 Atomically find and get the value of a keyword.
void erase (const T1 &key)
 Erase the value of the keyword.
trantor::EventLoop * getLoop ()
 Get the event loop object.
void runAfter (size_t delay, std::function< void()> &&task)
 run the task function after a period of time.
void runAfter (size_t delay, const std::function< void()> &task)

Detailed Description

template<typename T1, typename T2>
class drogon::CacheMap< T1, T2 >

Cache Map.

Template Parameters
T1The keyword type.
T2The value type.
Note
Four wheels with 200 buckets per wheel means the cache map can work with a timeout up to 200^4 seconds (about 50 years).

Constructor & Destructor Documentation

◆ CacheMap()

template<typename T1, typename T2>
drogon::CacheMap< T1, T2 >::CacheMap ( trantor::EventLoop * loop,
float tickInterval = TICK_INTERVAL,
size_t wheelsNum = WHEELS_NUM,
size_t bucketsNumPerWheel = BUCKET_NUM_PER_WHEEL,
std::function< void(const T1 &)> fnOnInsert = nullptr,
std::function< void(const T1 &)> fnOnErase = nullptr )
inline

constructor

Parameters
loopeventloop pointer
tickIntervalsecond
wheelsNumnumber of wheels
bucketsNumPerWheelbuckets number per wheel
fnOnInsertfunction to execute on insertion
fnOnErasefunction to execute on erase

The max delay of the CacheMap is about tickInterval*(bucketsNumPerWheel^wheelsNum) seconds.

Member Function Documentation

◆ erase()

template<typename T1, typename T2>
void drogon::CacheMap< T1, T2 >::erase ( const T1 & key)
inline

Erase the value of the keyword.

Parameters
keythe keyword.
Note
This function does not cause the timeout callback to be executed.

◆ findAndFetch()

template<typename T1, typename T2>
bool drogon::CacheMap< T1, T2 >::findAndFetch ( const T1 & key,
T2 & value )
inline

Atomically find and get the value of a keyword.

Return true when the value is found, and the value is assigned to the value argument.

◆ getLoop()

template<typename T1, typename T2>
trantor::EventLoop * drogon::CacheMap< T1, T2 >::getLoop ( )
inline

Get the event loop object.

Returns
trantor::EventLoop*

◆ insert() [1/2]

template<typename T1, typename T2>
void drogon::CacheMap< T1, T2 >::insert ( const T1 & key,
const T2 & value,
size_t timeout = 0,
std::function< void()> timeoutCallback = std::function<void()>() )
inline

Insert a key-value pair into the cache.

Parameters
keyThe key
valueThe value
timeoutThe timeout in seconds, if timeout > 0, the value will be erased within the 'timeout' seconds after the last access. If the timeout is zero, the value exists until being removed explicitly.
timeoutCallbackis called when the timeout expires.

◆ insert() [2/2]

template<typename T1, typename T2>
void drogon::CacheMap< T1, T2 >::insert ( const T1 & key,
T2 && value,
size_t timeout = 0,
std::function< void()> timeoutCallback = std::function<void()>() )
inline

Insert a key-value pair into the cache.

Parameters
keyThe key
valueThe value
timeoutThe timeout in seconds, if timeout > 0, the value will be erased within the 'timeout' seconds after the last access. If the timeout is zero, the value exists until being removed explicitly.
timeoutCallbackis called when the timeout expires.

◆ modify()

template<typename T1, typename T2>
template<typename Callable>
void drogon::CacheMap< T1, T2 >::modify ( const T1 & key,
Callable && handler,
size_t timeout = 0 )
inline

Modify or visit the data identified by the key parameter.

Template Parameters
Callablethe type of the handler.
Parameters
key
handlerA callable that can modify or visit the data. The signature of the handler should be equivalent to 'void(T2&)' or 'void(const T2&)'
timeoutIn seconds.
Note
This function is multiple-thread safe. if the data identified by the key doesn't exist, a new one is created and passed to the handler and stored in the cache with the timeout parameter. The changing of the data is protected by the mutex of the cache.

◆ operator[]()

template<typename T1, typename T2>
T2 drogon::CacheMap< T1, T2 >::operator[] ( const T1 & key)
inline

Return the value of the keyword.

Parameters
key
Returns
T2
Note
This function returns a copy of the data in the cache. If the data is not found, a default T2 type value is returned and nothing is inserted into the cache.

◆ runAfter()

template<typename T1, typename T2>
void drogon::CacheMap< T1, T2 >::runAfter ( size_t delay,
std::function< void()> && task )
inline

run the task function after a period of time.

Parameters
delayin seconds
task
Note
This timer is a low-precision timer whose accuracy depends on the tickInterval parameter of the cache. The advantage of the timer is its low cost.

The documentation for this class was generated from the following file: