001/*
002 * Copyright 2015-2024 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns;
012
013import org.minidns.dnsmessage.DnsMessage;
014import org.minidns.dnsname.DnsName;
015import org.minidns.dnsqueryresult.CachedDnsQueryResult;
016import org.minidns.dnsqueryresult.DnsQueryResult;
017
018/**
019 * Cache for DNS Entries. Implementations must be thread safe.
020 */
021public abstract class DnsCache {
022
023    public static final int DEFAULT_CACHE_SIZE = 512;
024
025    /**
026     * Add an an dns answer/response for a given dns question. Implementations
027     * should honor the ttl / receive timestamp.
028     * @param query The query message containing a question.
029     * @param result The DNS query result.
030     */
031    public final void put(DnsMessage query, DnsQueryResult result) {
032        putNormalized(query.asNormalizedVersion(), result);
033    }
034
035    protected abstract void putNormalized(DnsMessage normalizedQuery, DnsQueryResult result);
036
037    public abstract void offer(DnsMessage query, DnsQueryResult result, DnsName authoritativeZone);
038
039    /**
040     * Request a cached dns response.
041     * @param query The query message containing a question.
042     * @return The dns message.
043     */
044    public final CachedDnsQueryResult get(DnsMessage query) {
045        return getNormalized(query.asNormalizedVersion());
046    }
047
048    protected abstract CachedDnsQueryResult getNormalized(DnsMessage normalizedQuery);
049
050}