OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 CHROME_BROWSER_ANDROID_THUMBNAIL_LRU_EXPIRING_CACHE_H_ |
| 6 #define CHROME_BROWSER_ANDROID_THUMBNAIL_LRU_EXPIRING_CACHE_H_ |
| 7 |
| 8 #include "net/base/linked_hash_map.h" |
| 9 |
| 10 template <class Key, class Value> |
| 11 class LRUExpiringCache { |
| 12 private: |
| 13 typedef linked_hash_map<Key, Value> LinkedHashMap; |
| 14 |
| 15 public: |
| 16 typedef typename LinkedHashMap::iterator iterator; |
| 17 |
| 18 explicit LRUExpiringCache(size_t max_cache_size) |
| 19 : max_cache_size_(max_cache_size) {} |
| 20 |
| 21 ~LRUExpiringCache() {} |
| 22 |
| 23 void Put(const Key& key, const Value& value) { |
| 24 map_[key] = value; |
| 25 EvictIfFull(); |
| 26 } |
| 27 |
| 28 Value& Get(const Key& key) { |
| 29 DCHECK(Contains(key)); |
| 30 iterator iter = map_.find(key); |
| 31 return iter->second; |
| 32 } |
| 33 |
| 34 bool Contains(const Key& key) const { |
| 35 return map_.find(key) != map_.end(); |
| 36 } |
| 37 |
| 38 void Remove(const Key& key) { |
| 39 map_.erase(key); |
| 40 } |
| 41 |
| 42 void Clear() { map_.clear(); } |
| 43 |
| 44 iterator begin() { return map_.begin(); } |
| 45 iterator end() { return map_.end(); } |
| 46 size_t MaximumCacheSize() const { return max_cache_size_; } |
| 47 size_t size() const { return map_.size(); } |
| 48 |
| 49 private: |
| 50 void EvictIfFull() { |
| 51 while (map_.size() > max_cache_size_) { |
| 52 iterator it = map_.begin(); |
| 53 map_.erase(it); |
| 54 } |
| 55 } |
| 56 |
| 57 size_t max_cache_size_; |
| 58 LinkedHashMap map_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(LRUExpiringCache); |
| 61 }; |
| 62 |
| 63 #endif // CHROME_BROWSER_ANDROID_THUMBNAIL_LRU_EXPIRING_CACHE_H_ |
OLD | NEW |