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

Side by Side Diff: net/base/host_cache.cc

Issue 9197009: Adds custom ttl argument to HostCache::Set. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed incomplete refactor. Created 8 years, 11 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "net/base/host_cache.h" 5 #include "net/base/host_cache.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "net/base/net_errors.h" 8 #include "net/base/net_errors.h"
9 9
10 namespace net { 10 namespace net {
11 11
12 //----------------------------------------------------------------------------- 12 //-----------------------------------------------------------------------------
13 13
14 HostCache::Entry::Entry(int error, 14 HostCache::Entry::Entry(int error,
15 const AddressList& addrlist, 15 const AddressList& addrlist,
16 base::TimeTicks expiration) 16 base::TimeTicks expiration)
17 : error(error), addrlist(addrlist), expiration(expiration) { 17 : error(error), addrlist(addrlist), expiration(expiration) {
18 } 18 }
19 19
20 HostCache::Entry::~Entry() { 20 HostCache::Entry::~Entry() {
21 } 21 }
22 22
23 //----------------------------------------------------------------------------- 23 //-----------------------------------------------------------------------------
24 24
25 HostCache::HostCache(size_t max_entries, 25 HostCache::HostCache(size_t max_entries)
26 base::TimeDelta success_entry_ttl, 26 : max_entries_(max_entries) {
27 base::TimeDelta failure_entry_ttl)
28 : max_entries_(max_entries),
29 success_entry_ttl_(success_entry_ttl),
30 failure_entry_ttl_(failure_entry_ttl) {
31 } 27 }
32 28
33 HostCache::~HostCache() { 29 HostCache::~HostCache() {
34 } 30 }
35 31
36 const HostCache::Entry* HostCache::Lookup(const Key& key, 32 const HostCache::Entry* HostCache::Lookup(const Key& key,
37 base::TimeTicks now) const { 33 base::TimeTicks now) const {
38 DCHECK(CalledOnValidThread()); 34 DCHECK(CalledOnValidThread());
39 if (caching_is_disabled()) 35 if (caching_is_disabled())
40 return NULL; 36 return NULL;
41 37
42 EntryMap::const_iterator it = entries_.find(key); 38 EntryMap::const_iterator it = entries_.find(key);
43 if (it == entries_.end()) 39 if (it == entries_.end())
44 return NULL; // Not found. 40 return NULL; // Not found.
45 41
46 Entry* entry = it->second.get(); 42 Entry* entry = it->second.get();
47 if (CanUseEntry(entry, now)) 43 if (CanUseEntry(entry, now))
48 return entry; 44 return entry;
49 45
50 return NULL; 46 return NULL;
51 } 47 }
52 48
53 HostCache::Entry* HostCache::Set(const Key& key, 49 HostCache::Entry* HostCache::Set(const Key& key,
54 int error, 50 int error,
55 const AddressList& addrlist, 51 const AddressList& addrlist,
56 base::TimeTicks now) { 52 base::TimeTicks now,
53 base::TimeDelta ttl) {
57 DCHECK(CalledOnValidThread()); 54 DCHECK(CalledOnValidThread());
58 if (caching_is_disabled()) 55 if (caching_is_disabled())
59 return NULL; 56 return NULL;
60 57
61 base::TimeTicks expiration = now + 58 base::TimeTicks expiration = now + ttl;
62 (error == OK ? success_entry_ttl_ : failure_entry_ttl_);
63 59
64 scoped_refptr<Entry>& entry = entries_[key]; 60 scoped_refptr<Entry>& entry = entries_[key];
65 if (!entry) { 61 if (!entry) {
66 // Entry didn't exist, creating one now. 62 // Entry didn't exist, creating one now.
67 Entry* ptr = new Entry(error, addrlist, expiration); 63 Entry* ptr = new Entry(error, addrlist, expiration);
68 entry = ptr; 64 entry = ptr;
69 65
70 // Compact the cache if we grew it beyond limit -- exclude |entry| from 66 // Compact the cache if we grew it beyond limit -- exclude |entry| from
71 // being pruned though! 67 // being pruned though!
72 if (entries_.size() > max_entries_) 68 if (entries_.size() > max_entries_)
(...skipping 16 matching lines...) Expand all
89 size_t HostCache::size() const { 85 size_t HostCache::size() const {
90 DCHECK(CalledOnValidThread()); 86 DCHECK(CalledOnValidThread());
91 return entries_.size(); 87 return entries_.size();
92 } 88 }
93 89
94 size_t HostCache::max_entries() const { 90 size_t HostCache::max_entries() const {
95 DCHECK(CalledOnValidThread()); 91 DCHECK(CalledOnValidThread());
96 return max_entries_; 92 return max_entries_;
97 } 93 }
98 94
99 base::TimeDelta HostCache::success_entry_ttl() const {
100 DCHECK(CalledOnValidThread());
101 return success_entry_ttl_;
102 }
103
104 base::TimeDelta HostCache::failure_entry_ttl() const {
105 DCHECK(CalledOnValidThread());
106 return failure_entry_ttl_;
107 }
108
109 // Note that this map may contain expired entries. 95 // Note that this map may contain expired entries.
110 const HostCache::EntryMap& HostCache::entries() const { 96 const HostCache::EntryMap& HostCache::entries() const {
111 DCHECK(CalledOnValidThread()); 97 DCHECK(CalledOnValidThread());
112 return entries_; 98 return entries_;
113 } 99 }
114 100
115 // static 101 // static
116 bool HostCache::CanUseEntry(const Entry* entry, const base::TimeTicks now) { 102 bool HostCache::CanUseEntry(const Entry* entry, const base::TimeTicks now) {
117 return entry->expiration > now; 103 return entry->expiration > now;
118 } 104 }
119 105
120 // static 106 // static
121 HostCache* HostCache::CreateDefaultCache() { 107 HostCache* HostCache::CreateDefaultCache() {
122 static const size_t kMaxHostCacheEntries = 100; 108 static const size_t kMaxHostCacheEntries = 100;
123 109 return new HostCache(kMaxHostCacheEntries);
124 HostCache* cache = new HostCache(
125 kMaxHostCacheEntries,
126 base::TimeDelta::FromMinutes(1),
127 base::TimeDelta::FromSeconds(0)); // Disable caching of failed DNS.
128
129 return cache;
130 } 110 }
131 111
132 void HostCache::Compact(base::TimeTicks now, const Entry* pinned_entry) { 112 void HostCache::Compact(base::TimeTicks now, const Entry* pinned_entry) {
133 // Clear out expired entries. 113 // Clear out expired entries.
134 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); ) { 114 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); ) {
135 Entry* entry = (it->second).get(); 115 Entry* entry = (it->second).get();
136 if (entry != pinned_entry && !CanUseEntry(entry, now)) { 116 if (entry != pinned_entry && !CanUseEntry(entry, now)) {
137 entries_.erase(it++); 117 entries_.erase(it++);
138 } else { 118 } else {
139 ++it; 119 ++it;
(...skipping 15 matching lines...) Expand all
155 } else { 135 } else {
156 ++it; 136 ++it;
157 } 137 }
158 } 138 }
159 139
160 if (entries_.size() > max_entries_) 140 if (entries_.size() > max_entries_)
161 DLOG(WARNING) << "Still above max entries limit"; 141 DLOG(WARNING) << "Still above max entries limit";
162 } 142 }
163 143
164 } // namespace net 144 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698