eve.aio.dns_cache

DNS cache with TTL-based expiration.

Caches successful and negative DNS results with configurable TTL bounds. Designed to wrap DnsResolver — check the cache before calling resolve(), and store results on completion.

Thread safety: cache access must happen on the event loop thread only. For cross-thread access, wrap calls with external synchronization.

Example:

auto cache = DnsCache.create();
scope (exit) cache.dispose();

// Before resolving:
auto cached = cache.lookup("example.com");
if (cached.length > 0) {
    // use cached addresses
} else {
    resolver.resolve("example.com", (ref l, t, addrs, err) @safe nothrow {
        if (err == DnsError.OK) {
            cache.store("example.com", addrs, 300); // TTL 300s
        } else if (err == DnsError.NONAME) {
            cache.storeNegative("example.com", 60); // negative TTL 60s
        }
    });
}

Types 3

Configuration for a DnsCache.

Fields
size_t maxEntriesMaximum number of entries in the cache.
uint defaultTtlDefault TTL in seconds for successful results.
uint minTtlMinimum TTL in seconds.
uint maxTtlMaximum TTL in seconds.
uint negativeTtlTTL in seconds for negative results (NXDOMAIN, non-recoverable errors).

A cached DNS entry.

Fields
IpAddress[] addressesResolved IP addresses (empty for negative entries).
DnsError errorError code for negative cache entries (`DnsError.OK` for positive).
long expiresAtMsMonotonic time (ms) when this entry expires.
Methods
bool isNegative() @property const pure @safe nothrow @nogcWhether this is a negative cache entry.
bool isExpired(long nowMs) const pure @safe nothrow @nogcWhether this entry has expired relative to the given time.
structDnsCache

TTL-based DNS cache for frequently resolved hostnames.

Stores both positive results (IP addresses) and negative results (NXDOMAIN / non-recoverable errors) with separate TTL policies. Entries are pruned on lookup and during periodic eviction.

Not thread-safe by default. All access must occur on the event loop thread, or callers must provide external synchronization.

Fields
DnsCacheEntry[string] _cache
Mutex _mutex
bool _initialized
Methods
long currentMonotonicMs() static @trusted nothrow
uint clampTtl(uint ttl) const pure @safe nothrow @nogc
DnsCache create(DnsCacheConfig config = DnsCacheConfig.init) static @trustedCreate a DNS cache with the given configuration.
void dispose() @trustedRelease cache resources.
IpAddress[] lookup(scope const(char)[] hostname) @trustedLook up a hostname in the cache.
const(DnsCacheEntry) * lookupEntry(scope const(char)[] hostname) @trustedLook up a hostname and return the cache entry (including error).
void store(scope const(char)[] hostname, scope const(IpAddress)[] addresses, uint ttlSeconds = 0) @trustedStore a successful DNS result in the cache.
void storeNegative(scope const(char)[] hostname, uint ttlSeconds = 0, DnsError error = DnsError.NONAME) @trustedStore a negative (error) result in the cache.
void remove(scope const(char)[] hostname) @trustedRemove a specific hostname from the cache.
size_t evictExpired() @trustedRemove all expired entries from the cache.
void clear() @trustedClear all entries from the cache.
size_t length() @property const @safe nothrowNumber of entries currently in the cache (including expired entries not yet pruned).
bool initialized() @property const pure @safe nothrow @nogcWhether the cache has been initialized.
void evictOne() @trusted