21#ifndef KICAD_DATABASE_CACHE_H
22#define KICAD_DATABASE_CACHE_H
28#include <unordered_map>
33template<
typename CacheValueType>
37 typedef std::pair<std::string, std::pair<time_t, CacheValueType>>
CACHE_ENTRY;
39 typedef std::unordered_map<std::string, typename std::list<CACHE_ENTRY>::iterator>
CACHE_TYPE;
50 void Put(
const std::string& aQuery,
const CacheValueType& aResult )
52 std::lock_guard<std::mutex> lock(
m_mutex );
54 auto it =
m_cache.find( aQuery );
56 time_t time = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
59 std::make_pair( time, aResult ) ) );
78 bool Get(
const std::string& aQuery, CacheValueType& aResult )
80 std::lock_guard<std::mutex> lock(
m_mutex );
82 auto it =
m_cache.find( aQuery );
87 time_t time = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
89 if( time - it->second->second.first >
m_maxAge )
98 aResult = it->second->second.second;
104 std::lock_guard<std::mutex> lock(
m_mutex );
110 std::lock_guard<std::mutex> lock(
m_mutex );
114 void Clear(
const std::string& aQuery )
116 std::lock_guard<std::mutex> lock(
m_mutex );
118 auto it =
m_cache.find( aQuery );
void Put(const std::string &aQuery, const CacheValueType &aResult)
std::pair< std::string, std::pair< time_t, CacheValueType > > CACHE_ENTRY
bool Get(const std::string &aQuery, CacheValueType &aResult)
std::list< CACHE_ENTRY > m_cacheMru
void SetMaxAge(time_t aMaxAge)
void Clear(const std::string &aQuery)
CACHE_TYPE::const_iterator CACHE_CITER
CacheValueType CACHE_VALUE
std::unordered_map< std::string, typename std::list< CACHE_ENTRY >::iterator > CACHE_TYPE
DATABASE_CACHE(size_t aMaxSize, time_t aMaxAge)
void SetMaxSize(size_t aMaxSize)