CppCommon  1.0.4.1
C++ Common Library
cache_memcache.cpp

Memory cache example

#include "cache/memcache.h"
#include "threads/thread.h"
#include <iostream>
int main(int argc, char** argv)
{
// Fill the memory cache
cache.insert("123", 123);
cache.insert("456", 456, CppCommon::Timespan::milliseconds(100));
int result;
// Get the memory cache values
if (cache.find("123", result))
std::cout << "Found: " << result << std::endl;
if (cache.find("456", result))
std::cout << "Found: " << result << std::endl;
// Sleep for a while...
// Watchdog the memory cache to erase entries with timeout
cache.watchdog();
// Get the memory cache values
if (cache.find("123", result))
std::cout << "Found: " << result << std::endl;
if (cache.find("456", result))
std::cout << "Found: " << result << std::endl;
return 0;
}
Memory cache.
Definition: memcache.h:30
void watchdog(const UtcTimestamp &utc=UtcTimestamp())
Watchdog the memory cache.
Definition: memcache.inl:148
bool find(const TKey &key)
Try to find the cache value by the given key.
Definition: memcache.inl:70
bool insert(const TKey &key, const TValue &value, const Timespan &timeout=Timespan(0))
Insert a new cache value with the given timeout into the memory cache.
Definition: memcache.inl:48
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
Memory cache definition.
Thread definition.