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, and cacheable.
  • 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 caching
  • CacheComponentRegistrar β€” registers cache components
  • CacheOperationContext / DefaultCacheOperationContext β€” runtime operation metadata

⚠ Error Handlers

  • CacheErrorHandler β€” base interface
  • CacheErrorHandlerRegistry β€” manages multiple handlers
  • LoggableCacheErrorHandler β€” logs errors instead of throwing
  • ThrowableCacheErrorHandler β€” propagates exceptions

🏷 Events

  • CacheEvent β€” base class
  • CacheHitEvent, CacheMissEvent, CachePutEvent, CacheEvictEvent, CacheExpireEvent, CacheClearEvent
    Allows observing cache lifecycle activities.

πŸ—‘ Eviction Policies

  • CacheEvictionPolicy β€” base interface
  • FifoEvictionPolicy, LfuEvictionPolicy, LruEvictionPolicy

πŸ— Managers

  • CacheManager β€” primary cache orchestrator
  • CacheManagerRegistry β€” registry for multiple managers
  • SimpleCacheManager β€” default implementation

πŸ“Š Metrics

  • CacheMetrics β€” metrics collection interface
  • SimpleCacheMetrics β€” basic implementation for monitoring

πŸ’Ύ Cache Operations

  • CacheOperation β€” base interface for all operations
  • CachePutOperation, CacheEvictOperation, CacheableOperation

πŸ” Resolver

  • CacheResolver β€” resolves cache targets dynamically
  • CacheResolverRegistry β€” manages multiple resolvers
  • SimpleCacheResolver β€” default implementation

πŸ—„ Storage

  • CacheStorage β€” interface for cache stores
  • CacheStorageRegistry β€” manage multiple stores
  • Cache / DefaultCache β€” standard cache abstraction
  • ConfigurableCacheStorage β€” customizable backends
  • CacheResource β€” resource abstraction
  • DefaultCacheStorage β€” default in-memory storage

πŸ“ Annotations & Config

  • annotations.dart β€” declarative caching via method-level annotations
  • CacheConfigurer β€” 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 @Cacheable method within the JetLeaf resource management framework.
CacheAnnotationMethodInterceptor
A composite ConditionalMethodInterceptor that 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 @CacheEvict method 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 @CachePut method 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 the Resource interface.
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.