cache

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides a generic caching mechanism with indexing capabilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache is a generic struct that provides caching functionality for objects of type T.

func NewIndexer

func NewIndexer[T any](
	store Storage[T],
	keyFunc KeyFunc[T],
) *Cache[T]

NewIndexer creates a new instance of Cache with the provided storage and key function.

func (*Cache[T]) Add

func (c *Cache[T]) Add(obj T) error

Add adds a new item to the cache.

func (*Cache[T]) AddIndexers

func (c *Cache[T]) AddIndexers(newIndexers port.Indexers[T]) error

AddIndexers adds new indexers to the cache.

func (*Cache[T]) ByIndex

func (c *Cache[T]) ByIndex(indexName, indexedValue string) ([]T, error)

ByIndex retrieves all items that match the given indexed value for a specific index function.

func (*Cache[T]) Delete

func (c *Cache[T]) Delete(obj T) error

Delete removes an item from the cache by its object.

func (*Cache[T]) Get

func (c *Cache[T]) Get(obj T) (item *T, exists bool, err error)

Get retrieves an item from the cache by its object.

func (*Cache[T]) GetByKey

func (c *Cache[T]) GetByKey(key string) (item *T, exists bool, err error)

GetByKey retrieves an item from the cache by its key.

func (*Cache[T]) GetIndexers

func (c *Cache[T]) GetIndexers() port.Indexers[T]

GetIndexers returns the indexers used by the cache.

func (*Cache[T]) Index

func (c *Cache[T]) Index(indexName string, obj T) ([]any, error)

Index returns the indexed values for a specific object and index function.

func (*Cache[T]) IndexKeys

func (c *Cache[T]) IndexKeys(indexName, indexedValue string) ([]string, error)

IndexKeys lists all keys that match the given indexed value for a specific index function.

func (*Cache[T]) List

func (c *Cache[T]) List() []T

List returns a list of all items in the cache.

func (*Cache[T]) ListIndexFuncValues

func (c *Cache[T]) ListIndexFuncValues(indexName string) []string

ListIndexFuncValues lists all indexed values for a given index function.

func (*Cache[T]) ListKeys

func (c *Cache[T]) ListKeys() []string

ListKeys returns a list of all keys in the cache.

func (*Cache[T]) Replace

func (c *Cache[T]) Replace(list []T, resourceVersion string) error

Replace replaces the entire contents of the cache with the provided list of items.

func (*Cache[T]) Update

func (c *Cache[T]) Update(obj T) error

Update updates an existing item in the cache.

type IndexNotFoundError

type IndexNotFoundError struct {
	IndexName string
}

IndexNotFoundError indicates that the requested index does not exist.

func (IndexNotFoundError) Error

func (e IndexNotFoundError) Error() string

Error implements the error interface.

type IndexerConflictError

type IndexerConflictError struct {
	IndexNames []string
}

IndexerConflictError indicates that one or more indexers being added already exist.

func (IndexerConflictError) Error

func (e IndexerConflictError) Error() string

Error implements the error interface.

type ItemStore

type ItemStore[T any] interface {
	Get(key string) *T
	Set(key string, obj T)
	Delete(key string)
	Values() []T
	Keys() []string
}

ItemStore is a simple key-value store for items of type T.

type KeyError

type KeyError[T any] struct {
	Obj T
	Err error
}

KeyError represents an error that occurred while generating a key for an object of type T.

func (KeyError[T]) Error

func (e KeyError[T]) Error() string

Error implements the error interface.

type KeyFunc

type KeyFunc[T any] func(obj T) (string, error)

KeyFunc is a function that takes an object of type T and returns a string key and an error if any.

type Storage

type Storage[T any] interface {
	Add(key string, obj T)
	Update(key string, obj T)
	Delete(key string)
	Get(key string) (item *T, exists bool)
	List() []T
	ListKeys() []string
	Replace(list map[string]T, resourceVersion string)
	Index(indexName string, obj T) ([]interface{}, error)
	IndexKeys(indexName, indexedValue string) ([]string, error)
	ListIndexFuncValues(name string) []string
	ByIndex(indexName, indexedValue string) ([]T, error)
	GetIndexers() port.Indexers[T]

	// AddIndexers adds more indexers to this store. This supports adding indexes after the store already has items.
	AddIndexers(newIndexers port.Indexers[T]) error
}

Storage is a generic interface for a storage backend that supports basic CRUD operations and indexing.

Jump to

Keyboard shortcuts

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