func (s *TCPNameServer) Cleanup() error {
	now := time.Now()
	s.Lock()
	defer s.Unlock()
	if len(s.ips) == 0 {
		return newError("nothing to do. stopping...")
	}
	for domain, record := range s.ips {
		if record.A != nil && record.A.Expire.Before(now) {
			record.A = nil
		}
		if record.AAAA != nil && record.AAAA.Expire.Before(now) {
			record.AAAA = nil
		}
		if record.A == nil && record.AAAA == nil {
			newError(s.name, " cleanup ", domain).AtDebug().WriteToLog()
			delete(s.ips, domain)
		} else {
			s.ips[domain] = record
		}
	}
	if len(s.ips) == 0 {
		s.ips = make(map[string]record)
	}
	return nil
}
Cleanup clears expired items from cache
package dns
import (
	"bytes"
	"context"
	"encoding/binary"
	"net/url"
	"sync"
	"sync/atomic"
	"time"
	"golang.org/x/net/dns/dnsmessage"
	"github.com/v2fly/v2ray-core/v4/common"
	"github.com/v2fly/v2ray-core/v4/common/buf"
	"github.com/v2fly/v2ray-core/v4/common/net"
	"github.com/v2fly/v2ray-core/v4/common/protocol/dns"
	"github.com/v2fly/v2ray-core/v4/common/session"
	"github.com/v2fly/v2ray-core/v4/common/signal/pubsub"
	"github.com/v2fly/v2ray-core/v4/common/task"
	dns_feature "github.com/v2fly/v2ray-core/v4/features/dns"
	"github.com/v2fly/v2ray-core/v4/features/routing"
	"github.com/v2fly/v2ray-core/v4/transport/internet"
)
type TCPNameServer struct {
	sync.RWMutex
	name        string
	destination net.Destination
	ips         map[string]record
	pub         *pubsub.Service
	cleanup     *task.Periodic
	reqID       uint32
	dial        func(context.Context) (net.Conn, error)
}
func NewTCPNameServer(url *url.URL, dispatcher routing.Dispatcher) (*TCPNameServer, error) {
	s, err := baseTCPNameServer(url, "TCP")
	if err != nil {
		return nil, err
	}
	s.dial = func(ctx context.Context) (net.Conn, error) {
		link, err := dispatcher.Dispatch(ctx, s.destination)
		if err != nil {
			return nil, err
		}
		return net.NewConnection(
			net.ConnectionInputMulti(link.Writer),
			net.ConnectionOutputMulti(link.Reader),
		), nil
	}
	return s, nil
}
func NewTCPLocalNameServer(url *url.URL) (*TCPNameServer, error) {
	s, err := baseTCPNameServer(url, "TCPL")
	if err != nil {
		return nil, err
	}
	s.dial = func(ctx context.Context) (net.Conn, error) {
		return internet.DialSystem(ctx, s.destination, nil)
	}
	return s, nil
}
func baseTCPNameServer(url *url.URL, prefix string) (*TCPNameServer, error) {
	var err error
	port := net.Port(53)
	if url.Port() != "" {
		port, err = net.PortFromString(url.Port())
		if err != nil {
			return nil, err
		}
	}
	dest := net.TCPDestination(net.ParseAddress(url.Hostname()), port)
	s := &TCPNameServer{
		destination: dest,
		ips:         make(map[string]record),
		pub:         pubsub.NewService(),
		name:        prefix + "//" + dest.NetAddr(),
	}
	s.cleanup = &task.Periodic{
		Interval: time.Minute,
		Execute:  s.Cleanup,
	}
	return s, nil
}
func (s *TCPNameServer) Name() string {
	return s.name
}