Skip to main content

New

Creates a new cache with the specified name, type, factory, load function, and Prometheus scope. It supports in-memory fixed-size caches or Redis-backed caches, optionally wrapping them with metric collection and marshaling capabilities (ProtoBuf or MsgPack) before returning a loadable or namespaced cache.

def New(
name: string,
cacheType: Type,
f: Factory,
loadFunc: cache.LoadFunction[T],
scope: promutils.Scope
) - > cache.CacheInterface[T], error

Creates a new cache with the given name and load function.

Parameters

NameTypeDescription
namestringThe name of the cache, used for scoping Prometheus metrics and for namespacing the cache.
cacheTypeTypeThe type of cache to create, determining the underlying storage mechanism (e.g., in-memory fixed size or Redis).
fFactoryA factory object providing the necessary components for creating cache stores, such as in-memory and Redis cache implementations.
loadFunccache.LoadFunction[T]An optional function that will be called to load data into the cache if a requested key is not found. If nil, the cache will not be loadable.
scopepromutils.ScopeThe Prometheus scope to use for registering metrics related to the cache's performance and usage.

Returns

TypeDescription
cache.CacheInterface[T], errorA new cache instance configured with the specified type, marshaler, and an optional load function, or an error if the cache cannot be created.