Implemented basic corebill tracking.

This commit is contained in:
ravinsp
2019-11-08 13:20:34 +05:30
parent c5ab13b357
commit 7815390b25
9 changed files with 195 additions and 13 deletions

View File

@@ -40,6 +40,44 @@ bool rollover_hashset::try_emplace(const std::string hash)
return false; // Hash already exists.
}
// ttl_set class methods.
/**
* If key does not exist, inserts it with the specified ttl. If key exists,
* renews the expiration time to match the time-to-live from now onwards.
* @param key Object to insert.
* @param ttl Time to live in milliseonds.
*/
void ttl_set::emplace(const std::string key, uint64_t ttl_milli)
{
ttlmap[key] = util::get_epoch_milliseconds() + ttl_milli;
}
void ttl_set::erase(const std::string &key)
{
const auto itr = ttlmap.find(key);
if (itr != ttlmap.end())
ttlmap.erase(itr);
}
/**
* Returns true of the key exists and not expired. Returns false if key does not exist
* or has expired.
*/
bool ttl_set::exists(const std::string &key)
{
const auto itr = ttlmap.find(key);
if (itr == ttlmap.end()) // Not found
return false;
// Check whether we are passed the expiration time (itr->second is the expiration time)
const bool expired = util::get_epoch_milliseconds() > itr->second;
if (expired)
ttlmap.erase(itr);
return !expired;
}
/**
* Encodes provided bytes to hex string.
*
@@ -92,8 +130,8 @@ int hex2bin(unsigned char *decodedbuf, const size_t decodedbuf_len, std::string_
int64_t get_epoch_milliseconds()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
std::chrono::system_clock::now().time_since_epoch())
.count();
}
/**