QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef QUICHE_QUIC_CORE_QUIC_LRU_CACHE_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_LRU_CACHE_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_containers.h" |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 14 | |
| 15 | namespace quic { |
| 16 | |
| 17 | // A LRU cache that maps from type Key to Value* in QUIC. |
| 18 | // This cache CANNOT be shared by multiple threads (even with locks) because |
| 19 | // Value* returned by Lookup() can be invalid if the entry is evicted by other |
| 20 | // threads. |
| 21 | template <class K, class V> |
dschinazi | e211642 | 2019-10-29 11:54:26 -0700 | [diff] [blame] | 22 | class QUIC_NO_EXPORT QuicLRUCache { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | public: |
| 24 | explicit QuicLRUCache(size_t capacity) : capacity_(capacity) {} |
| 25 | QuicLRUCache(const QuicLRUCache&) = delete; |
| 26 | QuicLRUCache& operator=(const QuicLRUCache&) = delete; |
| 27 | |
| 28 | // Inserts one unit of |key|, |value| pair to the cache. Cache takes ownership |
| 29 | // of inserted |value|. |
| 30 | void Insert(const K& key, std::unique_ptr<V> value) { |
| 31 | auto it = cache_.find(key); |
| 32 | if (it != cache_.end()) { |
| 33 | cache_.erase(it); |
| 34 | } |
| 35 | cache_.emplace(key, std::move(value)); |
| 36 | |
| 37 | if (cache_.size() > capacity_) { |
| 38 | cache_.pop_front(); |
| 39 | } |
| 40 | DCHECK_LE(cache_.size(), capacity_); |
| 41 | } |
| 42 | |
| 43 | // If cache contains an entry for |key|, return a pointer to it. This returned |
| 44 | // value is guaranteed to be valid until Insert or Clear. |
| 45 | // Else return nullptr. |
| 46 | V* Lookup(const K& key) { |
| 47 | auto it = cache_.find(key); |
| 48 | if (it == cache_.end()) { |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | std::unique_ptr<V> value = std::move(it->second); |
| 53 | cache_.erase(it); |
| 54 | auto result = cache_.emplace(key, std::move(value)); |
| 55 | DCHECK(result.second); |
| 56 | return result.first->second.get(); |
| 57 | } |
| 58 | |
| 59 | // Removes all entries from the cache. |
| 60 | void Clear() { cache_.clear(); } |
| 61 | |
| 62 | // Returns maximum size of the cache. |
| 63 | size_t MaxSize() const { return capacity_; } |
| 64 | |
| 65 | // Returns current size of the cache. |
| 66 | size_t Size() const { return cache_.size(); } |
| 67 | |
| 68 | private: |
| 69 | QuicLinkedHashMap<K, std::unique_ptr<V>> cache_; |
| 70 | const size_t capacity_; |
| 71 | }; |
| 72 | |
| 73 | } // namespace quic |
| 74 | |
| 75 | #endif // QUICHE_QUIC_CORE_QUIC_LRU_CACHE_H_ |