summaryrefslogtreecommitdiffstats
path: root/net/base/host_cache.h
blob: 538f7ef1eae18d78874e35bf8dab37f4c5442db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_HOST_CACHE_H_
#define NET_BASE_HOST_CACHE_H_

#include <map>
#include <string>

#include "base/ref_counted.h"
#include "base/time.h"
#include "net/base/address_family.h"
#include "net/base/address_list.h"
#include "testing/gtest/include/gtest/gtest_prod.h"

namespace net {

// Cache used by HostResolver to map hostnames to their resolved result.
class HostCache {
 public:
  // Stores the latest address list that was looked up for a hostname.
  struct Entry : public base::RefCounted<Entry> {
    Entry(int error, const AddressList& addrlist, base::TimeTicks expiration);
    ~Entry();

    // The resolve results for this entry.
    int error;
    AddressList addrlist;

    // The time when this entry expires.
    base::TimeTicks expiration;
  };

  struct Key {
    Key(const std::string& hostname, AddressFamily address_family)
        : hostname(hostname), address_family(address_family) {}

    bool operator==(const Key& other) const {
      return other.hostname == hostname &&
             other.address_family == address_family;
    }

    bool operator<(const Key& other) const {
      if (address_family < other.address_family)
        return true;
      return hostname < other.hostname;
    }

    std::string hostname;
    AddressFamily address_family;
  };

  typedef std::map<Key, scoped_refptr<Entry> > EntryMap;

  // Constructs a HostCache whose entries are valid for |cache_duration_ms|
  // milliseconds. The cache will store up to |max_entries|.
  HostCache(size_t max_entries, size_t cache_duration_ms);

  ~HostCache();

  // Returns a pointer to the entry for |key|, which is valid at time
  // |now|. If there is no such entry, returns NULL.
  const Entry* Lookup(const Key& key, base::TimeTicks now) const;

  // Overwrites or creates an entry for |key|. Returns the pointer to the
  // entry, or NULL on failure (fails if caching is disabled).
  // (|error|, |addrlist|) is the value to set, and |now| is the current
  // timestamp.
  Entry* Set(const Key& key,
             int error,
             const AddressList addrlist,
             base::TimeTicks now);

  // Returns true if this HostCache can contain no entries.
  bool caching_is_disabled() const {
    return max_entries_ == 0;
  }

  // Returns the number of entries in the cache.
  size_t size() const {
    return entries_.size();
  }

  size_t max_entries() const {
    return max_entries_;
  }

  size_t cache_duration_ms() const {
    return cache_duration_ms_;
  }

  // Note that this map may contain expired entries.
  const EntryMap& entries() const {
    return entries_;
  }

 private:
  FRIEND_TEST(HostCacheTest, Compact);
  FRIEND_TEST(HostCacheTest, NoCache);

  // Returns true if this cache entry's result is valid at time |now|.
  static bool CanUseEntry(const Entry* entry, const base::TimeTicks now);

  // Prunes entries from the cache to bring it below max entry bound. Entries
  // matching |pinned_entry| will NOT be pruned.
  void Compact(base::TimeTicks now, const Entry* pinned_entry);

  // Bound on total size of the cache.
  size_t max_entries_;

  // Time to live for cache entries in milliseconds.
  size_t cache_duration_ms_;

  // Map from hostname (presumably in lowercase canonicalized format) to
  // a resolved result entry.
  EntryMap entries_;

  DISALLOW_COPY_AND_ASSIGN(HostCache);
};

}  // namespace net

#endif  // NET_BASE_HOST_CACHE_H_