code: func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
  	do := false
  	mt, opt := response.Typify(res, time.Now().UTC())
  	if opt != nil {
  		do = opt.Do()
  	}
  
  	// key returns empty string for anything we don't want to cache.
  	key := key(res, mt, do)
  
  	duration := w.pttl
  	if mt == response.NameError || mt == response.NoData {
  		duration = w.nttl
  	}
  
  	msgTTL := minMsgTTL(res, mt)
  	if msgTTL < duration {
  		duration = msgTTL
  	}
  
  	if key != -1 && duration > 0 {
  		w.set(res, key, mt, duration)
  
  		cacheSize.WithLabelValues(Success).Set(float64(w.pcache.Len()))
  		cacheSize.WithLabelValues(Denial).Set(float64(w.ncache.Len()))
  	}
  
  	if w.prefetch {
  		return nil
  	}
  
  	// Apply capped TTL to this reply to avoid jarring TTL experience 1799 -> 8 (e.g.)
  	ttl := uint32(duration.Seconds())
  	for i := range res.Answer {
  		res.Answer[i].Header().Ttl = ttl
  	}
  	for i := range res.Ns {
  		res.Ns[i].Header().Ttl = ttl
  	}
  	for i := range res.Extra {
  		if res.Extra[i].Header().Rrtype != dns.TypeOPT {
  			res.Extra[i].Header().Ttl = ttl
  		}
  	}
  	return w.ResponseWriter.WriteMsg(res)
  }
docs: // WriteMsg implements the dns.ResponseWriter interface.