{"id":18673179,"url":"https://github.com/vicanso/dnscache","last_synced_at":"2025-06-28T17:32:58.814Z","repository":{"id":144270879,"uuid":"160824347","full_name":"vicanso/dnscache","owner":"vicanso","description":"dns cache","archived":false,"fork":false,"pushed_at":"2024-02-09T01:38:03.000Z","size":84,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-18T10:07:21.825Z","etag":null,"topics":["cache","dns","dns-cache"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vicanso.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-12-07T13:00:58.000Z","updated_at":"2023-10-30T10:29:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"ba203bd5-ebca-465b-b19b-739c639a4255","html_url":"https://github.com/vicanso/dnscache","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/vicanso/dnscache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fdnscache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fdnscache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fdnscache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fdnscache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vicanso","download_url":"https://codeload.github.com/vicanso/dnscache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fdnscache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262469048,"owners_count":23316242,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","dns","dns-cache"],"created_at":"2024-11-07T09:14:12.879Z","updated_at":"2025-06-28T17:32:58.764Z","avatar_url":"https://github.com/vicanso.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DNS Cache\n\n[![Build Status](https://github.com/vicanso/dnscache/workflows/Test/badge.svg)](https://github.com/vicanso/dnscache/actions)\n\nDNS Cache for http client\n\n## API\n\n### New\n\ncreate a dns cache instance\n\n- `ttl` cache's ttl seconds\n\n```go\nonStats := func(h string, d time.Duration, _ []string) {\n  fmt.Println(d)\n}\ndc := dnscache.New(\n  time.Minute,\n  dnscache.PolicyOption(dnscache.PolicyRandom),\n  dnscache.StaleOption(time.Minute),\n  dnscache.OnStatsOption(onStats),\n  dnscache.NetworkOption(dnscache.NetworkIPV4),\n)\n```\n\n### Lookup\n\nlookup ip address for host\n\n- `host` host name\n\n```go\ndc := dnscache.New(time.Minute)\nipAddrs, err := dc.Lookup(context.Background(), \"www.baidu.com\")\n```\n\n### LookupWithCache\n\nlookup ip address for host, it will use the cache first.\n\n- `host` host name\n\n```go\ndc := dnscache.New(time.Minute)\nipAddrs, err := dc.LookupWithCache(context.Background(), \"www.baidu.com\")\n```\n\n### GetDialContext\n\nget dial context function for http client\n\n```go\ndc := dnscache.New(time.Minute)\nhttp.DefaultClient.Transport = \u0026http.Transport{\n  DialContext: dc.GetDialContext(),\n}\nresp, err := http.Get(\"https://www.baidu.com/\")\n```\n\n### Set\n\nset the dns cache, if the `CreatedAt` is less than 0, it will never be expired.\n\n```go\ndc := dnscache.New(time.Minute)\ndc.Set(\"www.baidu.com\", IPCache{\n  CreatedAt: time.Now().Unix(),\n  IPAddrs: []net.IP{\n    net.ParseIP(\"1.1.1.1\"),\n  },\n})\n```\n\n### Get\n\nget the dns cache\n\n```go\ndc := dnscache.New(time.Minute)\nhost := \"www.baidu.com\"\ndc.Set(host, IPCache{\n  CreatedAt: time.Now().Unix(),\n  IPAddrs: []net.IP{\n    net.ParseIP(\"1.1.1.1\"),\n  },\n})\ncache, ok := dc.Get(host)\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicanso%2Fdnscache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvicanso%2Fdnscache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicanso%2Fdnscache/lists"}