001/*
002 * Copyright 2015-2018 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;
015
016/**
017 * Cache for DNS Entries. Implementations must be thread safe.
018 */
019public abstract class DnsCache {
020
021    public static final int DEFAULT_CACHE_SIZE = 512;
022
023    /**
024     * Add an an dns answer/response for a given dns question. Implementations
025     * should honor the ttl / receive timestamp.
026     * @param query The query message containing a question.
027     * @param message The dns message.
028     */
029    public final void put(DnsMessage query, DnsMessage message) {
030        putNormalized(query.asNormalizedVersion(), message);
031    }
032
033    protected abstract void putNormalized(DnsMessage normalizedQuery, DnsMessage reply);
034
035    public abstract void offer(DnsMessage query, DnsMessage reply, DnsName authoritativeZone);
036
037    /**
038     * Request a cached dns response.
039     * @param query The query message containing a question.
040     * @return The dns message.
041     */
042    public final DnsMessage get(DnsMessage query) {
043        return getNormalized(query.asNormalizedVersion());
044    }
045
046    protected abstract DnsMessage getNormalized(DnsMessage normalizedQuery);
047
048}