cache

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a sharded in-memory DNS cache with TTL-based expiration.

func NewCache

func NewCache(maxEntries int, minTTL, maxTTL, negMaxTTL uint32, m *metrics.Metrics) *Cache

NewCache creates a new sharded DNS cache.

func NewCacheWithStale

func NewCacheWithStale(maxEntries int, minTTL, maxTTL, negMaxTTL uint32, serveStale bool, staleTTL uint32, m *metrics.Metrics) *Cache

NewCacheWithStale creates a cache with optional serve-stale support (RFC 8767).

func (*Cache) Delete

func (c *Cache) Delete(name string, qtype uint16, class uint16) bool

Delete removes a specific entry from the cache. Returns true if the entry was found and deleted.

func (*Cache) DetailedStats

func (c *Cache) DetailedStats() CacheStats

DetailedStats returns detailed cache statistics including positive/negative breakdowns.

func (*Cache) Flush

func (c *Cache) Flush()

Flush clears all cache entries.

func (*Cache) Get

func (c *Cache) Get(name string, qtype uint16, class uint16) (*Entry, bool)

Get retrieves an entry from the cache with TTL decay.

func (*Cache) GetStale

func (c *Cache) GetStale(name string, qtype uint16, class uint16) (*Entry, bool)

GetStale retrieves an expired entry from the cache for serve-stale (RFC 8767). Returns the entry with staleTTL if serve-stale is enabled and the entry exists but is expired.

The returned entry's DNSSECStatus is cleared: when records are served past their TTL the covering RRSIGs may also be past their signature-inception/ expiration window, so we cannot honestly carry over the original "secure" verdict. Downstream validators that care will re-verify; the AD bit on a stale serve would be a lie.

func (*Cache) GetWithECS

func (c *Cache) GetWithECS(name string, qtype uint16, class uint16, ecsPrefix string) (*Entry, bool)

GetWithECS retrieves a cache entry using an ECS-aware key. If ecsPrefix is empty, it behaves like Get.

func (*Cache) Lookup

func (c *Cache) Lookup(name string, qtype uint16, class uint16) (*Entry, bool)

Lookup retrieves an entry from the cache without deleting expired entries. Unlike Get, this is a read-only operation suitable for inspection.

func (*Cache) LookupAll

func (c *Cache) LookupAll(name string, class uint16) []*Entry

LookupAll returns all cached entries for the given name (across all types).

func (*Cache) NegativeEntries

func (c *Cache) NegativeEntries(limit int) []NegativeEntryInfo

NegativeEntries returns up to limit negative cache entries across all shards.

func (*Cache) SetHardenBelowNX

func (c *Cache) SetHardenBelowNX(enabled bool)

SetHardenBelowNX enables or disables RFC 8020 harden-below-nxdomain.

func (*Cache) SetPrefetchEnabled

func (c *Cache) SetPrefetchEnabled(enabled bool)

SetPrefetchEnabled enables or disables proactive cache prefetching.

func (*Cache) SetPrefetchFunc

func (c *Cache) SetPrefetchFunc(fn func(name string, qtype, qclass uint16))

SetPrefetchFunc sets the callback used to re-resolve entries nearing expiry.

func (*Cache) StartSweeper

func (c *Cache) StartSweeper(ctx context.Context, interval time.Duration)

StartSweeper runs a background goroutine that periodically evicts expired entries.

func (*Cache) Stats

func (c *Cache) Stats() CacheStats

Stats returns current cache statistics.

func (*Cache) Store

func (c *Cache) Store(name string, qtype uint16, class uint16, answers []dns.ResourceRecord, authority []dns.ResourceRecord)

Store caches a positive DNS result.

func (*Cache) StoreNegative

func (c *Cache) StoreNegative(name string, qtype uint16, class uint16, negType NegativeType, rcode uint8, authority []dns.ResourceRecord)

StoreNegative caches a negative DNS result (NXDOMAIN/NODATA). NXDOMAIN applies to the entire name (RFC 2308 §3) so it is stored with qtype=0 as a sentinel. NODATA is type-specific.

func (*Cache) StoreWithECS

func (c *Cache) StoreWithECS(name string, qtype uint16, class uint16, ecsPrefix string, answers []dns.ResourceRecord, authority []dns.ResourceRecord)

StoreWithECS caches a positive DNS result with an ECS prefix key. If ecsPrefix is empty, it behaves like Store.

func (*Cache) StoreWithStatus added in v0.6.3

func (c *Cache) StoreWithStatus(name string, qtype uint16, class uint16, answers []dns.ResourceRecord, authority []dns.ResourceRecord, dnssecStatus string)

StoreWithStatus caches a positive DNS result together with the validator's verdict ("secure", "insecure", "bogus", or ""). The status is consumed by the server to set the AD bit on cache-served responses without re-running DNSSEC validation per lookup.

type CacheStats

type CacheStats struct {
	Entries         int
	PositiveEntries int
	NegativeEntries int
}

CacheStats holds cache statistics.

type Entry

type Entry struct {
	Records    []dns.ResourceRecord
	Authority  []dns.ResourceRecord
	InsertedAt time.Time
	OrigTTL    uint32
	Negative   bool
	NegType    NegativeType
	SOA        *dns.ResourceRecord
	RCODE      uint8

	// DNSSECStatus records the validator's verdict on the cached records:
	// "secure", "insecure", "bogus", or "" (unknown). Used by the server
	// to set the AD bit on responses served from cache without re-running
	// validation.
	DNSSECStatus string
	// contains filtered or unexported fields
}

Entry represents a cached DNS result.

func (*Entry) Expired

func (e *Entry) Expired() bool

Expired returns true if the entry has expired.

func (*Entry) RemainingTTL

func (e *Entry) RemainingTTL() uint32

RemainingTTL returns the remaining TTL in seconds.

func (*Entry) WithDecayedTTL

func (e *Entry) WithDecayedTTL(remaining uint32) *Entry

WithDecayedTTL returns a deep copy of the entry with TTL adjusted.

type NegativeEntryInfo

type NegativeEntryInfo struct {
	Name         string               `json:"name"`
	QType        string               `json:"qtype"`
	NegType      string               `json:"neg_type"`
	RCODE        string               `json:"rcode"`
	RemainingTTL uint32               `json:"remaining_ttl"`
	Authority    []ResourceRecordInfo `json:"authority"`
}

NegativeEntryInfo holds information about a negative cache entry.

type NegativeType

type NegativeType uint8

NegativeType indicates the type of negative cache entry.

const (
	NegNone     NegativeType = 0
	NegNXDomain NegativeType = 1
	NegNoData   NegativeType = 2
)

type ResourceRecordInfo

type ResourceRecordInfo struct {
	Name  string `json:"name"`
	Type  string `json:"type"`
	TTL   uint32 `json:"ttl"`
	RData string `json:"rdata"`
}

ResourceRecordInfo holds a human-readable representation of a DNS resource record.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL