cache library
ποΈ JetLeaf Caching Library
This library provides a comprehensive caching system for JetLeaf applications, including annotations, storage, eviction policies, operations, metrics, and error handling.
It supports declarative caching with method interceptors, programmatic cache management, and extensible backends.
π Key Concepts
- Cache Operations: define actions like
put,evict,expire, andcacheable. - Cache Storage: backends for storing cached data (in-memory, configurable stores, etc.).
- Eviction Policies: LRU, LFU, FIFO strategies for automatic cache pruning.
- Error Handling: robust handling of cache errors with pluggable handlers.
- Events & Metrics: observe cache activity and gather statistics.
π¦ Exports Overview
β Core
CacheAnnotationMethodInterceptorβ intercepts annotated methods for cachingCacheComponentRegistrarβ registers cache componentsCacheOperationContext/DefaultCacheOperationContextβ runtime operation metadata
β Error Handlers
CacheErrorHandlerβ base interfaceCacheErrorHandlerRegistryβ manages multiple handlersLoggableCacheErrorHandlerβ logs errors instead of throwingThrowableCacheErrorHandlerβ propagates exceptions
π· Events
CacheEventβ base classCacheHitEvent,CacheMissEvent,CachePutEvent,CacheEvictEvent,CacheExpireEvent,CacheClearEvent
Allows observing cache lifecycle activities.
π Eviction Policies
CacheEvictionPolicyβ base interfaceFifoEvictionPolicy,LfuEvictionPolicy,LruEvictionPolicy
π Managers
CacheManagerβ primary cache orchestratorCacheManagerRegistryβ registry for multiple managersSimpleCacheManagerβ default implementation
π Metrics
CacheMetricsβ metrics collection interfaceSimpleCacheMetricsβ basic implementation for monitoring
πΎ Cache Operations
CacheOperationβ base interface for all operationsCachePutOperation,CacheEvictOperation,CacheableOperation
π Resolver
CacheResolverβ resolves cache targets dynamicallyCacheResolverRegistryβ manages multiple resolversSimpleCacheResolverβ default implementation
π Storage
CacheStorageβ interface for cache storesCacheStorageRegistryβ manage multiple storesCache/DefaultCacheβ standard cache abstractionConfigurableCacheStorageβ customizable backendsCacheResourceβ resource abstractionDefaultCacheStorageβ default in-memory storage
π Annotations & Config
annotations.dartβ declarative caching via method-level annotationsCacheConfigurerβ programmatic configuration of caches
π― Intended Usage
Import this library to enable full caching capabilities in JetLeaf:
import 'package:jetleaf_resource/cache.dart';
@Cacheable('myCache')
String fetchData(String key) {
return computeData(key);
}
Supports pluggable storage, metrics, events, and error handling.
Β© 2025 Hapnium & JetLeaf Contributors
Classes
- Cache
- Represents a single cache entry, encapsulating both the cached value and its associated lifecycle metadata.
- Cacheable
- Marks a method as cacheable β its result will be stored in one or more caches.
- CacheableOperation
-
Represents the execution logic for a
@Cacheablemethod within the JetLeaf resource management framework. - CacheAnnotationMethodInterceptor
-
A composite
ConditionalMethodInterceptorthat transparently applies caching behavior based on JetLeaf cache annotations such as Cacheable, CachePut, and CacheEvict. - CacheClearEvent
- Event published when one or more entries in a cache are cleared.
- CacheComponentRegistrar
- Central entry point for JetLeaf cache subsystem initialization and configuration registration.
- CacheConfigurer
- Contract for JetLeaf cache configuration contributors.
- CacheErrorHandler
- A strategy interface for handling exceptions that occur during cache operations.
- CacheErrorHandlerRegistry
- A central registry for all cache-related infrastructure components.
- CacheEvent
- Base class representing an event related to a cache operation.
- CacheEvict
- Marks a method that triggers the removal (or invalidation) of cache entries.
- CacheEvictEvent
- Event published when an entry is removed (evicted) from a cache.
- CacheEvictionPolicy
- Represents a cache eviction strategy for determining which cache entry should be removed when the cache reaches capacity or when specific conditions are met.
- CacheEvictOperation
-
Represents the execution logic for a
@CacheEvictmethod within the JetLeaf caching subsystem. - CacheExpireEvent
- Event published when a cache entry expires due to reaching its TTL (time-to-live).
- CacheHitEvent
- Event published when a cached value is successfully retrieved.
- CacheManager
- Manages multiple CacheStorage instances and provides lookup by name.
- CacheManagerRegistry
- A registry contract for managing multiple CacheManager pods within JetLeaf.
- CacheMetrics
- An abstract interface class for collecting and reporting cache-related metrics.
- CacheMissEvent
- Event published when a cache lookup fails to find a value.
- CacheOperation
- Defines the contract for a cache operation within the JetLeaf caching subsystem.
-
CacheOperationContext<
T> - Defines the execution context for a cache operation within JetLeafβs caching subsystem.
- CachePut
- Marks a method whose result should always be stored (or updated) in the cache.
- CachePutEvent
- Event published when a value is stored in a cache.
- CachePutOperation
-
Represents the execution logic for a
@CachePutmethod within the JetLeaf caching subsystem. - CacheResolver
- Strategy interface for resolving one or more CacheStorage instances associated with a particular Cacheable operation.
- CacheResolverRegistry
- A registry contract for managing multiple CacheResolver pods within JetLeaf.
- CacheResource
-
A concurrent in-memory cache resource based on
HashMap, implementing theResourceinterface. - CacheStorage
- The core abstraction representing a single named cache.
- CacheStorageRegistry
- Defines a registry contract for managing CacheStorage instances.
- ConfigurableCacheStorage
- Defines configuration capabilities for a cache storage implementation.
- DefaultCache
- Represents a single cache entry's stored value and its associated metadata.
-
DefaultCacheOperationContext<
T> -
The default implementation of CacheOperationContext, providing a
comprehensive execution context for cache-related operations such as
@Cacheable,@CachePut, and@CacheEvict. - DefaultCacheStorage
- A thread-safe, in-memory cache implementation backed by a concurrent CacheResource.
- FifoEvictionPolicy
- A First-In-First-Out (FIFO) cache eviction policy that removes the oldest entry from the cache when eviction is necessary.
- LfuEvictionPolicy
- A Least-Frequently-Used (LFU) cache eviction policy for managing cache memory efficiently by evicting the entries that are accessed least often.
- LoggableCacheErrorHandler
- A CacheErrorHandler implementation that logs all cache operation failures.
- LruEvictionPolicy
- A Least-Recently-Used (LRU) cache eviction policy that removes the cache entry that has not been accessed for the longest time when eviction is required.
- SimpleCacheManager
- A composite registry and coordinator for multiple CacheManager implementations within JetLeaf.
- SimpleCacheMetrics
- Tracks and aggregates statistics related to cache operations for a specific cache instance.
- SimpleCacheResolver
- Default JetLeaf implementation of the CacheResolver interface, providing a simple yet extensible mechanism for resolving Cacheable annotations to their corresponding CacheStorage instances.
- ThrowableCacheErrorHandler
- A strict CacheErrorHandler implementation that immediately rethrows any exception encountered during cache operations.
Typedefs
-
CacheStorageCreator
= FutureOr<
CacheStorage?> Function(String name) - A factory function capable of creating a CacheStorage dynamically when the registry cannot find a storage by name.