| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_EXPIRING_CACHE_H_ | 5 #ifndef NET_BASE_EXPIRING_CACHE_H_ |
| 6 #define NET_BASE_EXPIRING_CACHE_H_ | 6 #define NET_BASE_EXPIRING_CACHE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 | 113 |
| 114 // Immediately remove expired entries. | 114 // Immediately remove expired entries. |
| 115 if (!expiration_comp_(now, it->second.second)) { | 115 if (!expiration_comp_(now, it->second.second)) { |
| 116 entries_.erase(it); | 116 entries_.erase(it); |
| 117 return NULL; | 117 return NULL; |
| 118 } | 118 } |
| 119 | 119 |
| 120 return &it->second.first; | 120 return &it->second.first; |
| 121 } | 121 } |
| 122 | 122 |
| 123 // Updates or replaces the value associated with |key|. | 123 // Updates or replaces the value associated with |key|. Returns a pointer to |
| 124 void Put(const KeyType& key, | 124 // the stored value. |
| 125 const ValueType& value, | 125 ValueType* Put(const KeyType& key, |
| 126 const ExpirationType& now, | 126 const ValueType& value, |
| 127 const ExpirationType& expiration) { | 127 const ExpirationType& now, |
| 128 const ExpirationType& expiration) { |
| 128 typename EntryMap::iterator it = entries_.find(key); | 129 typename EntryMap::iterator it = entries_.find(key); |
| 129 if (it == entries_.end()) { | 130 if (it == entries_.end()) { |
| 130 // Compact the cache if it grew beyond the limit. | 131 // Compact the cache if it grew beyond the limit. |
| 131 if (entries_.size() == max_entries_ ) | 132 if (entries_.size() == max_entries_ ) |
| 132 Compact(now); | 133 Compact(now); |
| 133 | 134 |
| 134 // No existing entry. Creating a new one. | 135 // No existing entry. Creating a new one. |
| 135 entries_.insert(std::make_pair(key, Entry(value, expiration))); | 136 it = entries_.insert(std::make_pair(key, Entry(value, expiration))).first; |
| 136 } else { | 137 } else { |
| 137 // Update an existing cache entry. | 138 // Update an existing cache entry. |
| 138 it->second.first = value; | 139 it->second.first = value; |
| 139 it->second.second = expiration; | 140 it->second.second = expiration; |
| 140 } | 141 } |
| 142 return &it->second.first; |
| 141 } | 143 } |
| 142 | 144 |
| 143 // Empties the cache. | 145 // Empties the cache. |
| 144 void Clear() { | 146 void Clear() { |
| 145 entries_.clear(); | 147 entries_.clear(); |
| 146 } | 148 } |
| 147 | 149 |
| 148 // Returns the number of entries in the cache. | 150 // Returns the number of entries in the cache. |
| 149 size_t size() const { return entries_.size(); } | 151 size_t size() const { return entries_.size(); } |
| 150 | 152 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 186 |
| 185 EntryMap entries_; | 187 EntryMap entries_; |
| 186 ExpirationCompare expiration_comp_; | 188 ExpirationCompare expiration_comp_; |
| 187 | 189 |
| 188 DISALLOW_COPY_AND_ASSIGN(ExpiringCache); | 190 DISALLOW_COPY_AND_ASSIGN(ExpiringCache); |
| 189 }; | 191 }; |
| 190 | 192 |
| 191 } // namespace net | 193 } // namespace net |
| 192 | 194 |
| 193 #endif // NET_BASE_EXPIRING_CACHE_H_ | 195 #endif // NET_BASE_EXPIRING_CACHE_H_ |
| OLD | NEW |