OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_BASE_HOST_CACHE_H_ |
| 6 #define NET_BASE_HOST_CACHE_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/hash_tables.h" |
| 11 #include "base/ref_counted.h" |
| 12 #include "base/time.h" |
| 13 #include "net/base/address_list.h" |
| 14 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 // Cache used by HostResolver to map hostnames to their resolved result. |
| 19 // If the resolve is still in progress, the entry will reference the job |
| 20 // responsible for populating it. |
| 21 class HostCache { |
| 22 public: |
| 23 // Stores the latest address list that was looked up for a hostname. |
| 24 struct Entry : public base::RefCounted<Entry> { |
| 25 Entry(int error, const AddressList& addrlist, base::TimeTicks expiration); |
| 26 ~Entry(); |
| 27 |
| 28 // The resolve results for this entry. |
| 29 int error; |
| 30 AddressList addrlist; |
| 31 |
| 32 // The time when this entry expires. |
| 33 base::TimeTicks expiration; |
| 34 }; |
| 35 |
| 36 // Constructs a HostCache whose entries are valid for |cache_duration_ms| |
| 37 // milliseconds. The cache will store up to |max_entries|. |
| 38 HostCache(size_t max_entries, size_t cache_duration_ms); |
| 39 |
| 40 ~HostCache(); |
| 41 |
| 42 // Returns a pointer to the entry for |hostname|, which is valid at time |
| 43 // |now|. If there is no such entry, returns NULL. |
| 44 const Entry* Lookup(const std::string& hostname, base::TimeTicks now) const; |
| 45 |
| 46 // Overwrites or creates an entry for |hostname|. Returns the pointer to the |
| 47 // entry, or NULL on failure (fails if caching is disabled). |
| 48 // (|error|, |addrlist|) is the value to set, and |now| is the current |
| 49 // timestamp. |
| 50 Entry* Set(const std::string& hostname, |
| 51 int error, |
| 52 const AddressList addrlist, |
| 53 base::TimeTicks now); |
| 54 |
| 55 // Returns true if this HostCache can contain no entries. |
| 56 bool caching_is_disabled() const { |
| 57 return max_entries_ == 0; |
| 58 } |
| 59 |
| 60 // Returns the number of entries in the cache. |
| 61 size_t size() const { |
| 62 return entries_.size(); |
| 63 } |
| 64 |
| 65 private: |
| 66 FRIEND_TEST(HostCacheTest, Compact); |
| 67 FRIEND_TEST(HostCacheTest, NoCache); |
| 68 |
| 69 typedef base::hash_map<std::string, scoped_refptr<Entry> > EntryMap; |
| 70 |
| 71 // Returns true if this cache entry's result is valid at time |now|. |
| 72 static bool CanUseEntry(const Entry* entry, const base::TimeTicks now); |
| 73 |
| 74 // Prunes entries from the cache to bring it below max entry bound. Entries |
| 75 // matching |pinned_entry| will NOT be pruned. |
| 76 void Compact(base::TimeTicks now, const Entry* pinned_entry); |
| 77 |
| 78 // Bound on total size of the cache. |
| 79 size_t max_entries_; |
| 80 |
| 81 // Time to live for cache entries in milliseconds. |
| 82 size_t cache_duration_ms_; |
| 83 |
| 84 // Map from hostname (presumably in lowercase canonicalized format) to |
| 85 // a resolved result entry. |
| 86 EntryMap entries_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 89 }; |
| 90 |
| 91 } // namespace net |
| 92 |
| 93 #endif // NET_BASE_HOST_CACHE_H_ |
OLD | NEW |