File: database_cache.h

package info (click to toggle)
kicad 9.0.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 771,448 kB
  • sloc: cpp: 969,355; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 228; javascript: 167; perl: 10
file content (107 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2022 Jon Evans <jon@craftyjon.com>
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef KICAD_DATABASE_CACHE_H
#define KICAD_DATABASE_CACHE_H

#include <chrono>
#include <list>
#include <string>
#include <unordered_map>

#include <database/database_connection.h>


template<typename CacheValueType>
class DATABASE_CACHE
{
public:
    typedef std::pair<std::string, std::pair<time_t, CacheValueType>> CACHE_ENTRY;

    typedef std::unordered_map<std::string, typename std::list<CACHE_ENTRY>::iterator> CACHE_TYPE;

    typedef typename CACHE_TYPE::const_iterator CACHE_CITER;

    typedef CacheValueType CACHE_VALUE;

    DATABASE_CACHE( size_t aMaxSize, time_t aMaxAge ) :
            m_maxSize( aMaxSize ),
            m_maxAge( aMaxAge )
    {}

    void Put( const std::string& aQuery, const CacheValueType& aResult )
    {
        auto it = m_cache.find( aQuery );

        time_t time = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );

        m_cacheMru.push_front( std::make_pair( aQuery,
                                               std::make_pair( time, aResult ) ) );

        if( it != m_cache.end() )
        {
            m_cacheMru.erase( it->second );
            m_cache.erase( it );
        }

        m_cache[aQuery] = m_cacheMru.begin();

        if( m_cache.size() > m_maxSize )
        {
            auto last = m_cacheMru.end();
            last--;
            m_cache.erase( last->first );
            m_cacheMru.pop_back();
        }
    }

    bool Get( const std::string& aQuery, CacheValueType& aResult )
    {
        auto it = m_cache.find( aQuery );

        if( it == m_cache.end() )
            return false;

        time_t time = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );

        if( time - it->second->second.first > m_maxAge )
        {
            m_cacheMru.erase( it->second );
            m_cache.erase( it );
            return false;
        }

        m_cacheMru.splice( m_cacheMru.begin(), m_cacheMru, it->second );

        aResult = it->second->second.second;
        return true;
    }

    void SetMaxSize( size_t aMaxSize ) { m_maxSize = aMaxSize; }
    void SetMaxAge( time_t aMaxAge ) { m_maxAge = aMaxAge; }

private:
    size_t m_maxSize;
    time_t m_maxAge;
    std::list<CACHE_ENTRY> m_cacheMru;
    CACHE_TYPE m_cache;
};

#endif //KICAD_DATABASE_CACHE_H