1 | /* |
---|
2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
---|
3 | |
---|
4 | This file is part of SINA. |
---|
5 | SINA is free software: you can redistribute it and/or modify it under |
---|
6 | the terms of the GNU General Public License as published by the Free |
---|
7 | Software Foundation, either version 3 of the License, or (at your |
---|
8 | option) any later version. |
---|
9 | |
---|
10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
13 | for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU General Public License |
---|
16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | Additional permission under GNU GPL version 3 section 7 |
---|
19 | |
---|
20 | If you modify SINA, or any covered work, by linking or combining it |
---|
21 | with components of ARB (or a modified version of that software), |
---|
22 | containing parts covered by the terms of the |
---|
23 | ARB-public-library-license, the licensors of SINA grant you additional |
---|
24 | permission to convey the resulting work. Corresponding Source for a |
---|
25 | non-source form of such a combination shall include the source code |
---|
26 | for the parts of ARB used as well as that of the covered work. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef _CACHE_H_ |
---|
30 | #define _CACHE_H_ |
---|
31 | |
---|
32 | #include <mutex> |
---|
33 | #include <list> |
---|
34 | #include <unordered_map> |
---|
35 | |
---|
36 | namespace sina { |
---|
37 | |
---|
38 | template<typename KEY, typename VALUE> |
---|
39 | class fifo_cache { |
---|
40 | public: |
---|
41 | explicit fifo_cache(size_t size) : _size(size) {} |
---|
42 | void store(KEY&& key, VALUE&& value) { |
---|
43 | std::lock_guard<std::mutex> lock(_mutex); |
---|
44 | auto it = _keys.find(key); |
---|
45 | if (it != _keys.end()) { |
---|
46 | _items.erase(it->second); |
---|
47 | _keys.erase(it); |
---|
48 | } |
---|
49 | _items.emplace_front(std::make_pair(KEY(key), value)); |
---|
50 | _keys.emplace(key, _items.begin()); |
---|
51 | if (_keys.size() > _size) { |
---|
52 | _keys.erase(_items.back().first); |
---|
53 | _items.pop_back(); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | bool try_get(const KEY& key, VALUE& val) { |
---|
58 | std::lock_guard<std::mutex> lock(_mutex); |
---|
59 | auto it = _keys.find(key); |
---|
60 | if (it == _keys.end()) { |
---|
61 | return false; |
---|
62 | } |
---|
63 | val = std::move(it->second->second); |
---|
64 | _items.erase(it->second); |
---|
65 | _keys.erase(it); |
---|
66 | return true; |
---|
67 | } |
---|
68 | |
---|
69 | private: |
---|
70 | size_t _size; |
---|
71 | using list_type = std::list<std::pair<KEY, VALUE>>; |
---|
72 | list_type _items; |
---|
73 | std::unordered_map<KEY, typename list_type::iterator> _keys; |
---|
74 | std::mutex _mutex; |
---|
75 | }; |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | } // namespace SINA |
---|
80 | |
---|
81 | #endif |
---|
82 | |
---|
83 | /* |
---|
84 | Local Variables: |
---|
85 | mode:c++ |
---|
86 | c-file-style:"stroustrup" |
---|
87 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
---|
88 | indent-tabs-mode:nil |
---|
89 | fill-column:99 |
---|
90 | End: |
---|
91 | */ |
---|
92 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
---|
93 | |
---|