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..10adaf8beca973440ecdca1a90a77db1e5806508 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; |
| 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; |
| 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) { |
| @@ -242,6 +251,8 @@ void HostCache::clear() { |
| DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); |
| entries_.clear(); |
| + if (delegate_) |
|
Julia Tuttle
2017/06/16 17:04:54
Maybe actually check if the cache is already empty
mgersh
2017/06/16 17:32:49
Done.
|
| + delegate_->ScheduleWrite(); |
| } |
| void HostCache::ClearForHosts( |
| @@ -264,6 +275,9 @@ void HostCache::ClearForHosts( |
| it = next_it; |
| } |
| + |
| + if (delegate_) |
|
Julia Tuttle
2017/06/16 17:04:54
Ditto; see if we actually remove any entries.
mgersh
2017/06/16 17:32:49
Done.
|
| + delegate_->ScheduleWrite(); |
| } |
| std::unique_ptr<base::ListValue> HostCache::GetAsListValue( |
| @@ -418,7 +432,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 +448,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; |