Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_DNS_HOST_CACHE_H_ | 5 #ifndef NET_DNS_HOST_CACHE_H_ |
| 6 #define NET_DNS_HOST_CACHE_H_ | 6 #define NET_DNS_HOST_CACHE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <functional> | 10 #include <functional> |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 return std::tie(address_family, host_resolver_flags, hostname) < | 57 return std::tie(address_family, host_resolver_flags, hostname) < |
| 58 std::tie(other.address_family, other.host_resolver_flags, | 58 std::tie(other.address_family, other.host_resolver_flags, |
| 59 other.hostname); | 59 other.hostname); |
| 60 } | 60 } |
| 61 | 61 |
| 62 std::string hostname; | 62 std::string hostname; |
| 63 AddressFamily address_family; | 63 AddressFamily address_family; |
| 64 HostResolverFlags host_resolver_flags; | 64 HostResolverFlags host_resolver_flags; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 struct EvictionHandler { | 67 struct NET_EXPORT EntryStaleness { |
| 68 void Handle(const Key& key, | 68 // Time since the entry's TTL has expired. Negative if not expired. |
| 69 const Entry& entry, | 69 base::TimeDelta expired_by; |
| 70 const base::TimeTicks& expiration, | 70 // Number of network changes since this result was cached. |
| 71 const base::TimeTicks& now, | 71 int network_changes; |
| 72 bool onGet) const; | 72 // Number of hits to the cache entry while stale (expired or past-network). |
| 73 int stale_hits; | |
| 74 | |
| 75 bool is_stale() const { | |
| 76 return network_changes > 0 || expired_by >= base::TimeDelta(); | |
| 77 } | |
| 73 }; | 78 }; |
| 74 | 79 |
| 75 typedef ExpiringCache<Key, Entry, base::TimeTicks, | 80 struct EntryInternal : public Entry { |
|
Ryan Sleevi
2016/04/29 23:09:38
Well, it's not really Internal now. Perhaps a comm
Julia Tuttle
2016/05/03 20:36:19
Done. This is *much* better, thank you.
| |
| 76 std::less<base::TimeTicks>, | 81 EntryInternal(const Entry& entry, |
| 77 EvictionHandler> EntryMap; | 82 base::TimeTicks now, |
| 83 base::TimeDelta ttl, | |
| 84 int network_changes); | |
| 85 | |
| 86 base::TimeTicks expires; | |
| 87 // Copied from HostCache::network_changes when this entry is set; can later | |
| 88 // be compared to it to see if the entry was received on the current | |
| 89 // network. | |
| 90 int network_changes; | |
| 91 int total_hits; | |
| 92 int stale_hits; | |
| 93 | |
| 94 bool IsStale(base::TimeTicks now, int network_changes) const; | |
| 95 void CountHit(bool hit_is_stale); | |
| 96 void GetStaleness(base::TimeTicks now, | |
| 97 int network_changes, | |
| 98 EntryStaleness* out) const; | |
| 99 }; | |
| 100 | |
| 101 // Ditto. | |
|
Ryan Sleevi
2016/04/29 23:09:38
Ditto what? This doesn't seem to need to be public
Julia Tuttle
2016/05/03 20:36:19
Done.
| |
| 102 typedef std::map<Key, EntryInternal> EntryMap; | |
| 78 | 103 |
| 79 // Constructs a HostCache that stores up to |max_entries|. | 104 // Constructs a HostCache that stores up to |max_entries|. |
| 80 explicit HostCache(size_t max_entries); | 105 explicit HostCache(size_t max_entries); |
| 81 | 106 |
| 82 ~HostCache(); | 107 ~HostCache(); |
| 83 | 108 |
| 84 // Returns a pointer to the entry for |key|, which is valid at time | 109 // Returns a pointer to the entry for |key|, which is valid at time |
| 85 // |now|. If there is no such entry, returns NULL. | 110 // |now|. If there is no such entry, returns NULL. |
| 86 const Entry* Lookup(const Key& key, base::TimeTicks now); | 111 const Entry* Lookup(const Key& key, base::TimeTicks now); |
| 87 | 112 |
| 113 // Returns a pointer to the entry for |key|, whether it is valid or stale at | |
| 114 // time |now|. Fills in |stale_out| with information about how stale it is. | |
| 115 // If there is no entry for |key| at all, returns NULL. | |
| 116 const Entry* LookupStale(const Key& key, | |
| 117 base::TimeTicks now, | |
| 118 EntryStaleness* stale_out); | |
| 119 | |
| 88 // Overwrites or creates an entry for |key|. | 120 // Overwrites or creates an entry for |key|. |
| 89 // |entry| is the value to set, |now| is the current time | 121 // |entry| is the value to set, |now| is the current time |
| 90 // |ttl| is the "time to live". | 122 // |ttl| is the "time to live". |
| 91 void Set(const Key& key, | 123 void Set(const Key& key, |
| 92 const Entry& entry, | 124 const Entry& entry, |
| 93 base::TimeTicks now, | 125 base::TimeTicks now, |
| 94 base::TimeDelta ttl); | 126 base::TimeDelta ttl); |
| 95 | 127 |
| 128 // Marks all entries as stale on account of a network change. | |
| 129 void OnNetworkChange(); | |
| 130 | |
| 96 // Empties the cache | 131 // Empties the cache |
| 97 void clear(); | 132 void clear(); |
| 98 | 133 |
| 99 // Returns the number of entries in the cache. | 134 // Returns the number of entries in the cache. |
| 100 size_t size() const; | 135 size_t size() const; |
| 101 | 136 |
| 102 // Following are used by net_internals UI. | 137 // Following are used by net_internals UI. |
| 103 size_t max_entries() const; | 138 size_t max_entries() const; |
| 104 | 139 |
| 105 const EntryMap& entries() const; | 140 std::unique_ptr<base::Value> GetEntriesAsValue() const; |
| 106 | 141 |
| 107 // Creates a default cache. | 142 // Creates a default cache. |
| 108 static std::unique_ptr<HostCache> CreateDefaultCache(); | 143 static std::unique_ptr<HostCache> CreateDefaultCache(); |
| 109 | 144 |
| 110 private: | 145 private: |
| 111 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); | 146 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); |
| 112 | 147 |
| 148 EntryInternal* LookupInternal(const Key& key); | |
| 149 | |
| 113 // Returns true if this HostCache can contain no entries. | 150 // Returns true if this HostCache can contain no entries. |
| 114 bool caching_is_disabled() const { | 151 bool caching_is_disabled() const { return max_entries_ == 0; } |
| 115 return entries_.max_entries() == 0; | 152 |
| 116 } | 153 void EvictOneEntry(base::TimeTicks now); |
| 117 | 154 |
| 118 // Map from hostname (presumably in lowercase canonicalized format) to | 155 // Map from hostname (presumably in lowercase canonicalized format) to |
| 119 // a resolved result entry. | 156 // a resolved result entry. |
| 120 EntryMap entries_; | 157 EntryMap entries_; |
| 158 size_t max_entries_; | |
| 159 int network_changes_; | |
| 121 | 160 |
| 122 DISALLOW_COPY_AND_ASSIGN(HostCache); | 161 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 123 }; | 162 }; |
| 124 | 163 |
| 125 } // namespace net | 164 } // namespace net |
| 126 | 165 |
| 127 #endif // NET_DNS_HOST_CACHE_H_ | 166 #endif // NET_DNS_HOST_CACHE_H_ |
| OLD | NEW |