cache

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoEntry     = errors.New("no matching cache entry")
	ErrInvalidType = errors.New("cache entry value cannot be converted to requested type")
)

Functions

func NewOverwriteError

func NewOverwriteError(existing *CacheResult) error

NewOverwriteError returns a new overwrite error, specifying the existing value in the cache that generated the error.

Types

type AsyncWriter

type AsyncWriter interface {
	// Queue add a model to an asynchronous write queue. Non-blocking.
	Queue(ctx context.Context, key cachekey.CacheKey, value any) error

	// Flush triggers writes of any remaining items in the queue. Blocks until operation completes
	Flush(ctx context.Context) error

	// Close cleans up any resources used by the AsyncWriter implementation. Writer cannot be reused after this call.
	Close(ctx context.Context) error
}

AysncWriter defines the interface for writer clients to queue aysnchronous, batched writes to the cache.

type CacheDriver

type CacheDriver interface {
	services.Dependency

	// Close cleans up any resources used by the CacheDriver implementation. CacheDriver cannot be reused after this call.
	Close(ctx context.Context) error
}

CacheDriver defines the interface for implementations of the cache provider for intermediate caching of K8s relationship data.

type CacheProvider

type CacheProvider interface {
	CacheReader

	// BulkWriter creates a new AsyncWriter instance to enable asynchronous bulk inserts.
	BulkWriter(ctx context.Context, opts ...WriterOption) (AsyncWriter, error)

	// Prepare drops all data from the cache (usually to ensure a clean start).
	Prepare(ctx context.Context) error
}

CacheProvider defines the interface for reading and writing data from the cache provider.

func Factory

Factory returns an initialized instance of a cache provider from the provided application config.

type CacheReader

type CacheReader interface {
	CacheDriver

	// Get fetches an entry from the cache for the provided cache key.
	Get(ctx context.Context, key cachekey.CacheKey) *CacheResult
}

CacheReader defines the interface for reading data from the cache provider.

type CacheResult

type CacheResult struct {
	Value any
	Err   error
}

CacheResult provides syntactic sugar around retrieval and type casting of entries from the cache.

func (*CacheResult) Bool

func (r *CacheResult) Bool() (bool, error)

Bool returns the result value as a boolean alongside any errors.

func (*CacheResult) Int64

func (r *CacheResult) Int64() (int64, error)

Int64 returns the result value as a int64 alongside any errors.

func (*CacheResult) ObjectID

func (r *CacheResult) ObjectID() (primitive.ObjectID, error)

ObjectID returns the result value as a bson ObjectID alongside any errors.

func (*CacheResult) Role

func (r *CacheResult) Role() (*store.Role, error)

Text returns the result value as a string alongside any errors.

func (*CacheResult) Text

func (r *CacheResult) Text() (string, error)

Text returns the result value as a string alongside any errors.

type MemCacheAsyncWriter

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

func (*MemCacheAsyncWriter) Close

func (*MemCacheAsyncWriter) Flush

func (*MemCacheAsyncWriter) Queue

func (m *MemCacheAsyncWriter) Queue(ctx context.Context, key cachekey.CacheKey, value any) error

To support the object writing, we will need at least redis 4 which support the HSET function

type MemCacheProvider

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

func NewMemCacheProvider

func NewMemCacheProvider(ctx context.Context) (*MemCacheProvider, error)

NewMemCacheProvider returns a new cache provider based on a simple in-memory map.

func (*MemCacheProvider) BulkWriter

func (m *MemCacheProvider) BulkWriter(ctx context.Context, opts ...WriterOption) (AsyncWriter, error)

func (*MemCacheProvider) Close

func (m *MemCacheProvider) Close(ctx context.Context) error

func (*MemCacheProvider) Get

func (*MemCacheProvider) HealthCheck

func (m *MemCacheProvider) HealthCheck(ctx context.Context) (bool, error)

func (*MemCacheProvider) Name

func (mp *MemCacheProvider) Name() string

func (*MemCacheProvider) Prepare added in v1.3.1

func (m *MemCacheProvider) Prepare(ctx context.Context) error

type OverwriteError

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

OverwriteError is a custom error type used by the cache implementation is an overwrite occurs when the WithTest option is set.

func (OverwriteError) Error

func (e OverwriteError) Error() string

Error implements the standard error interface.

func (OverwriteError) Existing

func (e OverwriteError) Existing() *CacheResult

Existing returns the existing value in the cache that generated the error.

type WriterOption

type WriterOption func(*writerOptions)

func WithExpectedOverwrite

func WithExpectedOverwrite() WriterOption

WithExpectedOverwrite signals that overwriting values is expected and suppresses logs & metrics generation. Mutually exclusive with WithTest.

func WithTest

func WithTest() WriterOption

Perform a test and set operation on writes. Only set the value if it does not currently exist. If the value does exist, return an ErrCacheEntryOverwrite error. Mutually exclusive with WithExpectedOverwrite.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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