2018-10-08 17:22:17 +00:00
|
|
|
#include <drogon/CacheMap.h>
|
|
|
|
#include <drogon/utils/Utilities.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2019-05-17 14:49:09 +00:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <trantor/net/EventLoopThread.h>
|
|
|
|
#include <trantor/utils/Logger.h>
|
2018-10-08 17:22:17 +00:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-01-25 03:58:20 +00:00
|
|
|
trantor::Logger::setLogLevel(trantor::Logger::kTrace);
|
2018-10-14 07:56:54 +00:00
|
|
|
trantor::EventLoopThread loopThread;
|
|
|
|
loopThread.run();
|
2018-10-08 17:22:17 +00:00
|
|
|
|
2018-10-14 07:56:54 +00:00
|
|
|
auto loop = loopThread.getLoop();
|
|
|
|
std::shared_ptr<drogon::CacheMap<std::string, std::string>> main_cachePtr;
|
|
|
|
auto now = trantor::Date::date();
|
|
|
|
loop->runAt(now.after(1).roundSecond(), [=, &main_cachePtr]() {
|
2019-05-17 14:49:09 +00:00
|
|
|
std::shared_ptr<drogon::CacheMap<std::string, std::string>> cachePtr =
|
2019-05-18 03:11:45 +00:00
|
|
|
std::make_shared<drogon::CacheMap<std::string, std::string>>(loop,
|
|
|
|
0.1,
|
|
|
|
3,
|
|
|
|
50);
|
2018-10-14 07:56:54 +00:00
|
|
|
main_cachePtr = cachePtr;
|
|
|
|
LOG_DEBUG << "insert :usecount=" << main_cachePtr.use_count();
|
2019-05-18 03:11:45 +00:00
|
|
|
cachePtr->insert("1", "1", 3, [=]() {
|
|
|
|
LOG_DEBUG << "timeout!erase 1!";
|
|
|
|
});
|
2019-05-17 14:49:09 +00:00
|
|
|
cachePtr->insert("2", "2", 10, []() { LOG_DEBUG << "2 timeout"; });
|
2018-10-14 07:56:54 +00:00
|
|
|
});
|
|
|
|
trantor::EventLoop mainLoop;
|
|
|
|
mainLoop.runAt(now.after(3).roundSecond().after(0.0013), [&]() {
|
2019-05-17 14:49:09 +00:00
|
|
|
(*main_cachePtr)["new"] = "new";
|
2018-10-14 07:56:54 +00:00
|
|
|
if (main_cachePtr->find("1"))
|
|
|
|
{
|
2018-10-25 10:07:29 +00:00
|
|
|
LOG_DEBUG << "find item 1:" << (*main_cachePtr)["1"];
|
|
|
|
(*main_cachePtr)["1"] = "22";
|
|
|
|
LOG_DEBUG << (*main_cachePtr)["1"];
|
2018-10-14 07:56:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_DEBUG << "can't find item 1";
|
|
|
|
}
|
|
|
|
});
|
2019-05-17 14:49:09 +00:00
|
|
|
mainLoop.runAfter(8, [&]() { mainLoop.quit(); });
|
2018-10-14 07:56:54 +00:00
|
|
|
mainLoop.loop();
|
2018-11-16 05:26:14 +00:00
|
|
|
}
|