mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Implemented basic corebill tracking.
This commit is contained in:
42
src/util.cpp
42
src/util.cpp
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user