Can I customize the client cache time in HTTPDNS?

更新时间:
复制 MD 格式

Problem:

Can I customize the client cache time in HTTPDNS?

Solution:

By default, the HTTPDNS client SDK uses the TTL value from the domain name resolution result as the cache duration. The SDK also provides an interface to customize the TTL. This lets you specify a different TTL for each domain name and change the cache duration of the resolution results.

You can call the interfaces below during HTTPDNS initialization to specify the TTL for a domain name. The SDK then uses this specified TTL value as the cache time for the domain name's resolution result.

Android interface:

SDK versions 2.3.0 and later support TTL updates.

/**
 * Interface to change the TTL duration.
 * Customize the TTL to control cache duration.
 */
public interface CacheTtlChanger {

    /**
     * Returns a custom TTL based on the domain name, IP type, and the TTL from the service.
     * @param host The domain name.
     * @param type The IP type.
     * @param ttl The TTL from the service, in seconds.
     * @return The custom TTL, in seconds.
     */
    int changeCacheTtl(String host, RequestIpType type, int ttl);
}
iOS interface:
@protocol HttpdnsTTLDelegate <NSObject>

/// Customize the TTL duration for the host.
/// @return The custom TTL duration.
/// @param host The domain name.
/// @param ipType The IP type for the current query.
/// @param ttl The TTL returned from the current domain name resolution.
- (int64_t)httpdnsHost:(NSString *)host ipType:(AlicloudHttpDNS_IPType)ipType ttl:(int64_t)ttl;

@end
Harmony interface:
/**
 * Callback interface to customize the TTL.
 */
export interface ICacheTtlCustomizer {
  /**
   * Returns the custom TTL.
   * @param host
   * @param type
   * @param serverTtlInSec The TTL returned by the HTTPDNS service.
   * @returns
   */
  getCacheTtlInSec(host: string, type: IpType, serverTtlInSec: number): number;
}