1 Star 0 Fork 1

陈奇/ComWeChatRobot

forked from Janisa/ComWeChatRobot 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
utils.h 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
amchii 提交于 2022-08-18 19:01 +08:00 . pre-commit忽略生成的_i.h,_p.c文件
#pragma once
#include <set>
#include <chrono>
#define CHRONO std::chrono
typedef unsigned long long ull;
/*
* 一个简单的带过期时间检查和清理的集合,interval为毫秒
* ExpireSet es(1000); // 过期时间1秒
* es.CheckIfDuplicatedAndAdd(1); true, 未重复
* es.CheckIfDuplicatedAndAdd(1); false, 重复
* After 1s;
* es.CheckIfDuplicatedAndAdd(2); 添加<2>并清理过期的元素<1>
*/
class ExpireSet
{
public:
ull interval; // 毫秒
ExpireSet(ull interval)
{
this->interval = interval;
this->expires_at = 0;
}
bool CheckIfDuplicatedAndAdd(ull id)
{
bool ok = true;
if (ids.count(id) != 0)
{
ok = false;
}
Add(id);
return ok;
}
private:
std::set<ull> ids;
ull expires_at;
void Add(ull id)
{
// 毫秒
auto now_ts = CHRONO::time_point_cast<CHRONO::milliseconds>(CHRONO::system_clock::now()).time_since_epoch().count();
#pragma warning(disable : 4018)
if (expires_at < now_ts)
{
ids.clear();
expires_at = now_ts + interval;
}
#pragma warning(default : 4018)
ids.insert(id);
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/chen_q_i/ComWeChatRobot.git
git@gitee.com:chen_q_i/ComWeChatRobot.git
chen_q_i
ComWeChatRobot
ComWeChatRobot
master

搜索帮助