CURLOPT_DNS_CACHE_TIMEOUT.md (2491B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_DNS_CACHE_TIMEOUT 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_CONNECTTIMEOUT_MS (3) 9 - CURLOPT_DNS_SERVERS (3) 10 - CURLOPT_DNS_USE_GLOBAL_CACHE (3) 11 - CURLOPT_MAXAGE_CONN (3) 12 - CURLOPT_RESOLVE (3) 13 Protocol: 14 - All 15 Added-in: 7.9.3 16 --- 17 18 # NAME 19 20 CURLOPT_DNS_CACHE_TIMEOUT - life-time for DNS cache entries 21 22 # SYNOPSIS 23 24 ~~~c 25 #include <curl/curl.h> 26 27 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_DNS_CACHE_TIMEOUT, long age); 28 ~~~ 29 30 # DESCRIPTION 31 32 Pass a long, this sets the timeout in seconds. Name resolve results are kept 33 in memory and used for this number of seconds. Set to zero to completely 34 disable caching, or set to -1 to make the cached entries remain forever. By 35 default, libcurl caches this info for 60 seconds. 36 37 We recommend users not to tamper with this option unless strictly necessary. 38 If you do, be careful of using large values that can make the cache size grow 39 significantly if many different hostnames are used within that timeout period. 40 41 The name resolve functions of various libc implementations do not re-read name 42 server information unless explicitly told so (for example, by calling 43 *res_init(3)*). This may cause libcurl to keep using the older server even 44 if DHCP has updated the server info, and this may look like a DNS cache issue 45 to the casual libcurl-app user. 46 47 DNS entries have a "TTL" property but libcurl does not use that. This DNS 48 cache timeout is entirely speculative that a name resolves to the same address 49 for a small amount of time into the future. 50 51 Since version 8.1.0, libcurl prunes entries from the DNS cache if it exceeds 52 30,000 entries no matter which timeout value is used. 53 54 # DEFAULT 55 56 60 57 58 # %PROTOCOLS% 59 60 # EXAMPLE 61 62 ~~~c 63 int main(void) 64 { 65 CURL *curl = curl_easy_init(); 66 if(curl) { 67 CURLcode res; 68 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/foo.bin"); 69 70 /* only reuse addresses for a short time */ 71 curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, 2L); 72 73 res = curl_easy_perform(curl); 74 75 /* in this second request, the cache is not be used if more than 76 two seconds have passed since the previous name resolve */ 77 res = curl_easy_perform(curl); 78 79 curl_easy_cleanup(curl); 80 } 81 } 82 ~~~ 83 84 # %AVAILABILITY% 85 86 # RETURN VALUE 87 88 curl_easy_setopt(3) returns a CURLcode indicating success or error. 89 90 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 91 libcurl-errors(3).