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
3 // found in the LICENSE file.
5 #include "net/dns/host_cache.h"
7 #include "base/logging.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/metrics/histogram.h"
10 #include "base/string_number_conversions.h"
11 #include "net/base/net_errors.h"
15 //-----------------------------------------------------------------------------
17 HostCache::Entry::Entry(int error
, const AddressList
& addrlist
,
22 DCHECK(ttl
>= base::TimeDelta());
25 HostCache::Entry::Entry(int error
, const AddressList
& addrlist
)
28 ttl(base::TimeDelta::FromSeconds(-1)) {
31 HostCache::Entry::~Entry() {
34 //-----------------------------------------------------------------------------
36 HostCache::HostCache(size_t max_entries
)
37 : entries_(max_entries
) {
40 HostCache::~HostCache() {
43 const HostCache::Entry
* HostCache::Lookup(const Key
& key
,
44 base::TimeTicks now
) {
45 DCHECK(CalledOnValidThread());
46 if (caching_is_disabled())
49 return entries_
.Get(key
, now
);
52 void HostCache::Set(const Key
& key
,
55 base::TimeDelta ttl
) {
56 DCHECK(CalledOnValidThread());
57 if (caching_is_disabled())
60 entries_
.Put(key
, entry
, now
, now
+ ttl
);
63 void HostCache::clear() {
64 DCHECK(CalledOnValidThread());
68 size_t HostCache::size() const {
69 DCHECK(CalledOnValidThread());
70 return entries_
.size();
73 size_t HostCache::max_entries() const {
74 DCHECK(CalledOnValidThread());
75 return entries_
.max_entries();
78 // Note that this map may contain expired entries.
79 const HostCache::EntryMap
& HostCache::entries() const {
80 DCHECK(CalledOnValidThread());
85 scoped_ptr
<HostCache
> HostCache::CreateDefaultCache() {
86 // Cache capacity is determined by the field trial.
87 #if defined(ENABLE_BUILT_IN_DNS)
88 const size_t kDefaultMaxEntries
= 1000;
90 const size_t kDefaultMaxEntries
= 100;
92 const size_t kSaneMaxEntries
= 1 << 20;
93 size_t max_entries
= 0;
94 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"),
96 if ((max_entries
== 0) || (max_entries
> kSaneMaxEntries
))
97 max_entries
= kDefaultMaxEntries
;
98 return make_scoped_ptr(new HostCache(max_entries
));
101 void HostCache::EvictionHandler::Handle(
104 const base::TimeTicks
& expiration
,
105 const base::TimeTicks
& now
,
108 DCHECK(now
>= expiration
);
109 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpiredOnGet", now
- expiration
,
110 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100);
113 if (expiration
> now
) {
114 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheEvicted", expiration
- now
,
115 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100);
117 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpired", now
- expiration
,
118 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100);