Chromium Code Reviews| Index: chrome/browser/android/thumbnail/lru_expiring_cache.h |
| diff --git a/chrome/browser/android/thumbnail/lru_expiring_cache.h b/chrome/browser/android/thumbnail/lru_expiring_cache.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..414ea3f553cfa88c47d8f8a5af088a0436f306aa |
| --- /dev/null |
| +++ b/chrome/browser/android/thumbnail/lru_expiring_cache.h |
| @@ -0,0 +1,64 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_ANDROID_THUMBNAIL_LRU_EXPIRING_CACHE_H_ |
| +#define CHROME_BROWSER_ANDROID_THUMBNAIL_LRU_EXPIRING_CACHE_H_ |
| + |
| +#include "net/base/linked_hash_map.h" |
| + |
| +template <class Key, class Value> |
| +class LRUExpiringCache { |
| + private: |
| + typedef linked_hash_map<Key, Value> LinkedHashMap; |
| + |
| + public: |
| + typedef typename LinkedHashMap::iterator iterator; |
| + |
| + explicit LRUExpiringCache(size_t max_cache_size) |
| + : max_cache_size_(max_cache_size) {} |
| + |
| + ~LRUExpiringCache() {} |
| + |
| + void Put(const Key& key, const Value& value) { |
| + map_[key] = value; |
| + EvictIfFull(); |
| + } |
| + |
| + Value Get(const Key& key) { |
| + iterator iter = map_.find(key); |
| + if (iter != map_.end()) |
| + return iter->second; |
| + return Value(); |
| + } |
| + |
| + bool Contains(const Key& key) const { |
| + return map_.find(key) != map_.end(); |
| + } |
| + |
| + Value Remove(const Key& key) { |
| + Value val = Get(key); |
| + map_.erase(key); |
| + return val; |
| + } |
| + |
| + void Clear() { map_.clear(); } |
| + |
| + iterator begin() { return map_.begin(); } |
| + iterator end() { return map_.end(); } |
| + size_t MaximumCacheSize() const { return max_cache_size_; } |
| + size_t size() const { return map_.size(); } |
| + |
| + private: |
|
David Trainor- moved to gerrit
2014/06/17 08:12:11
DISALLOW_COPY_AND_ASSIGN
powei
2014/06/19 23:05:58
Done.
|
| + void EvictIfFull() { |
| + while (map_.size() > max_cache_size_) { |
| + 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'
|
| + map_.erase(it); |
| + } |
| + } |
| + |
| + size_t max_cache_size_; |
| + LinkedHashMap map_; |
| +}; |
| + |
| +#endif // CHROME_BROWSER_ANDROID_THUMBNAIL_LRU_EXPIRING_CACHE_H_ |