How ziggycreatures/fusioncache works
Based on both the file tree and the README, FusionCache is a modular, hybrid caching library intended to provide a fast, resilient, multi-level caching solution for .NET applications. It’s not a typical full-stack web application; instead, it is a library (with multiple supporting modules) that provides both in-memory (L1) and distributed (L2) caching, along with resiliency features such as cache stampede protection, fail-safe mechanisms, auto-recovery, and even diagnostic/observability integrations like OpenTelemetry and structured logging.
Main Components
• Core Cache Engine: Represent the FusionCache core (located in the “src/ZiggyCreatures.FusionCache” directory). This component encapsulates the main API (GetOrSet, builder extensions, internal caching logic) and is responsible for coordinating cache read/write, fallback strategies, and resiliency.
• Caching Levels:
– L1 (Memory Cache): The in-process memory cache that quickly serves repeated requests.
– L2 (Distributed Cache): An optional distributed cache, via any implementation of the IDistributedCache interface, to allow for remote fallback and caching on multiple nodes.
• Backplane Components: These are used for synchronizing caches between nodes. There are separate backplane implementations such as:
– MemoryBackplane (src/ZiggyCreatures.FusionCache.Backplane.Memory)
– RedisBackplane (src/ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis)
• Supporting Modules and Plugins:
– Resiliency Modules (fail-safe, auto-recovery, cache stampede protection – implemented in the core as well as in helper classes under the Internals folder).
– Serialization Modules: Multiple serialization projects are provided (e.g. NewtonsoftJson, SystemTextJson, ProtoBufNet, CysharpMemoryPack, ServiceStackJson). These define how the entries are stored/transmitted especially when using distributed caching.
– Observability: The OpenTelemetry module (src/ZiggyCreatures.FusionCache.OpenTelemetry) and events/logging systems support metrics and diagnostics.
– ASP.NET Core Integration: A dedicated project for output caching integration (src/ZiggyCreatures.FusionCache.AspNetCore.OutputCaching) which is used for web scenarios, indicating that FusionCache can plug into web frameworks.
– Chaos Module: (src/ZiggyCreatures.FusionCache.Chaos) used for testing resilience by simulating failures or latency.
– Microsoft HybridCache integration: This module shows that FusionCache “talks” to Microsoft’s own caching abstractions.
• Additional utilities and extension points:
– Dependency Injection and Builder Extensions: Illustrates how FusionCache is configured and integrated within the host application.
– Plugins: Custom plug-in mechanisms (provided in the Plugins folder) that allow extension of core functionality, like additional metrics or custom behaviors.
• External Interactions:
– Integration points with standard external services such as IDistributedCache implementations, external data sources for factory methods, and potentially other logging/monitoring infrastructures.
Relationships and Data Flow
• The core caching component sits centrally. It directly communicates to the L1 memory cache for the fastest operations.
• When cache misses occur in the L1, it falls back to the L2 provider (if configured).
• If the cache is used across multiple nodes, a backplane (either in-memory for testing or Redis for production) is used to propagate cache invalidation or refresh events across nodes.
• The resiliency and fail-safe aspects intercept calls to the data source (factory method) and add circuit-breaker and timeout logic.
• Serializer modules interact with both the L2 caching layer and the backplane, converting cache entries to a transferable format.
• The dependency injection and builder extensions play a role in wiring up these components so that the user (or host application) doesn’t manually instantiate all the pieces.
• Observability modules (logging, OpenTelemetry instrumentation) hook into the core operations to propagate metrics and events.
Architectural Patterns & Design Principles
• Multi-Level (Hybrid) Caching: A pattern where an L1 fast cache is supplemented by an optional L2 distributed cache.
• Pluggability/Modularity: Separate serializer, backplane, and integration modules are self-contained and can be swapped out.
• Resiliency Strategy: With fail-safe, cache stampede protection, and auto-recovery to ensure service continuity.
• Dependency Injection & Fluent Builder Pattern: The design promotes configuring the core using DI and fluent interface extensions.
• Observability: Instrumentation is baked in using standardized interfaces such as ILogger and OpenTelemetry extensions, making the system’s internals transparent.
Conclusion
• At the center, the FusionCache core that orchestrates cache operations.
• Clearly delineated L1 (memory) and L2 (distributed) caching layers with fallback mechanisms.
• Side-by-side components for resiliency (fail-safe, auto-recovery, cache stampede protection), backplane synchronization, and observability.
• Separate modules for serializer implementations, ASP.NET output caching, and experimental chaos handling.
• Directional flows and interactions that emphasize how caching requests propagate and how configurations and plugins extend the core functionalities.