cache

package
v1.3.15-beta Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const SnapshotReplaced = "*"

SnapshotReplaced is the value sent on the resourceChanged channel when the entire cache is to be replaced via ReplaceAll.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfiguredObjectCache

type ConfiguredObjectCache = ObjectCache[*pb.ConfiguredKubernetesObjectData]

ConfiguredObjectCache is the cache type for CloudSecure configured objects.

func NewConfiguredObjectCache

func NewConfiguredObjectCache() *ConfiguredObjectCache

NewConfiguredObjectCache creates a new cache for configured objects.

type ObjectCache

type ObjectCache[T proto.Message] struct {
	// contains filtered or unexported fields
}

ObjectCache is a generic thread-safe cache for storing objects by ID. It supports atomic replacement for snapshot-based updates.

This cache is generic to support both:

  • Config cache: stores *pb.ConfiguredKubernetesObjectData from CloudSecure (desired state)
  • Runtime cache: stores *pb.ConfiguredKubernetesObjectData from Kubernetes (actual state)

Access patterns:

  • Snapshot: Build a local map, then call ReplaceAll() to atomically swap it in
  • Mutations: Use Insert() and Delete() which handle locking internally
  • Reading: Use Get(), Values(), Len() which handle locking internally

Block on <-cache.IsReady() to wait for the first snapshot to complete before reading.

The cache notifies consumers of changes via an unbuffered resourceChanged channel. Insert and Delete send the object ID; ReplaceAll sends SnapshotReplaced to indicate a full snapshot.

func NewObjectCache

func NewObjectCache[T proto.Message]() *ObjectCache[T]

NewObjectCache creates a new cache instance.

func (*ObjectCache[T]) Close added in v1.4.0

func (c *ObjectCache[T]) Close()

Close closes the resourceChanged channel, unblocking any goroutines waiting to send.

func (*ObjectCache[T]) Delete

func (c *ObjectCache[T]) Delete(ctx context.Context, id string) error

Delete removes an object from the cache by ID. Sends the object ID on the resourceChanged channel only if the object existed.

func (*ObjectCache[T]) Get

func (c *ObjectCache[T]) Get(id string) T

Get retrieves an object by ID. Returns the zero value if not found.

func (*ObjectCache[T]) Insert

func (c *ObjectCache[T]) Insert(ctx context.Context, id string, obj T) error

Insert adds or updates an object in the cache. Sends the object ID on the resourceChanged channel only if the object changed.

func (*ObjectCache[T]) IsReady

func (c *ObjectCache[T]) IsReady() <-chan struct{}

IsReady returns a channel that is closed when the first snapshot is complete. Use <-cache.IsReady() to block until the cache has consistent data.

func (*ObjectCache[T]) Len

func (c *ObjectCache[T]) Len() int

Len returns the number of objects in the cache.

func (*ObjectCache[T]) ReplaceAll

func (c *ObjectCache[T]) ReplaceAll(ctx context.Context, objects map[string]T) error

ReplaceAll atomically replaces all objects in the cache with the provided map. Use this for snapshot ingestion: build a local map, then call ReplaceAll to swap it in. The cache remains consistent until the swap completes. Also marks the cache as ready on first call to indicate cache now has valid data. Sends SnapshotReplaced on the resourceChanged channel to signal a full snapshot replacement.

func (*ObjectCache[T]) ResourceChanged added in v1.4.0

func (c *ObjectCache[T]) ResourceChanged() <-chan string

ResourceChanged returns an unbuffered channel that emits the ID of every resource modified. SnapshotReplaced ("*") indicates the entire cache was replaced via a full snapshot (ReplaceAll). The cache owns this channel and writes to it on Insert, Delete, and ReplaceAll.

func (*ObjectCache[T]) Values

func (c *ObjectCache[T]) Values() []T

Values returns all objects in the cache, sorted by ID for consistency.

Jump to

Keyboard shortcuts

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