libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
dnscache.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_NETWORK
13#include <cstdint>
14#include <promeki/namespace.h>
15#include <promeki/dnsrecord.h>
16#include <promeki/duration.h>
17#include <promeki/enums_dns.h>
18#include <promeki/list.h>
19#include <promeki/map.h>
20#include <promeki/mutex.h>
21#include <promeki/string.h>
22#include <promeki/timestamp.h>
23
24PROMEKI_NAMESPACE_BEGIN
25
52class DnsCache {
53 public:
55 static constexpr int64_t DefaultMaxTtlSeconds = 3600;
56
58 static constexpr int64_t DefaultMaxNegativeTtlSeconds = 300;
59
61 static constexpr size_t DefaultCapacity = 4096;
62
63 DnsCache();
64
66 void setMaxTtl(const Duration &d);
68 Duration maxTtl() const;
69
71 void setMaxNegativeTtl(const Duration &d);
73 Duration maxNegativeTtl() const;
74
76 void setCapacity(size_t cap);
77
79 size_t capacity() const;
80
82 size_t size() const;
83
85 void clear();
86
88 struct Hit {
90 bool found = false;
92 bool negative = false;
94 DnsRcode rcode;
96 List<DnsRecord> records;
97 };
98
107 Hit get(const String &name, uint16_t type, uint16_t klass) const;
108
130 void put(const String &name, uint16_t type, uint16_t klass,
131 const List<DnsRecord> &records,
132 const Duration &ttlCap = Duration::zero());
133
147 void putNegative(const String &name, uint16_t type, uint16_t klass,
148 const DnsRcode &rcode,
149 const Duration &ttlCap = Duration::zero());
150
151 private:
152 struct Entry {
153 TimeStamp expiresAt;
154 bool negative = false;
155 DnsRcode rcode = DnsRcode::NoError;
156 List<DnsRecord> records;
157 };
158
159 struct Key {
160 String name;
161 uint16_t type = 0;
162 uint16_t klass = 0;
163 bool operator<(const Key &o) const {
164 if (name < o.name) return true;
165 if (o.name < name) return false;
166 if (type < o.type) return true;
167 if (type > o.type) return false;
168 return klass < o.klass;
169 }
170 };
171
172 void evictIfOversized();
173
174 mutable Mutex _mtx;
175 Map<Key, Entry> _entries;
176 Duration _maxTtl;
177 Duration _maxNegTtl;
178 size_t _cap = DefaultCapacity;
179};
180
181PROMEKI_NAMESPACE_END
182
183#endif // PROMEKI_ENABLE_NETWORK