Skip to content

Instantly share code, notes, and snippets.

@dehora
Created November 22, 2016 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dehora/ecaed7fd0af801a589c8e6c148c6425f to your computer and use it in GitHub Desktop.
Save dehora/ecaed7fd0af801a589c8e6c148c6425f to your computer and use it in GitHub Desktop.
package services
import java.security.Security
import org.slf4j.LoggerFactory
object DnsCache {
private val logger = LoggerFactory.getLogger("DnsCache")
/**
* Set the dns cache settings for the jvm to stop caching names forever
*/
def setup() {
val nct: String = "networkaddress.cache.ttl"
val ncnt: String = "networkaddress.cache.negative.ttl"
logger.info("cache_dns pre {}: {}", nct, Security.getProperty(nct))
logger.info("cache_dns pre {}: {}", ncnt, Security.getProperty(ncnt))
Security.setProperty(nct, System.getProperty("sun.net.inetaddr.ttl", "10"))
Security.setProperty(ncnt, System.getProperty("sun.net.inetaddr.negative.ttl", "3"))
logger.info("cache_dns post {}: {}", nct, Security.getProperty(nct))
logger.info("cache_dns post {}: {}", ncnt, Security.getProperty(ncnt))
}
}
@dehora
Copy link
Author

dehora commented Feb 21, 2017

for java

package outland.feature.server;

import java.security.Security;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class DnsCache {

  private static final Logger logger = LoggerFactory.getLogger(DnsCache.class);

  /**
   * Set the dns cache settings for the jvm on startup to stop caching names forever
   */
  static void setup() {
    change();
  }

  private static void change() {
    try {
      String nct = "networkaddress.cache.ttl";
      String ncnt = "networkaddress.cache.negative.ttl";
      logger.info("cache_dns pre {}: {}", nct, Security.getProperty(nct));
      logger.info("cache_dns pre {}: {}", ncnt, Security.getProperty(ncnt));
      Security.setProperty(nct, "3");
      Security.setProperty(ncnt, "3");
      logger.info("cache_dns post {}: {}", nct, Security.getProperty(nct));
      logger.info("cache_dns post {}: {}", ncnt, Security.getProperty(ncnt));
    } catch (SecurityException se) {
      throw new RuntimeException(se);
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment