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

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: 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') | no next file with comments »
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
szym 2012/06/15 22:19:30 nit: end with '.'
25 // ExpirationCompare is a class that takes two arguments of the
szym 2012/06/15 22:19:30 I suggest "function class" or "class of function o
26 // type ExpirationType and returns a bool. The expression comp(a, b),
27 // where comp is an object of the comparison class and |a| and |b| are
28 // ExpirationType values, shall return true if |a| is within the validity
29 // period of |b|. That is, |b| is the expiration criteria, and |a| is the
szym 2012/06/15 22:19:30 Maybe it'd be simpler to say: If |comp| is an inst
30 // current criteria, then it returns true if |a| is valid at this time.
31 template <typename KeyType, typename ValueType,
32 typename ExpirationType = base::TimeTicks,
33 typename ExpirationCompare = std::less<ExpirationType> >
24 class ExpiringCache { 34 class ExpiringCache {
25 private: 35 private:
26 // Intentionally violate the C++ Style Guide so that EntryMap is known to be 36 // 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 37 // a dependent type. Without this, Clang's two-phase lookup complains when
28 // using EntryMap::const_iterator, while GCC and MSVC happily resolve the 38 // using EntryMap::const_iterator, while GCC and MSVC happily resolve the
29 // typename. 39 // typename.
30 40
31 // Tuple to represent the value and when it expires. 41 // Tuple to represent the value and when it expires.
32 typedef std::pair<ValueType, base::TimeTicks> Entry; 42 typedef std::pair<ValueType, ExpirationType> Entry;
33 typedef std::map<KeyType, Entry> EntryMap; 43 typedef std::map<KeyType, Entry> EntryMap;
34 44
35 public: 45 public:
36 typedef KeyType key_type; 46 typedef KeyType key_type;
37 typedef ValueType value_type; 47 typedef ValueType value_type;
48 typedef ExpirationType expiration_type;
38 49
39 // This class provides a read-only iterator over items in the ExpiringCache 50 // This class provides a read-only iterator over items in the ExpiringCache
40 class Iterator { 51 class Iterator {
41 public: 52 public:
42 explicit Iterator(const ExpiringCache& cache) 53 explicit Iterator(const ExpiringCache& cache)
43 : cache_(cache), 54 : cache_(cache),
44 it_(cache_.entries_.begin()) { 55 it_(cache_.entries_.begin()) {
45 } 56 }
46 ~Iterator() {} 57 ~Iterator() {}
47 58
48 bool HasNext() const { return it_ != cache_.entries_.end(); } 59 bool HasNext() const { return it_ != cache_.entries_.end(); }
49 void Advance() { ++it_; } 60 void Advance() { ++it_; }
50 61
51 const KeyType& key() const { return it_->first; } 62 const KeyType& key() const { return it_->first; }
52 const ValueType& value() const { return it_->second.first; } 63 const ValueType& value() const { return it_->second.first; }
53 const base::TimeTicks& expiration() const { return it_->second.second; } 64 const ExpirationType& expiration() const { return it_->second.second; }
54 65
55 private: 66 private:
56 const ExpiringCache& cache_; 67 const ExpiringCache& cache_;
57 68
58 // Use a second layer of type indirection, as both EntryMap and 69 // Use a second layer of type indirection, as both EntryMap and
59 // EntryMap::const_iterator are dependent types. 70 // EntryMap::const_iterator are dependent types.
60 typedef typename ExpiringCache::EntryMap EntryMap; 71 typedef typename ExpiringCache::EntryMap EntryMap;
61 typename EntryMap::const_iterator it_; 72 typename EntryMap::const_iterator it_;
62 }; 73 };
63 74
64 75
65 // Constructs an ExpiringCache that stores up to |max_entries|. 76 // Constructs an ExpiringCache that stores up to |max_entries|, using
66 explicit ExpiringCache(size_t max_entries) : max_entries_(max_entries) {} 77 // |expiration_comp| to compare the expirations of elements.
szym 2012/06/15 22:19:30 suggest: "check if elements are expired".
78 explicit ExpiringCache(
79 size_t max_entries,
80 const ExpirationCompare& expiration_comp = ExpirationCompare())
81 : max_entries_(max_entries),
82 expiration_comp_(expiration_comp) {
83 }
84
67 ~ExpiringCache() {} 85 ~ExpiringCache() {}
68 86
69 // Returns the value matching |key|, which must be valid at the time |now|. 87 // 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 88 // Returns NULL if the item is not found or has expired. If the item has
71 // expired, it is immediately removed from the cache. 89 // expired, it is immediately removed from the cache.
72 // Note: The returned pointer remains owned by the ExpiringCache and is 90 // Note: The returned pointer remains owned by the ExpiringCache and is
73 // invalidated by a call to a non-const method. 91 // invalidated by a call to a non-const method.
74 const ValueType* Get(const KeyType& key, base::TimeTicks now) { 92 const ValueType* Get(const KeyType& key, const ExpirationType& now) {
75 typename EntryMap::iterator it = entries_.find(key); 93 typename EntryMap::iterator it = entries_.find(key);
76 if (it == entries_.end()) 94 if (it == entries_.end())
77 return NULL; 95 return NULL;
78 96
79 // Immediately remove expired entries. 97 // Immediately remove expired entries.
80 if (!CanUseEntry(it->second, now)) { 98 if (!expiration_comp_(now, it->second.second)) {
81 entries_.erase(it); 99 entries_.erase(it);
82 return NULL; 100 return NULL;
83 } 101 }
84 102
85 return &it->second.first; 103 return &it->second.first;
86 } 104 }
87 105
88 // Updates or replaces the value associated with |key|. 106 // Updates or replaces the value associated with |key|.
89 void Put(const KeyType& key, 107 void Put(const KeyType& key,
90 const ValueType& value, 108 const ValueType& value,
91 base::TimeTicks now, 109 const ExpirationType& now,
92 base::TimeDelta ttl) { 110 const ExpirationType& expiration) {
93 base::TimeTicks expiration = now + ttl;
94 typename EntryMap::iterator it = entries_.find(key); 111 typename EntryMap::iterator it = entries_.find(key);
95 if (it == entries_.end()) { 112 if (it == entries_.end()) {
96 // Compact the cache if it grew beyond the limit. 113 // Compact the cache if it grew beyond the limit.
97 if (entries_.size() == max_entries_ ) 114 if (entries_.size() == max_entries_ )
98 Compact(now); 115 Compact(now);
99 116
100 // No existing entry. Creating a new one. 117 // No existing entry. Creating a new one.
101 entries_.insert(std::make_pair(key, Entry(value, expiration))); 118 entries_.insert(std::make_pair(key, Entry(value, expiration)));
102 } else { 119 } else {
103 // Update an existing cache entry. 120 // Update an existing cache entry.
(...skipping 11 matching lines...) Expand all
115 size_t size() const { return entries_.size(); } 132 size_t size() const { return entries_.size(); }
116 133
117 // Returns the maximum number of entries in the cache. 134 // Returns the maximum number of entries in the cache.
118 size_t max_entries() const { return max_entries_; } 135 size_t max_entries() const { return max_entries_; }
119 136
120 bool empty() const { return entries_.empty(); } 137 bool empty() const { return entries_.empty(); }
121 138
122 private: 139 private:
123 FRIEND_TEST_ALL_PREFIXES(ExpiringCacheTest, Compact); 140 FRIEND_TEST_ALL_PREFIXES(ExpiringCacheTest, Compact);
124 141
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()|. 142 // Prunes entries from the cache to bring it below |max_entries()|.
131 void Compact(base::TimeTicks now) { 143 void Compact(const ExpirationType& now) {
132 // Clear out expired entries. 144 // Clear out expired entries.
133 typename EntryMap::iterator it; 145 typename EntryMap::iterator it;
134 for (it = entries_.begin(); it != entries_.end(); ) { 146 for (it = entries_.begin(); it != entries_.end(); ) {
135 if (!CanUseEntry(it->second, now)) { 147 if (!expiration_comp_(now, it->second.second)) {
136 entries_.erase(it++); 148 entries_.erase(it++);
137 } else { 149 } else {
138 ++it; 150 ++it;
139 } 151 }
140 } 152 }
141 153
142 if (entries_.size() < max_entries_) 154 if (entries_.size() < max_entries_)
143 return; 155 return;
144 156
145 // If the cache is still too full, start deleting items 'randomly'. 157 // If the cache is still too full, start deleting items 'randomly'.
146 for (it = entries_.begin(); 158 for (it = entries_.begin();
147 it != entries_.end() && entries_.size() >= max_entries_;) { 159 it != entries_.end() && entries_.size() >= max_entries_;) {
148 entries_.erase(it++); 160 entries_.erase(it++);
149 } 161 }
150 } 162 }
151 163
152 // Bound on total size of the cache. 164 // Bound on total size of the cache.
153 size_t max_entries_; 165 size_t max_entries_;
154 166
155 EntryMap entries_; 167 EntryMap entries_;
168 const ExpirationCompare expiration_comp_;
156 169
157 DISALLOW_COPY_AND_ASSIGN(ExpiringCache); 170 DISALLOW_COPY_AND_ASSIGN(ExpiringCache);
158 }; 171 };
159 172
160 } // namespace net 173 } // namespace net
161 174
162 #endif // NET_BASE_EXPIRING_CACHE_H_ 175 #endif // NET_BASE_EXPIRING_CACHE_H_
OLDNEW
« no previous file with comments | « no previous file | net/base/expiring_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698