Giter Site home page Giter Site logo

dnscache's Introduction

DNS Cache

Build Status

DNS Cache for http client

API

New

create a dns cache instance

  • ttl cache's ttl seconds
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
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
dc := dnscache.New(time.Minute)
ipAddrs, err := dc.LookupWithCache(context.Background(), "www.baidu.com")

GetDialContext

get dial context function for http client

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.

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

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)

dnscache's People

Contributors

vicanso avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.