Chromium Code Reviews| 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 iterator iter = map_.find(key); | |
| 30 if (iter != map_.end()) | |
| 31 return iter->second; | |
| 32 return Value(); | |
| 33 } | |
| 34 | |
| 35 bool Contains(const Key& key) const { | |
| 36 return map_.find(key) != map_.end(); | |
| 37 } | |
| 38 | |
| 39 Value Remove(const Key& key) { | |
| 40 Value val = Get(key); | |
| 41 map_.erase(key); | |
| 42 return val; | |
| 43 } | |
| 44 | |
| 45 void Clear() { map_.clear(); } | |
| 46 | |
| 47 iterator begin() { return map_.begin(); } | |
| 48 iterator end() { return map_.end(); } | |
| 49 size_t MaximumCacheSize() const { return max_cache_size_; } | |
| 50 size_t size() const { return map_.size(); } | |
| 51 | |
| 52 private: | |
|
David Trainor- moved to gerrit
2014/06/17 08:12:11
DISALLOW_COPY_AND_ASSIGN
powei
2014/06/19 23:05:58
Done.
| |
| 53 void EvictIfFull() { | |
| 54 while (map_.size() > max_cache_size_) { | |
| 55 iterator it = map_.begin(); | |
|
David Trainor- moved to gerrit
2014/06/17 08:12:11
Just checking, is the cache supposed to expire bas
powei
2014/06/19 23:05:58
LinkedHashMap is insertion ordered. I think that'
| |
| 56 map_.erase(it); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 size_t max_cache_size_; | |
| 61 LinkedHashMap map_; | |
| 62 }; | |
| 63 | |
| 64 #endif // CHROME_BROWSER_ANDROID_THUMBNAIL_LRU_EXPIRING_CACHE_H_ | |
| OLD | NEW |