CppCommon  1.0.4.1
C++ Common Library
cache_filecache.cpp

File cache example

#include "threads/thread.h"
#include <iostream>
int main(int argc, char** argv)
{
// Fill the file cache
cache.insert("123", "123");
cache.insert("456", "456", CppCommon::Timespan::milliseconds(100));
std::pair<bool, std::string_view> result;
// Get the memory cache values
result = cache.find("123");
if (result.first)
std::cout << "Found: " << result.second << std::endl;
result = cache.find("456");
if (result.first)
std::cout << "Found: " << result.second << std::endl;
// Sleep for a while...
// Watchdog the memory cache to erase entries with timeout
cache.watchdog();
// Get the memory cache values
result = cache.find("123");
if (result.first)
std::cout << "Found: " << result.second << std::endl;
result = cache.find("456");
if (result.first)
std::cout << "Found: " << result.second << std::endl;
return 0;
}
File cache.
Definition: filecache.h:36
bool insert(const std::string &key, const std::string &value, const Timespan &timeout=Timespan(0))
Insert a new cache value with the given timeout into the file cache.
Definition: filecache.cpp:34
void watchdog(const UtcTimestamp &utc=UtcTimestamp())
Watchdog the file cache.
Definition: filecache.cpp:226
std::pair< bool, std::string_view > find(const std::string &key)
Try to find the cache value by the given key.
Definition: filecache.cpp:55
static void SleepFor(const Timespan &timespan) noexcept
Sleep the current thread for the given timespan.
Definition: thread.cpp:83
int64_t milliseconds() const noexcept
Get total milliseconds of the current timespan.
Definition: timespan.h:141
File cache definition.
Thread definition.