Cache Map.
More...
#include <drogon/CacheMap.h>
|
| | 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) |
template<typename T1, typename T2>
class drogon::CacheMap< T1, T2 >
Cache Map.
- Template Parameters
-
| T1 | The keyword type. |
| T2 | The 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).
◆ 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
-
| loop | eventloop pointer |
| tickInterval | second |
| wheelsNum | number of wheels |
| bucketsNumPerWheel | buckets number per wheel |
| fnOnInsert | function to execute on insertion |
| fnOnErase | function to execute on erase |
The max delay of the CacheMap is about tickInterval*(bucketsNumPerWheel^wheelsNum) seconds.
◆ erase()
template<typename T1, typename T2>
Erase the value of the keyword.
- Parameters
-
- Note
- This function does not cause the timeout callback to be executed.
◆ findAndFetch()
template<typename T1, typename T2>
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>
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
-
| key | The key |
| value | The value |
| timeout | The 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. |
| timeoutCallback | is 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
-
| key | The key |
| value | The value |
| timeout | The 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. |
| timeoutCallback | is 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
-
| Callable | the type of the handler. |
- Parameters
-
| key | |
| handler | A callable that can modify or visit the data. The signature of the handler should be equivalent to 'void(T2&)' or 'void(const T2&)' |
| timeout | In 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>
Return the value of the keyword.
- Parameters
-
- 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
-
- 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: