How to configure HTTPDNS to reduce resolution frequency

更新时间:
复制 MD 格式

You can adjust the cache policy to reduce the frequency of HTTPDNS resolutions and lower overhead. The configuration steps are as follows:

1. Enable and configure the local cache:

  • When you initialize the HTTPDNS service, enable the local cache feature by calling setEnableCacheIp(true). This allows the client to store the IP addresses of recently resolved domain names in memory and avoid duplicate requests.

  • Example code:

InitConfig.Builder()
    .setEnableCacheIp(true)
new InitConfig.Builder()
    .setEnableCacheIp(true);
Note

This method was added in version 2.2.1. When this method is called, it loads records from the local persistence cache into the memory cache and purges any expired local cache records.

2. Customize the cache TTL:

  • You can use the configCacheTtlChanger(ttlChanger) method to customize the cache TTL. A reasonable TTL value helps control the cache update frequency and reduces unnecessary DNS queries.

  • Example code:

InitConfig.Builder().configCacheTtlChanger { host, requestIpType, ttl ->
    if (TextUtils.equals(host, "${YOUR_DOMAIN_NAME}")) {
        ttl * 10
    } else ttl
}
new InitConfig.Builder().configCacheTtlChanger(new CacheTtlChanger() {
    @Override
    public int changeCacheTtl(String host, RequestIpType requestIpType, int ttl) {
        if (TextUtils.equals(host,"${YOUR_DOMAIN_NAME}")) {
            return ttl * 10;
        }
        return ttl;
    }
});
Note

This method was added in version 2.3.0.

3. Pre-resolve hot spot domain names:

  • For frequently accessed domain names, you can pre-resolve them during application startup and save the results to the cache. This allows subsequent requests to use the cached data directly without initiating another DNS query.

  • Example code:

 var preResolveList: MutableList<String> = mutableListOf()
 preResolveList.add("${YOUR_DOMAIN_NAME}")
 val httpdns = httpdns=HttpDns.getService(context,ACCOUNT_ID, SECRET_KEY)
 httpdns?.setPreResolveHosts(preResolveList)
HttpDnsService httpdns = HttpDns.getService(context, ACCOUNT_ID, SECRET_KEY);
ArrayList<String> preResolveHosts = new ArrayList<>();
preResolveHosts.add("${YOUR_DOMAIN_NAME}");
httpdns.setPreResolveHosts(preResolveHosts);

4. Configure a list of domain names with fixed IP addresses:

  • If the IP addresses of certain domain names are stable, you can add them to a list of hosts with fixed IP addresses. This helps optimize the SDK's internal logic and further reduces unnecessary domain name resolution operations.

  • Example code:

var hostListWithFixedIp: MutableList<String> = mutableListOf()
hostListWithFixedIp.add("${YOUR_DOMAIN_NAME}")
val config = InitConfig.Builder()
    .configHostWithFixedIp(hostListWithFixedIp)
    .build()
HttpDns.init(accountID, config)
ArrayList<String> hostListWithFixedIp = new ArrayList<>();
hostListWithFixedIp.add("${YOUR_DOMAIN_NAME}");
InitConfig config = new InitConfig.Builder()
    .configHostWithFixedIp(hostListWithFixedIp)
    .build();
HttpDns.init(accountID, config);   
Note

The advanced features described in this topic require HTTPDNS SDK version 2.3.0 or later.

Applies to

  • HTTPDNS