KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_cache.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 Jon Evans <[email protected]>
5 * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef KICAD_DATABASE_CACHE_H
22#define KICAD_DATABASE_CACHE_H
23
24#include <chrono>
25#include <list>
26#include <string>
27#include <unordered_map>
28
30
31
32template<typename CacheValueType>
34{
35public:
36 typedef std::pair<std::string, std::pair<time_t, CacheValueType>> CACHE_ENTRY;
37
38 typedef std::unordered_map<std::string, typename std::list<CACHE_ENTRY>::iterator> CACHE_TYPE;
39
40 typedef typename CACHE_TYPE::const_iterator CACHE_CITER;
41
42 typedef CacheValueType CACHE_VALUE;
43
44 DATABASE_CACHE( size_t aMaxSize, time_t aMaxAge ) :
45 m_maxSize( aMaxSize ),
46 m_maxAge( aMaxAge )
47 {}
48
49 void Put( const std::string& aQuery, const CacheValueType& aResult )
50 {
51 auto it = m_cache.find( aQuery );
52
53 time_t time = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
54
55 m_cacheMru.push_front( std::make_pair( aQuery,
56 std::make_pair( time, aResult ) ) );
57
58 if( it != m_cache.end() )
59 {
60 m_cacheMru.erase( it->second );
61 m_cache.erase( it );
62 }
63
64 m_cache[aQuery] = m_cacheMru.begin();
65
66 if( m_cache.size() > m_maxSize )
67 {
68 auto last = m_cacheMru.end();
69 last--;
70 m_cache.erase( last->first );
71 m_cacheMru.pop_back();
72 }
73 }
74
75 bool Get( const std::string& aQuery, CacheValueType& aResult )
76 {
77 auto it = m_cache.find( aQuery );
78
79 if( it == m_cache.end() )
80 return false;
81
82 time_t time = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
83
84 if( time - it->second->second.first > m_maxAge )
85 {
86 m_cacheMru.erase( it->second );
87 m_cache.erase( it );
88 return false;
89 }
90
91 m_cacheMru.splice( m_cacheMru.begin(), m_cacheMru, it->second );
92
93 aResult = it->second->second.second;
94 return true;
95 }
96
97 void SetMaxSize( size_t aMaxSize ) { m_maxSize = aMaxSize; }
98 void SetMaxAge( time_t aMaxAge ) { m_maxAge = aMaxAge; }
99
100private:
101 size_t m_maxSize;
102 time_t m_maxAge;
103 std::list<CACHE_ENTRY> m_cacheMru;
105};
106
107#endif //KICAD_DATABASE_CACHE_H
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)
CACHE_TYPE::const_iterator CACHE_CITER
CACHE_TYPE m_cache
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)