Chromium Code Reviews| Index: net/dns/host_cache.cc |
| diff --git a/net/dns/host_cache.cc b/net/dns/host_cache.cc |
| index 84a6e80c6f38072d18d33fd8dd71492389d591d3..1b57c1d9fcb9f56f8f3406c5fd4b37a5e3e86416 100644 |
| --- a/net/dns/host_cache.cc |
| +++ b/net/dns/host_cache.cc |
| @@ -145,7 +145,7 @@ void HostCache::Entry::GetStaleness(base::TimeTicks now, |
| } |
| HostCache::HostCache(size_t max_entries) |
| - : max_entries_(max_entries), network_changes_(0) {} |
| + : max_entries_(max_entries), network_changes_(0), delegate_(nullptr) {} |
| HostCache::~HostCache() { |
| DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| @@ -210,21 +210,30 @@ void HostCache::Set(const Key& key, |
| if (caching_is_disabled()) |
| return; |
| + bool result_changed; |
|
mef
2017/06/20 16:18:12
nit: Maybe initialize to false?
nit: Maybe name it
mgersh
2017/06/20 17:16:29
Done.
|
| auto it = entries_.find(key); |
| if (it != entries_.end()) { |
| bool is_stale = it->second.IsStale(now, network_changes_); |
| + AddressListDeltaType delta = |
| + FindAddressListDeltaType(it->second.addresses(), entry.addresses()); |
| RecordSet(is_stale ? SET_UPDATE_STALE : SET_UPDATE_VALID, now, &it->second, |
| - entry); |
| + entry, delta); |
| // TODO(juliatuttle): Remember some old metadata (hit count or frequency or |
| // something like that) if it's useful for better eviction algorithms? |
| + result_changed = |
| + it->second.error() != entry.error() || delta != DELTA_IDENTICAL; |
|
mef
2017/06/20 16:18:12
Would we want to schedule write if new entry.error
mgersh
2017/06/20 17:16:29
I could go either way on this, but I guess the wri
|
| entries_.erase(it); |
| } else { |
| + result_changed = true; |
| if (size() == max_entries_) |
| EvictOneEntry(now); |
| - RecordSet(SET_INSERT, now, nullptr, entry); |
| + RecordSet(SET_INSERT, now, nullptr, entry, DELTA_DISJOINT); |
| } |
| AddEntry(Key(key), Entry(entry, now, ttl, network_changes_)); |
| + |
| + if (delegate_ && result_changed) |
| + delegate_->ScheduleWrite(); |
| } |
| void HostCache::AddEntry(const Key& key, const Entry& entry) { |
| @@ -241,7 +250,14 @@ void HostCache::OnNetworkChange() { |
| void HostCache::clear() { |
| DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); |
| + |
| + // Don't bother scheduling a write if there's nothing to clear. |
| + if (size() == 0) |
| + return; |
| + |
| entries_.clear(); |
| + if (delegate_) |
| + delegate_->ScheduleWrite(); |
| } |
| void HostCache::ClearForHosts( |
| @@ -253,6 +269,7 @@ void HostCache::ClearForHosts( |
| return; |
| } |
| + bool changed = false; |
| base::TimeTicks now = base::TimeTicks::Now(); |
| for (EntryMap::iterator it = entries_.begin(); it != entries_.end();) { |
| EntryMap::iterator next_it = std::next(it); |
| @@ -260,10 +277,14 @@ void HostCache::ClearForHosts( |
| if (host_filter.Run(it->first.hostname)) { |
| RecordErase(ERASE_CLEAR, now, it->second); |
| entries_.erase(it); |
| + changed = true; |
| } |
| it = next_it; |
| } |
| + |
| + if (delegate_ && changed) |
| + delegate_->ScheduleWrite(); |
| } |
| std::unique_ptr<base::ListValue> HostCache::GetAsListValue( |
| @@ -418,7 +439,8 @@ void HostCache::EvictOneEntry(base::TimeTicks now) { |
| void HostCache::RecordSet(SetOutcome outcome, |
| base::TimeTicks now, |
| const Entry* old_entry, |
| - const Entry& new_entry) { |
| + const Entry& new_entry, |
| + AddressListDeltaType delta) { |
| CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME); |
| switch (outcome) { |
| case SET_INSERT: |
| @@ -433,8 +455,6 @@ void HostCache::RecordSet(SetOutcome outcome, |
| stale.network_changes); |
| CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale.stale_hits); |
| if (old_entry->error() == OK && new_entry.error() == OK) { |
| - AddressListDeltaType delta = FindAddressListDeltaType( |
| - old_entry->addresses(), new_entry.addresses()); |
| RecordUpdateStale(delta, stale); |
| } |
| break; |