Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: net/base/expiring_cache.h

Issue 10556022: Consider the verification time as well as the expiration time when caching certificate verification… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment fixes Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | net/base/expiring_cache_unittest.cc » ('j') | net/base/expiring_cache_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #pragma once 7 #pragma once
8 8
9 #include <functional>
9 #include <map> 10 #include <map>
10 #include <utility> 11 #include <utility>
11 12
12 #include "base/basictypes.h" 13 #include "base/basictypes.h"
13 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
14 #include "base/time.h" 15 #include "base/time.h"
15 16
16 namespace net { 17 namespace net {
17 18
18 // Cache implementation where all entries have an explicit expiration policy. As 19 // Cache implementation where all entries have an explicit expiration policy. As
19 // new items are added, expired items will be removed first. 20 // new items are added, expired items will be removed first.
20 // The template types have the following requirements: 21 // The template types have the following requirements:
21 // KeyType must be LessThanComparable, Assignable, and CopyConstructible. 22 // KeyType must be LessThanComparable, Assignable, and CopyConstructible.
22 // ValueType must be CopyConstructible and Assignable. 23 // ValueType must be CopyConstructible and Assignable.
23 template <typename KeyType, typename ValueType> 24 // ExpirationType must be CopyConstructible and Assignable.
25 // ExpirationCompare is a function class that takes two arguments of the
cbentzel 2012/06/18 19:08:28 This is interesting - given the name of the templa
26 // type ExpirationType and returns a bool. If |comp| is an instance of
27 // ExpirationCompare, then the expression |comp(current, expiration)| shall
28 // return true iff |current| is still valid within |expiration|.
29 template <typename KeyType, typename ValueType,
30 typename ExpirationType = base::TimeTicks,
wtc 2012/06/18 22:10:38 Nit: Ideally this type parameter should be named E
Ryan Sleevi 2012/06/19 00:59:28 Now at least, by virtue of taking ExpirationType +
31 typename ExpirationCompare = std::less<ExpirationType> >
24 class ExpiringCache { 32 class ExpiringCache {
25 private: 33 private:
26 // Intentionally violate the C++ Style Guide so that EntryMap is known to be 34 // Intentionally violate the C++ Style Guide so that EntryMap is known to be
27 // a dependent type. Without this, Clang's two-phase lookup complains when 35 // a dependent type. Without this, Clang's two-phase lookup complains when
28 // using EntryMap::const_iterator, while GCC and MSVC happily resolve the 36 // using EntryMap::const_iterator, while GCC and MSVC happily resolve the
29 // typename. 37 // typename.
30 38
31 // Tuple to represent the value and when it expires. 39 // Tuple to represent the value and when it expires.
32 typedef std::pair<ValueType, base::TimeTicks> Entry; 40 typedef std::pair<ValueType, ExpirationType> Entry;
33 typedef std::map<KeyType, Entry> EntryMap; 41 typedef std::map<KeyType, Entry> EntryMap;
34 42
35 public: 43 public:
36 typedef KeyType key_type; 44 typedef KeyType key_type;
37 typedef ValueType value_type; 45 typedef ValueType value_type;
46 typedef ExpirationType expiration_type;
38 47
39 // This class provides a read-only iterator over items in the ExpiringCache 48 // This class provides a read-only iterator over items in the ExpiringCache
40 class Iterator { 49 class Iterator {
41 public: 50 public:
42 explicit Iterator(const ExpiringCache& cache) 51 explicit Iterator(const ExpiringCache& cache)
43 : cache_(cache), 52 : cache_(cache),
44 it_(cache_.entries_.begin()) { 53 it_(cache_.entries_.begin()) {
45 } 54 }
46 ~Iterator() {} 55 ~Iterator() {}
47 56
48 bool HasNext() const { return it_ != cache_.entries_.end(); } 57 bool HasNext() const { return it_ != cache_.entries_.end(); }
49 void Advance() { ++it_; } 58 void Advance() { ++it_; }
50 59
51 const KeyType& key() const { return it_->first; } 60 const KeyType& key() const { return it_->first; }
52 const ValueType& value() const { return it_->second.first; } 61 const ValueType& value() const { return it_->second.first; }
53 const base::TimeTicks& expiration() const { return it_->second.second; } 62 const ExpirationType& expiration() const { return it_->second.second; }
54 63
55 private: 64 private:
56 const ExpiringCache& cache_; 65 const ExpiringCache& cache_;
57 66
58 // Use a second layer of type indirection, as both EntryMap and 67 // Use a second layer of type indirection, as both EntryMap and
59 // EntryMap::const_iterator are dependent types. 68 // EntryMap::const_iterator are dependent types.
60 typedef typename ExpiringCache::EntryMap EntryMap; 69 typedef typename ExpiringCache::EntryMap EntryMap;
61 typename EntryMap::const_iterator it_; 70 typename EntryMap::const_iterator it_;
62 }; 71 };
63 72
64 73
65 // Constructs an ExpiringCache that stores up to |max_entries|. 74 // Constructs an ExpiringCache that stores up to |max_entries|, using
66 explicit ExpiringCache(size_t max_entries) : max_entries_(max_entries) {} 75 // |expiration_comp| to check if elements are expired.
76 explicit ExpiringCache(
cbentzel 2012/06/18 19:08:28 You provided good justification for this, but it m
Ryan Sleevi 2012/06/19 00:59:28 Checking the internal style list that led to the G
77 size_t max_entries,
78 const ExpirationCompare& expiration_comp = ExpirationCompare())
79 : max_entries_(max_entries),
80 expiration_comp_(expiration_comp) {
81 }
82
67 ~ExpiringCache() {} 83 ~ExpiringCache() {}
68 84
69 // Returns the value matching |key|, which must be valid at the time |now|. 85 // Returns the value matching |key|, which must be valid at the time |now|.
70 // Returns NULL if the item is not found or has expired. If the item has 86 // Returns NULL if the item is not found or has expired. If the item has
71 // expired, it is immediately removed from the cache. 87 // expired, it is immediately removed from the cache.
72 // Note: The returned pointer remains owned by the ExpiringCache and is 88 // Note: The returned pointer remains owned by the ExpiringCache and is
73 // invalidated by a call to a non-const method. 89 // invalidated by a call to a non-const method.
74 const ValueType* Get(const KeyType& key, base::TimeTicks now) { 90 const ValueType* Get(const KeyType& key, const ExpirationType& now) {
75 typename EntryMap::iterator it = entries_.find(key); 91 typename EntryMap::iterator it = entries_.find(key);
76 if (it == entries_.end()) 92 if (it == entries_.end())
77 return NULL; 93 return NULL;
78 94
79 // Immediately remove expired entries. 95 // Immediately remove expired entries.
80 if (!CanUseEntry(it->second, now)) { 96 if (!expiration_comp_(now, it->second.second)) {
81 entries_.erase(it); 97 entries_.erase(it);
82 return NULL; 98 return NULL;
83 } 99 }
84 100
85 return &it->second.first; 101 return &it->second.first;
86 } 102 }
87 103
88 // Updates or replaces the value associated with |key|. 104 // Updates or replaces the value associated with |key|.
89 void Put(const KeyType& key, 105 void Put(const KeyType& key,
90 const ValueType& value, 106 const ValueType& value,
91 base::TimeTicks now, 107 const ExpirationType& now,
92 base::TimeDelta ttl) { 108 const ExpirationType& expiration) {
93 base::TimeTicks expiration = now + ttl;
94 typename EntryMap::iterator it = entries_.find(key); 109 typename EntryMap::iterator it = entries_.find(key);
95 if (it == entries_.end()) { 110 if (it == entries_.end()) {
96 // Compact the cache if it grew beyond the limit. 111 // Compact the cache if it grew beyond the limit.
97 if (entries_.size() == max_entries_ ) 112 if (entries_.size() == max_entries_ )
98 Compact(now); 113 Compact(now);
99 114
100 // No existing entry. Creating a new one. 115 // No existing entry. Creating a new one.
101 entries_.insert(std::make_pair(key, Entry(value, expiration))); 116 entries_.insert(std::make_pair(key, Entry(value, expiration)));
102 } else { 117 } else {
103 // Update an existing cache entry. 118 // Update an existing cache entry.
(...skipping 11 matching lines...) Expand all
115 size_t size() const { return entries_.size(); } 130 size_t size() const { return entries_.size(); }
116 131
117 // Returns the maximum number of entries in the cache. 132 // Returns the maximum number of entries in the cache.
118 size_t max_entries() const { return max_entries_; } 133 size_t max_entries() const { return max_entries_; }
119 134
120 bool empty() const { return entries_.empty(); } 135 bool empty() const { return entries_.empty(); }
121 136
122 private: 137 private:
123 FRIEND_TEST_ALL_PREFIXES(ExpiringCacheTest, Compact); 138 FRIEND_TEST_ALL_PREFIXES(ExpiringCacheTest, Compact);
124 139
125 // Returns true if this cache entry's result is valid at time |now|.
126 static bool CanUseEntry(const Entry& entry, const base::TimeTicks now) {
127 return entry.second > now;
128 }
129
130 // Prunes entries from the cache to bring it below |max_entries()|. 140 // Prunes entries from the cache to bring it below |max_entries()|.
131 void Compact(base::TimeTicks now) { 141 void Compact(const ExpirationType& now) {
132 // Clear out expired entries. 142 // Clear out expired entries.
133 typename EntryMap::iterator it; 143 typename EntryMap::iterator it;
134 for (it = entries_.begin(); it != entries_.end(); ) { 144 for (it = entries_.begin(); it != entries_.end(); ) {
135 if (!CanUseEntry(it->second, now)) { 145 if (!expiration_comp_(now, it->second.second)) {
136 entries_.erase(it++); 146 entries_.erase(it++);
137 } else { 147 } else {
138 ++it; 148 ++it;
139 } 149 }
140 } 150 }
141 151
142 if (entries_.size() < max_entries_) 152 if (entries_.size() < max_entries_)
143 return; 153 return;
144 154
145 // If the cache is still too full, start deleting items 'randomly'. 155 // If the cache is still too full, start deleting items 'randomly'.
146 for (it = entries_.begin(); 156 for (it = entries_.begin();
147 it != entries_.end() && entries_.size() >= max_entries_;) { 157 it != entries_.end() && entries_.size() >= max_entries_;) {
148 entries_.erase(it++); 158 entries_.erase(it++);
149 } 159 }
150 } 160 }
151 161
152 // Bound on total size of the cache. 162 // Bound on total size of the cache.
153 size_t max_entries_; 163 size_t max_entries_;
154 164
155 EntryMap entries_; 165 EntryMap entries_;
166 const ExpirationCompare expiration_comp_;
156 167
157 DISALLOW_COPY_AND_ASSIGN(ExpiringCache); 168 DISALLOW_COPY_AND_ASSIGN(ExpiringCache);
158 }; 169 };
159 170
160 } // namespace net 171 } // namespace net
161 172
162 #endif // NET_BASE_EXPIRING_CACHE_H_ 173 #endif // NET_BASE_EXPIRING_CACHE_H_
OLDNEW
« no previous file with comments | « no previous file | net/base/expiring_cache_unittest.cc » ('j') | net/base/expiring_cache_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698