View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.resolver.dns;
17  
18  import io.netty5.channel.EventLoop;
19  import io.netty5.handler.codec.dns.DnsRecord;
20  
21  import java.net.InetAddress;
22  import java.util.Collections;
23  import java.util.List;
24  
25  /**
26   * A noop DNS cache that actually never caches anything.
27   */
28  public final class NoopDnsCache implements DnsCache {
29  
30      public static final NoopDnsCache INSTANCE = new NoopDnsCache();
31  
32      /**
33       * Private singleton constructor.
34       */
35      private NoopDnsCache() {
36      }
37  
38      @Override
39      public void clear() {
40      }
41  
42      @Override
43      public boolean clear(String hostname) {
44          return false;
45      }
46  
47      @Override
48      public List<? extends DnsCacheEntry> get(String hostname, DnsRecord[] additionals) {
49          return Collections.emptyList();
50      }
51  
52      @Override
53      public DnsCacheEntry cache(String hostname, DnsRecord[] additional,
54                                 InetAddress address, long originalTtl, EventLoop loop) {
55          return new NoopDnsCacheEntry(address);
56      }
57  
58      @Override
59      public DnsCacheEntry cache(String hostname, DnsRecord[] additional, Throwable cause, EventLoop loop) {
60          return null;
61      }
62  
63      @Override
64      public String toString() {
65          return NoopDnsCache.class.getSimpleName();
66      }
67  
68      private static final class NoopDnsCacheEntry implements DnsCacheEntry {
69          private final InetAddress address;
70  
71          NoopDnsCacheEntry(InetAddress address) {
72              this.address = address;
73          }
74  
75          @Override
76          public InetAddress address() {
77              return address;
78          }
79  
80          @Override
81          public Throwable cause() {
82              return null;
83          }
84  
85          @Override
86          public String toString() {
87              return address.toString();
88          }
89      }
90  }