cache

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache caches the loaded flag configurations in memory for the evaluation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToFlagStruct

func ConvertToFlagStruct(
	loadedFlags []byte,
	fileFormat string,
) (map[string]dto.DTO, error)

ConvertToFlagStruct converts the flag configuration DTOs to internal flags.

Types

type Cache

type Cache interface {

	// Copy return a copy version of the cache.
	Copy() *InMemoryCache

	// All return the complete list of the flags, as deep copies the caller is free to change.
	All() map[string]flagdef.Flag

	// Init allow to initialize the cache with a collection of flags.
	Init(flags map[string]dto.DTO)
	// contains filtered or unexported methods
}

Cache is the interface to represent a cache in the system.

type InMemoryCache

type InMemoryCache struct {
	// Flags are the flags by key. A flag of the map is shared by every reader: it must be treated
	// as read-only once it is in the map.
	Flags  map[string]*flagdef.InternalFlag
	Logger *slog.Logger
	// Service validates the flags that are added, and keeps the parses of their queries.
	Service *flagdef.Service
}

InMemoryCache stores the flags in memory.

func NewInMemoryCache

func NewInMemoryCache(svc *flagdef.Service, logger *slog.Logger) *InMemoryCache

NewInMemoryCache creates an empty cache, which validates its flags with svc.

func (*InMemoryCache) All

func (fc *InMemoryCache) All() map[string]flagdef.Flag

All returns a snapshot of the flags: every flag is a deep copy of the cached one, so the caller can keep the map and change its flags, at any depth, without affecting the cache. The copy is not free: the evaluations read the flags with getFlag or shared, which do not copy.

func (*InMemoryCache) Copy

func (fc *InMemoryCache) Copy() *InMemoryCache

Copy returns a copy of the cache that holds the same flags.

func (*InMemoryCache) Init

func (fc *InMemoryCache) Init(flags map[string]dto.DTO)

Init replaces the cached flags with the given flags.

type Manager

type Manager interface {
	UpdateCache(newFlags map[string]dto.DTO, log *slog.Logger, notifyChanges bool) error
	Close()
	GetFlag(key string) (*flagdef.InternalFlag, error)
	// AllFlags returns a snapshot of the flags that the caller is free to change.
	AllFlags() (map[string]flagdef.Flag, error)
	// AllFlagsReadOnly returns the flags of the cache without copying them (cheap): the caller must only
	// read them, and must not keep them or hand them to code that does not.
	AllFlagsReadOnly() (map[string]flagdef.Flag, error)
	GetLatestUpdateDate() time.Time
}

Manager caches the flag configurations and gives access to the flags.

type ManagerImpl added in v0.2.0

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

ManagerImpl is the default Manager implementation, which keeps the flags in memory.

func New

func New(
	notificationService notification.Service,
	persistentFlagConfigurationFile string,
	logger *slog.Logger,
	opts ...Option,
) *ManagerImpl

New returns a cache manager configured with the given options.

func (*ManagerImpl) AllFlags added in v0.2.0

func (c *ManagerImpl) AllFlags() (map[string]flagdef.Flag, error)

AllFlags returns a copy of all the cached flags.

func (*ManagerImpl) AllFlagsReadOnly added in v0.2.0

func (c *ManagerImpl) AllFlagsReadOnly() (map[string]flagdef.Flag, error)

AllFlagsReadOnly returns all the cached flags without copying them, and callers must not modify them.

func (*ManagerImpl) Close added in v0.2.0

func (c *ManagerImpl) Close()

Close releases the resources of the cache manager.

func (*ManagerImpl) GetFlag added in v0.2.0

func (c *ManagerImpl) GetFlag(key string) (*flagdef.InternalFlag, error)

GetFlag returns the cached flag with the given key, or an error if the flag does not exist.

func (*ManagerImpl) GetLatestUpdateDate added in v0.2.0

func (c *ManagerImpl) GetLatestUpdateDate() time.Time

GetLatestUpdateDate returns the time of the last cache update.

func (*ManagerImpl) PersistCache added in v0.2.0

func (c *ManagerImpl) PersistCache(oldCache, newCache map[string]flagdef.Flag)

PersistCache is writing the flags to a file to be able to restart without being able to access the retrievers. It is useful to have a fallback in case of a problem with the retrievers, such as a network issue.

The persistence is done in a goroutine to not block the main thread.

func (*ManagerImpl) UpdateCache added in v0.2.0

func (c *ManagerImpl) UpdateCache(
	newFlags map[string]dto.DTO,
	log *slog.Logger,
	notifyChanges bool,
) error

UpdateCache replaces the cached flags with the flags of the new configuration.

type Option

type Option func(*ManagerImpl)

Option customizes the ManagerImpl that New creates.

func WithService

func WithService(svc *flagdef.Service) Option

WithService makes the manager validate its flags with svc, which the caller owns (and closes). By default the manager creates a Service of its own.

Jump to

Keyboard shortcuts

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