|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License, version 2.0 |
| 5 | +// (GPLv2), as published by the Free Software Foundation, with the |
| 6 | +// following additional permissions: |
| 7 | +// |
| 8 | +// This program is distributed with certain software that is licensed |
| 9 | +// under separate terms, as designated in a particular file or component |
| 10 | +// or in the license documentation. Without limiting your rights under |
| 11 | +// the GPLv2, the authors of this program hereby grant you an additional |
| 12 | +// permission to link the program and your derivative works with the |
| 13 | +// separately licensed software that they have included with the program. |
| 14 | +// |
| 15 | +// Without limiting the foregoing grant of rights under the GPLv2 and |
| 16 | +// additional permission as to separately licensed software, this |
| 17 | +// program is also subject to the Universal FOSS Exception, version 1.0, |
| 18 | +// a copy of which can be found along with its FAQ at |
| 19 | +// http://oss.oracle.com/licenses/universal-foss-exception. |
| 20 | +// |
| 21 | +// This program is distributed in the hope that it will be useful, but |
| 22 | +// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 24 | +// See the GNU General Public License, version 2.0, for more details. |
| 25 | +// |
| 26 | +// You should have received a copy of the GNU General Public License |
| 27 | +// along with this program. If not, see |
| 28 | +// http://www.gnu.org/licenses/gpl-2.0.html. |
| 29 | + |
| 30 | +#include "cache_map.h" |
| 31 | + |
| 32 | +#include <utility> |
| 33 | + |
| 34 | +#include "custom_endpoint_info.h" |
| 35 | + |
| 36 | +template <class K, class V> |
| 37 | +void CACHE_MAP<K, V>::put(K key, V value, long long item_expiration_nanos) { |
| 38 | + this->cache[key] = std::make_shared<CACHE_ITEM>( |
| 39 | + value, std::chrono::steady_clock::now() + std::chrono::nanoseconds(item_expiration_nanos)); |
| 40 | +} |
| 41 | + |
| 42 | +template <class K, class V> |
| 43 | +V CACHE_MAP<K, V>::get(K key, V default_value) { |
| 44 | + if (cache.count(key) > 0 && !cache[key]->is_expired()) { |
| 45 | + return this->cache[key]->item; |
| 46 | + } |
| 47 | + return default_value; |
| 48 | +} |
| 49 | + |
| 50 | +template <class K, class V> |
| 51 | +V CACHE_MAP<K, V>::get(K key, V default_value, long long item_expiration_nanos) { |
| 52 | + if (cache.count(key) == 0 || this->cache[key]->is_expired()) { |
| 53 | + this->put(key, std::move(default_value), item_expiration_nanos); |
| 54 | + } |
| 55 | + return this->cache[key]->item; |
| 56 | +} |
| 57 | + |
| 58 | +template <class K, class V> |
| 59 | +void CACHE_MAP<K, V>::remove(K key) { |
| 60 | + if (this->cache.count(key)) { |
| 61 | + this->cache.erase(key); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +template <class K, class V> |
| 66 | +int CACHE_MAP<K, V>::size() { |
| 67 | + return this->cache.size(); |
| 68 | +} |
| 69 | + |
| 70 | +template <class K, class V> |
| 71 | +void CACHE_MAP<K, V>::clear() { |
| 72 | + this->cache.clear(); |
| 73 | + this->clean_up(); |
| 74 | +} |
| 75 | + |
| 76 | +template <class K, class V> |
| 77 | +void CACHE_MAP<K, V>::clean_up() { |
| 78 | + if (std::chrono::steady_clock::now() > this->clean_up_time_nanos.load()) { |
| 79 | + this->clean_up_time_nanos = |
| 80 | + std::chrono::steady_clock::now() + std::chrono::nanoseconds(this->clean_up_time_interval_nanos); |
| 81 | + std::vector<K> keys; |
| 82 | + keys.reserve(this->cache.size()); |
| 83 | + for (auto& [key, cache_item] : this->cache) { |
| 84 | + keys.push_back(key); |
| 85 | + } |
| 86 | + for (const auto& key : keys) { |
| 87 | + if (this->cache[key]->is_expired()) { |
| 88 | + this->cache.erase(key); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +template class CACHE_MAP<std::string, std::shared_ptr<CUSTOM_ENDPOINT_INFO>>; |
0 commit comments