dedup

package
v0.2.49 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: GPL-2.0, GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package dedup provides a bounded, TTL-based idempotency cache for deduplicating repeated requests by request ID.

Index

Constants

View Source
const DefaultMaxEntries = 10_000

DefaultMaxEntries caps the request_id→result cache.

View Source
const DefaultMaxResult = 64 * 1024

DefaultMaxResult skips caching replies larger than this.

View Source
const DefaultTTL = 5 * time.Minute

DefaultTTL is the default time-to-live for cache entries.

Variables

This section is empty.

Functions

func PruneExpired

func PruneExpired(now time.Time, ttl time.Duration, entries map[string]Entry) int

PruneExpired removes entries older than ttl relative to now. Returns the number of entries removed.

Types

type Cache

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

Cache deduplicates repeated command requests by caching request_id → result for a bounded TTL. It owns its own mutex so check/record operations don't contend with other subsystems.

func New

func New(ttl time.Duration, maxEntries, maxResult int) *Cache

New creates a Cache with the given TTL, max entries, and max result size.

func (*Cache) Check

func (c *Cache) Check(reqID string) ([]byte, bool)

Check returns the cached result for reqID if present and not expired.

func (*Cache) Record

func (c *Cache) Record(reqID string, result []byte)

Record stores a request_id → result mapping. Enforces two bounds: results over maxResult are not cached, and when the map hits maxEntries the oldest entry is evicted via a min-heap in O(log n). Falls back to an O(n) scan if the heap is empty.

func (*Cache) StartCleaner

func (c *Cache) StartCleaner(done <-chan struct{})

StartCleaner runs a periodic sweep that removes expired entries. Exits when done is closed.

type Entry

type Entry struct {
	Ts     time.Time
	Result []byte
}

Entry is a single cached result with its insertion timestamp.

Jump to

Keyboard shortcuts

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