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    *   http://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.netty.resolver.dns;
17  
18  import io.netty.channel.EventLoop;
19  import io.netty.handler.codec.dns.DnsRecord;
20  import io.netty.util.internal.UnstableApi;
21  
22  import java.net.InetAddress;
23  import java.util.Collections;
24  import java.util.List;
25  
26  /**
27   * A noop DNS cache that actually never caches anything.
28   */
29  @UnstableApi
30  public final class NoopDnsCache implements DnsCache {
31  
32      public static final NoopDnsCache INSTANCE = new NoopDnsCache();
33  
34      /**
35       * Private singleton constructor.
36       */
37      private NoopDnsCache() {
38      }
39  
40      @Override
41      public void clear() {
42      }
43  
44      @Override
45      public boolean clear(String hostname) {
46          return false;
47      }
48  
49      @Override
50      public List<? extends DnsCacheEntry> get(String hostname, DnsRecord[] additionals) {
51          return Collections.emptyList();
52      }
53  
54      @Override
55      public DnsCacheEntry cache(String hostname, DnsRecord[] additional,
56                                 InetAddress address, long originalTtl, EventLoop loop) {
57          return new NoopDnsCacheEntry(address);
58      }
59  
60      @Override
61      public DnsCacheEntry cache(String hostname, DnsRecord[] additional, Throwable cause, EventLoop loop) {
62          return null;
63      }
64  
65      @Override
66      public String toString() {
67          return NoopDnsCache.class.getSimpleName();
68      }
69  
70      private static final class NoopDnsCacheEntry implements DnsCacheEntry {
71          private final InetAddress address;
72  
73          NoopDnsCacheEntry(InetAddress address) {
74              this.address = address;
75          }
76  
77          @Override
78          public InetAddress address() {
79              return address;
80          }
81  
82          @Override
83          public Throwable cause() {
84              return null;
85          }
86  
87          @Override
88          public String toString() {
89              return address.toString();
90          }
91      }
92  }