cache

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cache provides generic cache. It is used to store values by key and retrieve them later.

Index

Constants

View Source
const (
	// RunCmdCacheContextKey is the context key used to store and retrieve the run command cache
	RunCmdCacheContextKey ctxKey = iota

	// RepoRootCacheContextKey is the context key for the repo-root cache.
	RepoRootCacheContextKey
)

Variables

This section is empty.

Functions

func ContextWithCache added in v0.73.0

func ContextWithCache(ctx context.Context) context.Context

Types

type Cache

type Cache[V any] struct {
	Cache map[string]V
	Mutex *sync.RWMutex
	Name  string
}

Cache - generic cache implementation

func ContextCache added in v0.66.3

func ContextCache[T any](ctx context.Context, key any) *Cache[T]

ContextCache returns cache from the context. If the cache is nil, it creates a new instance.

func NewCache

func NewCache[V any](name string) *Cache[V]

NewCache - create new cache with generic type V

func (*Cache[V]) Get

func (c *Cache[V]) Get(ctx context.Context, key string) (V, bool)

Get - fetch value from cache by key

func (*Cache[V]) Put

func (c *Cache[V]) Put(ctx context.Context, key string, value V)

Put - put value into cache by key

type ExpiringCache

type ExpiringCache[V any] struct {
	Cache map[string]ExpiringItem[V]
	Mutex *sync.RWMutex
	Name  string
}

ExpiringCache - cache with items with expiration time

func NewExpiringCache

func NewExpiringCache[V any](name string) *ExpiringCache[V]

NewExpiringCache - create new cache with generic type V

func (*ExpiringCache[V]) Get

func (c *ExpiringCache[V]) Get(ctx context.Context, key string) (V, bool)

Get - fetch value from cache by key

func (*ExpiringCache[V]) Put

func (c *ExpiringCache[V]) Put(ctx context.Context, key string, value V, expiration time.Time)

Put - put value into cache by key

type ExpiringItem

type ExpiringItem[V any] struct {
	Value      V
	Expiration time.Time
}

ExpiringItem - item with expiration time

type RepoRootCache added in v1.0.4

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

RepoRootCache stores git repository roots and answers prefix-containment queries. Roots are kept sorted by descending length so Lookup yields the deepest matching root for paths inside nested repositories.

func ContextRepoRootCache added in v1.0.4

func ContextRepoRootCache(ctx context.Context, key any) *RepoRootCache

ContextRepoRootCache returns the RepoRootCache stored on the context, or a fresh detached instance if none is present so callers do not need to nil-check.

func NewRepoRootCache added in v1.0.4

func NewRepoRootCache(name string) *RepoRootCache

NewRepoRootCache constructs an empty RepoRootCache. The name is used as a prefix for telemetry counters.

func (*RepoRootCache) Add added in v1.0.4

func (c *RepoRootCache) Add(ctx context.Context, root string)

Add records root as a known repository root. Inserts are ordered by descending length so Lookup's first match is the deepest. Duplicates are ignored.

func (*RepoRootCache) BeginResolve added in v1.0.4

func (c *RepoRootCache) BeginResolve()

BeginResolve serializes callers that are about to perform the external lookup whose result they will Add. Callers must Lookup again after BeginResolve returns so a concurrent populate is observed before running the external resolver. EndResolve releases the lock.

The lock is independent of the cache's read/write mutex; it is held across the external call so concurrent misses collapse to a single resolution.

func (*RepoRootCache) EndResolve added in v1.0.4

func (c *RepoRootCache) EndResolve()

EndResolve releases the lock taken by BeginResolve.

func (*RepoRootCache) Len added in v1.0.4

func (c *RepoRootCache) Len() int

Len returns the number of cached roots.

func (*RepoRootCache) Lookup added in v1.0.4

func (c *RepoRootCache) Lookup(ctx context.Context, path string) (string, bool)

Lookup returns the deepest cached root that contains path. Containment is component-aware: `/foo` does not match `/foobar`.

Jump to

Keyboard shortcuts

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