| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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, | 14 HostCache::Entry::Entry(int error, |
| 15 const AddressList& addrlist, | 15 const AddressList& addrlist, |
| 16 base::TimeTicks expiration) | 16 base::TimeTicks expiration) |
| 17 : error(error), addrlist(addrlist), expiration(expiration) { | 17 : error(error), addrlist(addrlist), expiration(expiration) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 HostCache::Entry::~Entry() { | 20 HostCache::Entry::~Entry() { |
| 21 } | 21 } |
| 22 | 22 |
| 23 //----------------------------------------------------------------------------- | 23 //----------------------------------------------------------------------------- |
| 24 | 24 |
| 25 HostCache::HostCache(size_t max_entries, size_t cache_duration_ms) | 25 HostCache::HostCache(size_t max_entries, |
| 26 : max_entries_(max_entries), cache_duration_ms_(cache_duration_ms) { | 26 base::TimeDelta success_entry_ttl, |
| 27 base::TimeDelta failure_entry_ttl) |
| 28 : max_entries_(max_entries), |
| 29 success_entry_ttl_(success_entry_ttl), |
| 30 failure_entry_ttl_(failure_entry_ttl) { |
| 27 } | 31 } |
| 28 | 32 |
| 29 HostCache::~HostCache() { | 33 HostCache::~HostCache() { |
| 30 } | 34 } |
| 31 | 35 |
| 32 const HostCache::Entry* HostCache::Lookup(const Key& key, | 36 const HostCache::Entry* HostCache::Lookup(const Key& key, |
| 33 base::TimeTicks now) const { | 37 base::TimeTicks now) const { |
| 34 if (caching_is_disabled()) | 38 if (caching_is_disabled()) |
| 35 return NULL; | 39 return NULL; |
| 36 | 40 |
| 37 EntryMap::const_iterator it = entries_.find(key); | 41 EntryMap::const_iterator it = entries_.find(key); |
| 38 if (it == entries_.end()) | 42 if (it == entries_.end()) |
| 39 return NULL; // Not found. | 43 return NULL; // Not found. |
| 40 | 44 |
| 41 Entry* entry = it->second.get(); | 45 Entry* entry = it->second.get(); |
| 42 if (CanUseEntry(entry, now)) | 46 if (CanUseEntry(entry, now)) |
| 43 return entry; | 47 return entry; |
| 44 | 48 |
| 45 return NULL; | 49 return NULL; |
| 46 } | 50 } |
| 47 | 51 |
| 48 HostCache::Entry* HostCache::Set(const Key& key, | 52 HostCache::Entry* HostCache::Set(const Key& key, |
| 49 int error, | 53 int error, |
| 50 const AddressList addrlist, | 54 const AddressList addrlist, |
| 51 base::TimeTicks now) { | 55 base::TimeTicks now) { |
| 52 if (caching_is_disabled()) | 56 if (caching_is_disabled()) |
| 53 return NULL; | 57 return NULL; |
| 54 | 58 |
| 55 base::TimeTicks expiration = now + | 59 base::TimeTicks expiration = now + |
| 56 base::TimeDelta::FromMilliseconds(cache_duration_ms_); | 60 (error == OK ? success_entry_ttl_ : failure_entry_ttl_); |
| 57 | 61 |
| 58 scoped_refptr<Entry>& entry = entries_[key]; | 62 scoped_refptr<Entry>& entry = entries_[key]; |
| 59 if (!entry) { | 63 if (!entry) { |
| 60 // Entry didn't exist, creating one now. | 64 // Entry didn't exist, creating one now. |
| 61 Entry* ptr = new Entry(error, addrlist, expiration); | 65 Entry* ptr = new Entry(error, addrlist, expiration); |
| 62 entry = ptr; | 66 entry = ptr; |
| 63 | 67 |
| 64 // Compact the cache if we grew it beyond limit -- exclude |entry| from | 68 // Compact the cache if we grew it beyond limit -- exclude |entry| from |
| 65 // being pruned though! | 69 // being pruned though! |
| 66 if (entries_.size() > max_entries_) | 70 if (entries_.size() > max_entries_) |
| 67 Compact(now, ptr); | 71 Compact(now, ptr); |
| 68 return ptr; | 72 return ptr; |
| 69 } else { | 73 } else { |
| 70 // Update an existing cache entry. | 74 // Update an existing cache entry. |
| 71 entry->error = error; | 75 entry->error = error; |
| 72 entry->addrlist = addrlist; | 76 entry->addrlist = addrlist; |
| 73 entry->expiration = expiration; | 77 entry->expiration = expiration; |
| 74 return entry.get(); | 78 return entry.get(); |
| 75 } | 79 } |
| 76 } | 80 } |
| 77 | 81 |
| 78 // static | 82 // static |
| 79 bool HostCache::CanUseEntry(const Entry* entry, const base::TimeTicks now) { | 83 bool HostCache::CanUseEntry(const Entry* entry, const base::TimeTicks now) { |
| 80 return entry->error == OK && entry->expiration > now; | 84 return entry->expiration > now; |
| 81 } | 85 } |
| 82 | 86 |
| 83 void HostCache::Compact(base::TimeTicks now, const Entry* pinned_entry) { | 87 void HostCache::Compact(base::TimeTicks now, const Entry* pinned_entry) { |
| 84 // Clear out expired entries. | 88 // Clear out expired entries. |
| 85 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); ) { | 89 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); ) { |
| 86 Entry* entry = (it->second).get(); | 90 Entry* entry = (it->second).get(); |
| 87 if (entry != pinned_entry && !CanUseEntry(entry, now)) { | 91 if (entry != pinned_entry && !CanUseEntry(entry, now)) { |
| 88 entries_.erase(it++); | 92 entries_.erase(it++); |
| 89 } else { | 93 } else { |
| 90 ++it; | 94 ++it; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 106 } else { | 110 } else { |
| 107 ++it; | 111 ++it; |
| 108 } | 112 } |
| 109 } | 113 } |
| 110 | 114 |
| 111 if (entries_.size() > max_entries_) | 115 if (entries_.size() > max_entries_) |
| 112 DLOG(WARNING) << "Still above max entries limit"; | 116 DLOG(WARNING) << "Still above max entries limit"; |
| 113 } | 117 } |
| 114 | 118 |
| 115 } // namespace net | 119 } // namespace net |
| OLD | NEW |