https://github.com/vicanso/dnscache
dns cache
https://github.com/vicanso/dnscache
cache dns dns-cache
Last synced: 3 months ago
JSON representation
dns cache
- Host: GitHub
- URL: https://github.com/vicanso/dnscache
- Owner: vicanso
- License: apache-2.0
- Created: 2018-12-07T13:00:58.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-09T01:38:03.000Z (over 1 year ago)
- Last Synced: 2025-05-18T10:07:21.825Z (5 months ago)
- Topics: cache, dns, dns-cache
- Language: Go
- Size: 82 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DNS Cache
[](https://github.com/vicanso/dnscache/actions)
DNS Cache for http client
## API
### New
create a dns cache instance
- `ttl` cache's ttl seconds
```go
onStats := func(h string, d time.Duration, _ []string) {
fmt.Println(d)
}
dc := dnscache.New(
time.Minute,
dnscache.PolicyOption(dnscache.PolicyRandom),
dnscache.StaleOption(time.Minute),
dnscache.OnStatsOption(onStats),
dnscache.NetworkOption(dnscache.NetworkIPV4),
)
```### Lookup
lookup ip address for host
- `host` host name
```go
dc := dnscache.New(time.Minute)
ipAddrs, err := dc.Lookup(context.Background(), "www.baidu.com")
```### LookupWithCache
lookup ip address for host, it will use the cache first.
- `host` host name
```go
dc := dnscache.New(time.Minute)
ipAddrs, err := dc.LookupWithCache(context.Background(), "www.baidu.com")
```### GetDialContext
get dial context function for http client
```go
dc := dnscache.New(time.Minute)
http.DefaultClient.Transport = &http.Transport{
DialContext: dc.GetDialContext(),
}
resp, err := http.Get("https://www.baidu.com/")
```### Set
set the dns cache, if the `CreatedAt` is less than 0, it will never be expired.
```go
dc := dnscache.New(time.Minute)
dc.Set("www.baidu.com", IPCache{
CreatedAt: time.Now().Unix(),
IPAddrs: []net.IP{
net.ParseIP("1.1.1.1"),
},
})
```### Get
get the dns cache
```go
dc := dnscache.New(time.Minute)
host := "www.baidu.com"
dc.Set(host, IPCache{
CreatedAt: time.Now().Unix(),
IPAddrs: []net.IP{
net.ParseIP("1.1.1.1"),
},
})
cache, ok := dc.Get(host)
```