New (cache/store.go)

devops-ai-guidelines · 02-mcp-for-devops/code/07/pkg/cache/store.go · go · MIT
infra

Thread‑safe TTL+LRU cache implementation for memoising Kubernetes API calls.

// Source: https://github.com/VersusControl/devops-ai-guidelines | License: MIT | Copyright 2025 VersusControl
// Provided via justclaw.it.com; keep this notice with the code.
// Package cache provides a small TTL + LRU store used to memoise expensive
// Kubernetes API calls. It is intentionally dependency-free so it can be
// dropped into any of the chapter modules.
package cache

import (
	"container/list"
	"sync"
	"time"
)

// Store is a thread-safe TTL+LRU cache keyed by string.
type Store struct {
	mu      sync.Mutex
	maxSize int
	ttl     time.Duration
	items   map[string]*list.Element
	lru     *list.List
	now     func() time.Time
}

type entry struct {
	key       string
	value     any
	expiresAt time.Time
}

// New constructs a Store with the supplied max size and TTL.
func New(maxSi

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key