OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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 NET_QUIC_PLATFORM_IMPL_QUIC_LRU_CACHE_IMPL_H_ |
| 6 #define NET_QUIC_PLATFORM_IMPL_QUIC_LRU_CACHE_IMPL_H_ |
| 7 |
| 8 #include "base/containers/mru_cache.h" |
| 9 |
| 10 namespace net { |
| 11 |
| 12 template <class K, class V> |
| 13 class QuicLRUCacheImpl { |
| 14 public: |
| 15 explicit QuicLRUCacheImpl(int64_t total_units) : mru_cache_(total_units) {} |
| 16 |
| 17 // Inserts one unit of |key|, |value| pair to the cache. |
| 18 void Insert(const K& key, std::unique_ptr<V> value) { |
| 19 mru_cache_.Put(key, std::move(value)); |
| 20 } |
| 21 |
| 22 // If cache contains an entry for |key|, return a pointer to it. This returned |
| 23 // value is guaranteed to be valid until Insert or Clear. |
| 24 // Else return nullptr. |
| 25 V* Lookup(const K& key) { |
| 26 auto cached_it = mru_cache_.Get(key); |
| 27 if (cached_it != mru_cache_.end()) { |
| 28 return cached_it->second.get(); |
| 29 } |
| 30 return nullptr; |
| 31 } |
| 32 |
| 33 // Removes all entries from the cache. |
| 34 void Clear() { mru_cache_.Clear(); } |
| 35 |
| 36 // Returns maximum size of the cache. |
| 37 int64_t MaxSize() const { return mru_cache_.max_size(); } |
| 38 |
| 39 // Returns current size of the cache. |
| 40 int64_t Size() const { return mru_cache_.size(); } |
| 41 |
| 42 private: |
| 43 base::MRUCache<K, std::unique_ptr<V>> mru_cache_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(QuicLRUCacheImpl); |
| 46 }; |
| 47 |
| 48 } // namespace net |
| 49 |
| 50 #endif // NET_QUIC_PLATFORM_IMPL_QUIC_LRU_CACHE_IMPL_H_ |
OLD | NEW |