Function CacheMap

Synopsis

#include <lib/inc/drogon/CacheMap.h>

CacheMap(trantor::EventLoop *loop, float tickInterval=TICK_INTERVAL, size_t wheelsNum=WHEELS_NUM, size_t bucketsNumPerWheel=BUCKET_NUM_PER_WHEEL)

Description

constructor

Parameters:

loop - eventloop pointer

tickInterval - second

wheelsNum - number of wheels

bucketsNumPerWheel - buckets number per wheel The max delay of the CacheMap is about tickInterval*(bucketsNumPerWheel^wheelsNum) seconds.

Source

Lines 86-127 in lib/inc/drogon/CacheMap.h.

CacheMap(trantor::EventLoop *loop,
         float tickInterval = TICK_INTERVAL,
         size_t wheelsNum = WHEELS_NUM,
         size_t bucketsNumPerWheel = BUCKET_NUM_PER_WHEEL)
    : loop_(loop),
      tickInterval_(tickInterval),
      wheelsNumber_(wheelsNum),
      bucketsNumPerWheel_(bucketsNumPerWheel)
{
    wheels_.resize(wheelsNumber_);
    for (size_t i = 0; i < wheelsNumber_; ++i)
    {
        wheels_[i].resize(bucketsNumPerWheel_);
    }
    if (tickInterval_ > 0 && wheelsNumber_ > 0 && bucketsNumPerWheel_ > 0)
    {
        timerId_ = loop_->runEvery(tickInterval_, [=]() {
            size_t t = ++ticksCounter_;
            size_t pow = 1;
            for (size_t i = 0; i < wheelsNumber_; ++i)
            {
                if ((t % pow) == 0)
                {
                    CallbackBucket tmp;
                    {
                        std::lock_guard<std::mutex> lock(bucketMutex_);
                        // use tmp val to make this critical area as short
                        // as possible.
                        wheels_[i].front().swap(tmp);
                        wheels_[i].pop_front();
                        wheels_[i].push_back(CallbackBucket());
                    }
                }
                pow = pow * bucketsNumPerWheel_;
            }
        });
    }
    else
    {
        noWheels_ = true;
    }
};





Add Discussion as Guest

Log in to DocsForge