Skip to content

Commit 3818202

Browse files
committed
add getDnsCachePolicy/getDnsNegativeCachePolicy methods; add synchronized when set cache policy. #25
1 parent 346c460 commit 3818202

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/main/java/com/alibaba/dcm/DnsCacheManipulator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,24 @@ public static void clearDnsCache() {
208208
}
209209
}
210210

211+
/**
212+
* Get JVM DNS cache policy.
213+
*
214+
* @return cache seconds.
215+
* <p/>
216+
* <ul>
217+
* <li> {@code -1} means never expired.(In effect, all negative value)</li>
218+
* <li> {@code 0} never cached.</li>
219+
* </ul>
220+
*/
221+
public static int getDnsCachePolicy() {
222+
try {
223+
return InetAddressCacheUtil.getDnsCachePolicy();
224+
} catch (Exception e) {
225+
throw new DnsCacheManipulatorException("Fail to getDnsCachePolicy, cause: " + e.toString(), e);
226+
}
227+
}
228+
211229
/**
212230
* Set JVM DNS cache policy
213231
*
@@ -226,6 +244,24 @@ public static void setDnsCachePolicy(int cacheSeconds) {
226244
}
227245
}
228246

247+
/**
248+
* JVM DNS negative cache policy
249+
*
250+
* @return negative cache seconds.
251+
* <p/>
252+
* <ul>
253+
* <li> {@code -1} means never expired.(In effect, all negative value)</li>
254+
* <li> {@code 0} never cached.</li>
255+
* </ul>
256+
*/
257+
public static int getDnsNegativeCachePolicy() {
258+
try {
259+
return InetAddressCacheUtil.getDnsNegativeCachePolicy();
260+
} catch (Exception e) {
261+
throw new DnsCacheManipulatorException("Fail to getDnsNegativeCachePolicy, cause: " + e.toString(), e);
262+
}
263+
}
264+
229265
/**
230266
* Set JVM DNS negative cache policy
231267
*

src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtil.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ public static void setDnsCachePolicy(int cacheSeconds)
297297
setCachePolicy0(false, cacheSeconds);
298298
}
299299

300+
public static int getDnsCachePolicy()
301+
throws NoSuchFieldException, IllegalAccessException {
302+
return InetAddressCachePolicy.get();
303+
}
304+
300305
/**
301306
* Set JVM DNS negative cache policy
302307
*
@@ -313,16 +318,25 @@ public static void setDnsNegativeCachePolicy(int negativeCacheSeconds)
313318
setCachePolicy0(true, negativeCacheSeconds);
314319
}
315320

321+
public static int getDnsNegativeCachePolicy()
322+
throws NoSuchFieldException, IllegalAccessException {
323+
return InetAddressCachePolicy.getNegative();
324+
}
325+
316326
static void setCachePolicy0(boolean isNegative, int seconds)
317327
throws NoSuchFieldException, IllegalAccessException {
318328
if (seconds < 0) {
319329
seconds = -1;
320330
}
321-
Class<?> clazz = sun.net.InetAddressCachePolicy.class;
331+
332+
Class<?> clazz = InetAddressCachePolicy.class;
322333
Field cachePolicyFiled = clazz.getDeclaredField(
323334
isNegative ? "negativeCachePolicy" : "cachePolicy");
324335
cachePolicyFiled.setAccessible(true);
325-
cachePolicyFiled.set(null, seconds);
336+
337+
synchronized (InetAddressCachePolicy.class) { // static synchronized method!
338+
cachePolicyFiled.set(null, seconds);
339+
}
326340
}
327341

328342
private InetAddressCacheUtil() {

src/test/java/com/alibaba/dcm/DnsCacheManipulatorTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public void test_nullSafeForGetDnsCache() throws Exception {
186186
public void test_setDnsCachePolicy() throws Exception {
187187
final String host = "baidu.com";
188188
DnsCacheManipulator.setDnsCachePolicy(2);
189+
assertEquals(2, DnsCacheManipulator.getDnsCachePolicy());
189190

190191
InetAddress.getByName(host).getHostAddress();
191192
final long tick = currentTimeMillis();
@@ -217,6 +218,7 @@ public void test_setDnsCachePolicy() throws Exception {
217218
@Test
218219
public void test_setNegativeDnsCachePolicy() throws Exception {
219220
DnsCacheManipulator.setDnsNegativeCachePolicy(2);
221+
assertEquals(2, DnsCacheManipulator.getDnsNegativeCachePolicy());
220222

221223
try {
222224
InetAddress.getByName(DOMAIN_NOT_EXISTED).getHostAddress();

0 commit comments

Comments
 (0)