-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg_cache-dedup.cpp
More file actions
66 lines (53 loc) · 1.49 KB
/
msg_cache-dedup.cpp
File metadata and controls
66 lines (53 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// msg_cache-dedup.cpp
//
// Created by dev on 2021-10-28.
// Copyright © 2021 Root Interface. All rights reserved.
//
#include "msg_cache-dedup.h"
msg_cache::msg_cache(long l_poll_res_milliseconds)
{
// https://stackoverflow.com/questions/30425772/c-11-calling-a-c-function-periodically
//
execute_= true;
_thd = std::thread([this, l_poll_res_milliseconds]()
{
while (execute_.load(std::memory_order_acquire))
{
t_entries entries_del;
// printf("theard::run\n");
for (auto it : cache_)
{
t_entry entry_del= it.first;
t_entry_context ctx_del= it.second;
if (ctx_del.expired())
entries_del.insert( {entry_del, ctx_del} );
// printf("ENTRY: %s\n", entry_del.c_str());
}
for (auto it : entries_del) {
cache_.erase(it.first);
// printf("PURGE: %s\n", it.first.c_str());
}
std::this_thread::sleep_for(std::chrono::milliseconds(l_poll_res_milliseconds));
}
});
}
msg_cache::~msg_cache()
{
if ( execute_.load(std::memory_order_acquire) ) {
execute_.store(false, std::memory_order_release);
if ( _thd.joinable() )
_thd.join();
};
}
void msg_cache::cache(t_entry entry, t_entry_context ctx) {
cache_.insert( {entry, ctx} ); // std:make_pair()
}
std::tuple<bool,t_eindex> msg_cache::contains(t_entry entry) const {
//TODO: cache_.contains(entry); c++20
t_eindex ei= cache_.find(entry);
return std::make_tuple(ei != cache_.end(), ei);
}
void msg_cache::expire(t_eindex t_ei) {
cache_.erase(t_ei);
}