func (srv *Server) PopulateCachesByRR(listRR []*ResourceRecord, from string) (
	err error,
) {
	n := 0
	for _, rr := range listRR {
		err = srv.caches.upsertRR(rr)
		if err != nil {
			return err
		}
		n++
	}
	if debug.Value >= 1 {
		fmt.Printf("dns: %d out of %d records cached from %q\n", n,
			len(listRR), from)
	}
	return nil
}
PopulateCachesByRR update or insert new ResourceRecord into caches.
package dns
import (
	"crypto/tls"
	"encoding/base64"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"regexp"
	"strings"
	"sync"
	"time"
	libbytes "github.com/shuLhan/share/lib/bytes"
	"github.com/shuLhan/share/lib/debug"
)
const (
	aliveInterval = 10 * time.Second
)
type Server struct {
	HostsFiles map[string]*HostsFile
	opts        *ServerOptions
	errListener chan error
	caches      *caches
	tlsConfig *tls.Config
	udp *net.UDPConn
	tcp *net.TCPListener
	dot net.Listener
	doh *http.Server
	requestq  chan *request
	primaryq  chan *request
	fallbackq chan *request
	fwLocker   sync.Mutex
	fwStoppers []chan bool
	fwn        int  
}
func NewServer(opts *ServerOptions) (srv *Server, err error) {
	err = opts.init()
	if err != nil {
		return nil, err
	}
	srv = &Server{
		opts:     opts,
		requestq: make(chan *request, 512),
		primaryq: make(chan *request, 512),
	}
	udpAddr := opts.getUDPAddress()
	srv.udp, err = net.ListenUDP("udp", udpAddr)
	if err != nil {
		return nil, fmt.Errorf("dns: error listening on UDP '%v': %s",
			udpAddr, err.Error())
	}
	tcpAddr := opts.getTCPAddress()
	srv.tcp, err = net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		return nil, fmt.Errorf("dns: error listening on TCP '%v': %s",
			tcpAddr, err.Error())
	}
	if len(opts.TLSCertFile) > 0 && len(opts.TLSPrivateKey) > 0 {
		cert, err := tls.LoadX509KeyPair(opts.TLSCertFile, opts.TLSPrivateKey)
		if err != nil {
			return nil, fmt.Errorf("dns: error loading certificate: " + err.Error())
		}
		srv.tlsConfig = &tls.Config{
			Certificates: []tls.Certificate{
				cert,
			},
			InsecureSkipVerify: opts.TLSAllowInsecure,
		}
	}
	srv.errListener = make(chan error, 1)
	srv.caches = newCaches(opts.PruneDelay, opts.PruneThreshold)
	return srv, nil
}
func isResponseValid(req *request, res *Message) bool {
	if req.message.Question.Name != res.Question.Name {
		log.Printf("dns: unmatched response name, got %s want %s",
			req.message.Question.Name, res.Question.Name)
		return false
	}
	if req.message.Question.Type != res.Question.Type {
		log.Printf("dns: unmatched response type, got %s want %s",
			req.message.Question.String(), res.Question.String())
		return false
	}
	if req.message.Question.Class != res.Question.Class {
		log.Printf("dns: unmatched response class, got %s want %s",
			req.message.Question.String(), res.Question.String())
		return false
	}
	return true
}
func (srv *Server) CachesLRU() []*Answer {
	return srv.caches.list()
}
func (srv *Server) SearchCaches(re *regexp.Regexp) []*Message {
	return srv.caches.search(re)
}
func (srv *Server) PopulateCaches(msgs []*Message, from string) {
	var (
		n        int
		inserted bool
		isLocal  = true
	)
	for _, msg := range msgs {
		an := newAnswer(msg, isLocal)
		inserted = srv.caches.upsert(an)
		if inserted {
			n++
		}
	}
	if debug.Value >= 1 {
		fmt.Printf("dns: %d out of %d records cached from %q\n", n,
			len(msgs), from)
	}
}