| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.cache; |
| 12 |
|
| 13 |
import java.util.LinkedHashMap; |
| 14 |
import java.util.Map.Entry; |
| 15 |
|
| 16 |
import org.minidns.DnsCache; |
| 17 |
import org.minidns.dnsmessage.DnsMessage; |
| 18 |
import org.minidns.dnsname.DnsName; |
| 19 |
import org.minidns.dnsqueryresult.CachedDnsQueryResult; |
| 20 |
import org.minidns.dnsqueryresult.DirectCachedDnsQueryResult; |
| 21 |
import org.minidns.dnsqueryresult.DnsQueryResult; |
| 22 |
|
| 23 |
/** |
| 24 |
* LRU based DNSCache backed by a LinkedHashMap. |
| 25 |
*/ |
| 26 |
public class LruCache extends DnsCache { |
| 27 |
|
| 28 |
/** |
| 29 |
* Internal miss count. |
| 30 |
*/ |
| 31 |
protected long missCount = 0L; |
| 32 |
|
| 33 |
/** |
| 34 |
* Internal expire count (subset of misses that was caused by expire). |
| 35 |
*/ |
| 36 |
protected long expireCount = 0L; |
| 37 |
|
| 38 |
/** |
| 39 |
* Internal hit count. |
| 40 |
*/ |
| 41 |
protected long hitCount = 0L; |
| 42 |
|
| 43 |
/** |
| 44 |
* The internal capacity of the backend cache. |
| 45 |
*/ |
| 46 |
protected int capacity; |
| 47 |
|
| 48 |
/** |
| 49 |
* The upper bound of the ttl. All longer TTLs will be capped by this ttl. |
| 50 |
*/ |
| 51 |
protected long maxTTL; |
| 52 |
|
| 53 |
/** |
| 54 |
* The backend cache. |
| 55 |
*/ |
| 56 |
protected LinkedHashMap<DnsMessage, CachedDnsQueryResult> backend; |
| 57 |
|
| 58 |
/** |
| 59 |
* Create a new LRUCache with given capacity and upper bound ttl. |
| 60 |
* @param capacity The internal capacity. |
| 61 |
* @param maxTTL The upper bound for any ttl. |
| 62 |
*/ |
| 63 |
@SuppressWarnings("serial") |
| 64 |
public LruCache(final int capacity, final long maxTTL) { |
| 65 |
this.capacity = capacity; |
| 66 |
this.maxTTL = maxTTL; |
| 67 |
backend = new LinkedHashMap<DnsMessage, CachedDnsQueryResult>( |
| 68 |
Math.min(capacity + (capacity + 3) / 4 + 2, 11), 0.75f, true) { |
| 69 |
@Override |
| 70 |
protected boolean removeEldestEntry( |
| 71 |
Entry<DnsMessage, CachedDnsQueryResult> eldest) { |
| 72 |
return size() > capacity; |
| 73 |
} |
| 74 |
}; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Create a new LRUCache with given capacity. |
| 79 |
* @param capacity The capacity of this cache. |
| 80 |
*/ |
| 81 |
public LruCache(final int capacity) { |
| 82 |
this(capacity, Long.MAX_VALUE); |
| 83 |
} |
| 84 |
|
| 85 |
public LruCache() { |
| 86 |
this(DEFAULT_CACHE_SIZE); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
protected synchronized void putNormalized(DnsMessage q, DnsQueryResult result) { |
| 91 |
if (result.response.receiveTimestamp <= 0L) { |
| 92 |
return; |
| 93 |
} |
| 94 |
backend.put(q, new DirectCachedDnsQueryResult(q, result)); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
protected synchronized CachedDnsQueryResult getNormalized(DnsMessage q) { |
| 99 |
CachedDnsQueryResult result = backend.get(q); |
| 100 |
if (result == null) { |
| 101 |
missCount++; |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
DnsMessage message = result.response; |
| 106 |
|
| 107 |
// RFC 2181 ยง 5.2 says that all TTLs in a RRSet should be equal, if this isn't the case, then we assume the |
| 108 |
// shortest TTL to be the effective one. |
| 109 |
final long answersMinTtl = message.getAnswersMinTtl(); |
| 110 |
final long ttl = Math.min(answersMinTtl, maxTTL); |
| 111 |
|
| 112 |
final long expiryDate = message.receiveTimestamp + (ttl * 1000); |
| 113 |
final long now = System.currentTimeMillis(); |
| 114 |
if (expiryDate < now) { |
| 115 |
missCount++; |
| 116 |
expireCount++; |
| 117 |
backend.remove(q); |
| 118 |
return null; |
| 119 |
} else { |
| 120 |
hitCount++; |
| 121 |
return result; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Clear all entries in this cache. |
| 127 |
*/ |
| 128 |
public synchronized void clear() { |
| 129 |
backend.clear(); |
| 130 |
missCount = 0L; |
| 131 |
hitCount = 0L; |
| 132 |
expireCount = 0L; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get the miss count of this cache which is the number of fruitless |
| 137 |
* get calls since this cache was last resetted. |
| 138 |
* @return The number of cache misses. |
| 139 |
*/ |
| 140 |
public long getMissCount() { |
| 141 |
return missCount; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* The number of expires (cache hits that have had a ttl to low to be |
| 146 |
* retrieved). |
| 147 |
* @return The expire count. |
| 148 |
*/ |
| 149 |
public long getExpireCount() { |
| 150 |
return expireCount; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* The cache hit count (all successful calls to get). |
| 155 |
* @return The hit count. |
| 156 |
*/ |
| 157 |
public long getHitCount() { |
| 158 |
return hitCount; |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public String toString() { |
| 163 |
return "LRUCache{usage=" + backend.size() + "/" + capacity + ", hits=" + hitCount + ", misses=" + missCount + ", expires=" + expireCount + "}"; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void offer(DnsMessage query, DnsQueryResult result, DnsName knownAuthoritativeZone) { |
| 168 |
} |
| 169 |
} |