Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #ifndef NET_BASE_HOST_CACHE_H_ | 5 #ifndef NET_BASE_HOST_CACHE_H_ |
| 6 #define NET_BASE_HOST_CACHE_H_ | 6 #define NET_BASE_HOST_CACHE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 return hostname < other.hostname; | 66 return hostname < other.hostname; |
| 67 } | 67 } |
| 68 | 68 |
| 69 std::string hostname; | 69 std::string hostname; |
| 70 AddressFamily address_family; | 70 AddressFamily address_family; |
| 71 HostResolverFlags host_resolver_flags; | 71 HostResolverFlags host_resolver_flags; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 typedef std::map<Key, scoped_refptr<Entry> > EntryMap; | 74 typedef std::map<Key, scoped_refptr<Entry> > EntryMap; |
| 75 | 75 |
| 76 // Constructs a HostCache that caches successful host resolves for | 76 // Constructs a HostCache that stores up to |max_entries|. |
| 77 // |success_entry_ttl| time, and failed host resolves for | 77 HostCache(size_t max_entries); |
| 78 // |failure_entry_ttl|. The cache will store up to |max_entries|. | |
| 79 HostCache(size_t max_entries, | |
| 80 base::TimeDelta success_entry_ttl, | |
| 81 base::TimeDelta failure_entry_ttl); | |
| 82 | 78 |
| 83 ~HostCache(); | 79 ~HostCache(); |
| 84 | 80 |
| 85 // Returns a pointer to the entry for |key|, which is valid at time | 81 // Returns a pointer to the entry for |key|, which is valid at time |
| 86 // |now|. If there is no such entry, returns NULL. | 82 // |now|. If there is no such entry, returns NULL. |
| 87 const Entry* Lookup(const Key& key, base::TimeTicks now) const; | 83 const Entry* Lookup(const Key& key, base::TimeTicks now) const; |
| 88 | 84 |
| 89 // Overwrites or creates an entry for |key|. Returns the pointer to the | 85 // Overwrites or creates an entry for |key|. Returns the pointer to the |
| 90 // entry, or NULL on failure (fails if caching is disabled). | 86 // entry, or NULL on failure (fails if caching is disabled). |
| 91 // (|error|, |addrlist|) is the value to set, and |now| is the current | 87 // (|error|, |addrlist|) is the value to set, |ttl| is the "time to live", |
| 92 // timestamp. | 88 // and |now| is the current timestamp. |
| 93 Entry* Set(const Key& key, | 89 Entry* Set(const Key& key, |
| 94 int error, | 90 int error, |
| 95 const AddressList& addrlist, | 91 const AddressList& addrlist, |
| 92 base::TimeDelta ttl, | |
| 96 base::TimeTicks now); | 93 base::TimeTicks now); |
|
mmenke
2012/01/19 22:12:43
nit: Cognitively, I think putting |now| before |t
| |
| 97 | 94 |
| 98 // Empties the cache | 95 // Empties the cache |
| 99 void clear(); | 96 void clear(); |
| 100 | 97 |
| 101 // Returns the number of entries in the cache. | 98 // Returns the number of entries in the cache. |
| 102 size_t size() const; | 99 size_t size() const; |
| 103 | 100 |
| 104 // Following are used by net_internals UI. | 101 // Following are used by net_internals UI. |
| 105 size_t max_entries() const; | 102 size_t max_entries() const; |
| 106 | 103 |
| 107 base::TimeDelta success_entry_ttl() const; | |
| 108 | |
| 109 base::TimeDelta failure_entry_ttl() const; | |
| 110 | |
| 111 // Note that this map may contain expired entries. | 104 // Note that this map may contain expired entries. |
| 112 const EntryMap& entries() const; | 105 const EntryMap& entries() const; |
| 113 | 106 |
| 114 // Creates a default cache. | 107 // Creates a default cache. |
| 115 static HostCache* CreateDefaultCache(); | 108 static HostCache* CreateDefaultCache(); |
| 116 | 109 |
| 117 private: | 110 private: |
| 118 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, Compact); | 111 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, Compact); |
| 119 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); | 112 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); |
| 120 | 113 |
| 121 // Returns true if this cache entry's result is valid at time |now|. | 114 // Returns true if this cache entry's result is valid at time |now|. |
| 122 static bool CanUseEntry(const Entry* entry, const base::TimeTicks now); | 115 static bool CanUseEntry(const Entry* entry, const base::TimeTicks now); |
| 123 | 116 |
| 124 // Prunes entries from the cache to bring it below max entry bound. Entries | 117 // Prunes entries from the cache to bring it below max entry bound. Entries |
| 125 // matching |pinned_entry| will NOT be pruned. | 118 // matching |pinned_entry| will NOT be pruned. |
| 126 void Compact(base::TimeTicks now, const Entry* pinned_entry); | 119 void Compact(base::TimeTicks now, const Entry* pinned_entry); |
| 127 | 120 |
| 128 // Returns true if this HostCache can contain no entries. | 121 // Returns true if this HostCache can contain no entries. |
| 129 bool caching_is_disabled() const { | 122 bool caching_is_disabled() const { |
| 130 return max_entries_ == 0; | 123 return max_entries_ == 0; |
| 131 } | 124 } |
| 132 | 125 |
| 133 // Bound on total size of the cache. | 126 // Bound on total size of the cache. |
| 134 size_t max_entries_; | 127 size_t max_entries_; |
| 135 | 128 |
| 136 // Time to live for cache entries. | |
| 137 base::TimeDelta success_entry_ttl_; | |
| 138 base::TimeDelta failure_entry_ttl_; | |
| 139 | |
| 140 // Map from hostname (presumably in lowercase canonicalized format) to | 129 // Map from hostname (presumably in lowercase canonicalized format) to |
| 141 // a resolved result entry. | 130 // a resolved result entry. |
| 142 EntryMap entries_; | 131 EntryMap entries_; |
| 143 | 132 |
| 144 DISALLOW_COPY_AND_ASSIGN(HostCache); | 133 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 145 }; | 134 }; |
| 146 | 135 |
| 147 } // namespace net | 136 } // namespace net |
| 148 | 137 |
| 149 #endif // NET_BASE_HOST_CACHE_H_ | 138 #endif // NET_BASE_HOST_CACHE_H_ |
| OLD | NEW |