OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 { |
(...skipping 17 matching lines...) Expand all Loading... |
28 : max_entries_(max_entries), | 28 : max_entries_(max_entries), |
29 success_entry_ttl_(success_entry_ttl), | 29 success_entry_ttl_(success_entry_ttl), |
30 failure_entry_ttl_(failure_entry_ttl) { | 30 failure_entry_ttl_(failure_entry_ttl) { |
31 } | 31 } |
32 | 32 |
33 HostCache::~HostCache() { | 33 HostCache::~HostCache() { |
34 } | 34 } |
35 | 35 |
36 const HostCache::Entry* HostCache::Lookup(const Key& key, | 36 const HostCache::Entry* HostCache::Lookup(const Key& key, |
37 base::TimeTicks now) const { | 37 base::TimeTicks now) const { |
| 38 DCHECK(CalledOnValidThread()); |
38 if (caching_is_disabled()) | 39 if (caching_is_disabled()) |
39 return NULL; | 40 return NULL; |
40 | 41 |
41 EntryMap::const_iterator it = entries_.find(key); | 42 EntryMap::const_iterator it = entries_.find(key); |
42 if (it == entries_.end()) | 43 if (it == entries_.end()) |
43 return NULL; // Not found. | 44 return NULL; // Not found. |
44 | 45 |
45 Entry* entry = it->second.get(); | 46 Entry* entry = it->second.get(); |
46 if (CanUseEntry(entry, now)) | 47 if (CanUseEntry(entry, now)) |
47 return entry; | 48 return entry; |
48 | 49 |
49 return NULL; | 50 return NULL; |
50 } | 51 } |
51 | 52 |
52 HostCache::Entry* HostCache::Set(const Key& key, | 53 HostCache::Entry* HostCache::Set(const Key& key, |
53 int error, | 54 int error, |
54 const AddressList addrlist, | 55 const AddressList addrlist, |
55 base::TimeTicks now) { | 56 base::TimeTicks now) { |
| 57 DCHECK(CalledOnValidThread()); |
56 if (caching_is_disabled()) | 58 if (caching_is_disabled()) |
57 return NULL; | 59 return NULL; |
58 | 60 |
59 base::TimeTicks expiration = now + | 61 base::TimeTicks expiration = now + |
60 (error == OK ? success_entry_ttl_ : failure_entry_ttl_); | 62 (error == OK ? success_entry_ttl_ : failure_entry_ttl_); |
61 | 63 |
62 scoped_refptr<Entry>& entry = entries_[key]; | 64 scoped_refptr<Entry>& entry = entries_[key]; |
63 if (!entry) { | 65 if (!entry) { |
64 // Entry didn't exist, creating one now. | 66 // Entry didn't exist, creating one now. |
65 Entry* ptr = new Entry(error, addrlist, expiration); | 67 Entry* ptr = new Entry(error, addrlist, expiration); |
66 entry = ptr; | 68 entry = ptr; |
67 | 69 |
68 // Compact the cache if we grew it beyond limit -- exclude |entry| from | 70 // Compact the cache if we grew it beyond limit -- exclude |entry| from |
69 // being pruned though! | 71 // being pruned though! |
70 if (entries_.size() > max_entries_) | 72 if (entries_.size() > max_entries_) |
71 Compact(now, ptr); | 73 Compact(now, ptr); |
72 return ptr; | 74 return ptr; |
73 } else { | 75 } else { |
74 // Update an existing cache entry. | 76 // Update an existing cache entry. |
75 entry->error = error; | 77 entry->error = error; |
76 entry->addrlist = addrlist; | 78 entry->addrlist = addrlist; |
77 entry->expiration = expiration; | 79 entry->expiration = expiration; |
78 return entry.get(); | 80 return entry.get(); |
79 } | 81 } |
80 } | 82 } |
81 | 83 |
| 84 void HostCache::clear() { |
| 85 DCHECK(CalledOnValidThread()); |
| 86 entries_.clear(); |
| 87 } |
| 88 |
| 89 size_t HostCache::size() const { |
| 90 DCHECK(CalledOnValidThread()); |
| 91 return entries_.size(); |
| 92 } |
| 93 |
| 94 size_t HostCache::max_entries() const { |
| 95 DCHECK(CalledOnValidThread()); |
| 96 return max_entries_; |
| 97 } |
| 98 |
| 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. |
| 110 const HostCache::EntryMap& HostCache::entries() const { |
| 111 DCHECK(CalledOnValidThread()); |
| 112 return entries_; |
| 113 } |
| 114 |
82 // static | 115 // static |
83 bool HostCache::CanUseEntry(const Entry* entry, const base::TimeTicks now) { | 116 bool HostCache::CanUseEntry(const Entry* entry, const base::TimeTicks now) { |
84 return entry->expiration > now; | 117 return entry->expiration > now; |
85 } | 118 } |
86 | 119 |
87 void HostCache::Compact(base::TimeTicks now, const Entry* pinned_entry) { | 120 void HostCache::Compact(base::TimeTicks now, const Entry* pinned_entry) { |
88 // Clear out expired entries. | 121 // Clear out expired entries. |
89 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); ) { | 122 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); ) { |
90 Entry* entry = (it->second).get(); | 123 Entry* entry = (it->second).get(); |
91 if (entry != pinned_entry && !CanUseEntry(entry, now)) { | 124 if (entry != pinned_entry && !CanUseEntry(entry, now)) { |
(...skipping 18 matching lines...) Expand all Loading... |
110 } else { | 143 } else { |
111 ++it; | 144 ++it; |
112 } | 145 } |
113 } | 146 } |
114 | 147 |
115 if (entries_.size() > max_entries_) | 148 if (entries_.size() > max_entries_) |
116 DLOG(WARNING) << "Still above max entries limit"; | 149 DLOG(WARNING) << "Still above max entries limit"; |
117 } | 150 } |
118 | 151 |
119 } // namespace net | 152 } // namespace net |
OLD | NEW |