1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SERVICES_PROXY_RESOLVER_PROXY_HOST_RESOLVER_CACHE_H_
6 #define SERVICES_PROXY_RESOLVER_PROXY_HOST_RESOLVER_CACHE_H_
14 #include "base/time/time.h"
15 #include "net/base/ip_address.h"
16 #include "net/base/network_anonymization_key.h"
18 namespace proxy_resolver {
20 // Simple cache for proxy host resolutions. Maintains cached entries for up to
21 // `kTtl`, and evicts oldest entries when filled past capacity.
22 class ProxyHostResolverCache {
24 static constexpr auto kTtl = base::Seconds(5);
26 explicit ProxyHostResolverCache(size_t max_entries = 500u);
27 ~ProxyHostResolverCache();
28 ProxyHostResolverCache(const ProxyHostResolverCache&) = delete;
29 ProxyHostResolverCache& operator=(const ProxyHostResolverCache&) = delete;
31 void StoreEntry(std::string hostname,
32 net::NetworkAnonymizationKey network_anonymization_key,
34 std::vector<net::IPAddress> results);
36 // Returns `nullptr` if entry not found or expired. Erases from cache if
38 const std::vector<net::IPAddress>* LookupEntry(
40 net::NetworkAnonymizationKey network_anonymization_key,
41 bool is_ex_operation);
43 size_t GetSizeForTesting() const;
47 bool operator<(const Key& other) const {
48 return std::tie(hostname, network_anonymization_key, is_ex_operation) <
49 std::tie(other.hostname, other.network_anonymization_key,
50 other.is_ex_operation);
54 net::NetworkAnonymizationKey network_anonymization_key;
58 using ExpirationList = std::list<const Key*>;
61 Entry(std::vector<net::IPAddress> results,
62 base::TimeTicks expiration,
63 ExpirationList::iterator expiration_list_it);
66 Entry& operator=(Entry&&);
68 std::vector<net::IPAddress> results;
69 base::TimeTicks expiration;
70 ExpirationList::iterator expiration_list_it;
73 using EntryMap = std::map<Key, Entry>;
75 // Removes oldest entry iff `max_entries_` exceeded.
76 void RemoveOldestEntry();
81 // List of `const Key*`s in expiration order starting from the earliest to
83 ExpirationList expiration_list_;
86 } // namespace proxy_resolver
88 #endif // SERVICES_PROXY_RESOLVER_PROXY_HOST_RESOLVER_CACHE_H_