| 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 #include "net/base/host_cache.h" | 5 #include "net/base/host_cache.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 | 11 |
| 12 //----------------------------------------------------------------------------- | 12 //----------------------------------------------------------------------------- |
| 13 | 13 |
| 14 HostCache::Entry::Entry(int error, const AddressList& addrlist, |
| 15 base::TimeDelta ttl) |
| 16 : error(error), |
| 17 addrlist(addrlist), |
| 18 ttl(ttl) { |
| 19 } |
| 20 |
| 14 HostCache::Entry::Entry(int error, const AddressList& addrlist) | 21 HostCache::Entry::Entry(int error, const AddressList& addrlist) |
| 15 : error(error), | 22 : error(error), |
| 16 addrlist(addrlist) { | 23 addrlist(addrlist), |
| 24 ttl(base::TimeDelta::FromSeconds(-1)) { |
| 17 } | 25 } |
| 18 | 26 |
| 19 HostCache::Entry::~Entry() { | 27 HostCache::Entry::~Entry() { |
| 20 } | 28 } |
| 21 | 29 |
| 22 //----------------------------------------------------------------------------- | 30 //----------------------------------------------------------------------------- |
| 23 | 31 |
| 24 HostCache::HostCache(size_t max_entries) | 32 HostCache::HostCache(size_t max_entries) |
| 25 : entries_(max_entries) { | 33 : entries_(max_entries) { |
| 26 } | 34 } |
| 27 | 35 |
| 28 HostCache::~HostCache() { | 36 HostCache::~HostCache() { |
| 29 } | 37 } |
| 30 | 38 |
| 31 const HostCache::Entry* HostCache::Lookup(const Key& key, | 39 const HostCache::Entry* HostCache::Lookup(const Key& key, |
| 32 base::TimeTicks now) { | 40 base::TimeTicks now) { |
| 33 DCHECK(CalledOnValidThread()); | 41 DCHECK(CalledOnValidThread()); |
| 34 if (caching_is_disabled()) | 42 if (caching_is_disabled()) |
| 35 return NULL; | 43 return NULL; |
| 36 | 44 |
| 37 return entries_.Get(key, now); | 45 return entries_.Get(key, now); |
| 38 } | 46 } |
| 39 | 47 |
| 40 void HostCache::Set(const Key& key, | 48 void HostCache::Set(const Key& key, |
| 41 int error, | 49 const Entry& entry, |
| 42 const AddressList& addrlist, | |
| 43 base::TimeTicks now, | 50 base::TimeTicks now, |
| 44 base::TimeDelta ttl) { | 51 base::TimeDelta ttl) { |
| 45 DCHECK(CalledOnValidThread()); | 52 DCHECK(CalledOnValidThread()); |
| 46 if (caching_is_disabled()) | 53 if (caching_is_disabled()) |
| 47 return; | 54 return; |
| 48 | 55 |
| 49 entries_.Put(key, Entry(error, addrlist), now, now + ttl); | 56 entries_.Put(key, entry, now, now + ttl); |
| 50 } | 57 } |
| 51 | 58 |
| 52 void HostCache::clear() { | 59 void HostCache::clear() { |
| 53 DCHECK(CalledOnValidThread()); | 60 DCHECK(CalledOnValidThread()); |
| 54 entries_.Clear(); | 61 entries_.Clear(); |
| 55 } | 62 } |
| 56 | 63 |
| 57 size_t HostCache::size() const { | 64 size_t HostCache::size() const { |
| 58 DCHECK(CalledOnValidThread()); | 65 DCHECK(CalledOnValidThread()); |
| 59 return entries_.size(); | 66 return entries_.size(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 77 // http://crbug.com/143454 | 84 // http://crbug.com/143454 |
| 78 // TODO(szym): Determine the best size. http://crbug.com/114277 | 85 // TODO(szym): Determine the best size. http://crbug.com/114277 |
| 79 static const size_t kMaxHostCacheEntries = 1000; | 86 static const size_t kMaxHostCacheEntries = 1000; |
| 80 #else | 87 #else |
| 81 static const size_t kMaxHostCacheEntries = 100; | 88 static const size_t kMaxHostCacheEntries = 100; |
| 82 #endif | 89 #endif |
| 83 return new HostCache(kMaxHostCacheEntries); | 90 return new HostCache(kMaxHostCacheEntries); |
| 84 } | 91 } |
| 85 | 92 |
| 86 } // namespace net | 93 } // namespace net |
| OLD | NEW |