Skip to content

Improve bit Boilerplate caching configuration (#12184)#12185

Merged
yasmoradi merged 1 commit intobitfoundation:developfrom
yasmoradi:12184
Mar 17, 2026
Merged

Improve bit Boilerplate caching configuration (#12184)#12185
yasmoradi merged 1 commit intobitfoundation:developfrom
yasmoradi:12184

Conversation

@yasmoradi
Copy link
Copy Markdown
Member

@yasmoradi yasmoradi commented Mar 17, 2026

closes #12184

Summary

Added FusionCache's adapter for Microsoft's HybridCache, so developers/libraries/middlewares would be able to inject HybridCache

Added Microsoft's IDistributedCache support, so developers/libraries/middlewares would be able to inject IDistributedCache

Added Distributed Locker for distributed stampede protection

Re-use the same IMemoryCache instance for FusionCache's L1 cache

Note: Both IMemoryCache and MemoryDistributedCache configured to not to consume the whole server's memory.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 17, 2026

Walkthrough

FusionCache packages are upgraded to version 2.6.0 with the addition of distributed locking support via Redis. The caching infrastructure is enhanced by enabling hybrid cache mode, introducing IDistributedCache integration with Redis or in-memory fallback, and refactoring memory cache configuration. Lambda parameter names are standardized from opt to options across configuration methods.

Changes

Cohort / File(s) Summary
Package Dependencies
src/Templates/Boilerplate/.../Directory.Packages.props, src/Templates/Boilerplate/.../Boilerplate.Server.Shared.csproj
Upgraded FusionCache packages to 2.6.0 and added new ZiggyCreatures.FusionCache.Locking.Distributed.Redis package for distributed locking support.
Cache Configuration Implementation
src/Templates/Boilerplate/.../WebApplicationBuilderExtensions.cs, src/Templates/Boilerplate/.../ISharedServiceCollectionExtensions.cs
Enhanced FusionCache setup with hybrid cache mode, Redis-backed distributed locking, IDistributedCache integration with in-memory fallback. Refactored memory cache configuration to use Configure<MemoryCacheOptions>() pattern.
Documentation
.docs/15- Logging, OpenTelemetry and Health Checks.md
Minor lambda parameter rename from opt to options for consistency in health check configuration example.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant HC as HybridCache
    participant FC as FusionCache
    participant MC as IMemoryCache
    participant DL as RedisDistributedLocker
    participant DC as IDistributedCache
    participant Redis as Redis Backend

    App->>HC: Register HybridCache
    App->>FC: Configure FusionCache
    FC->>MC: Enable L1 Cache (in-memory)
    FC->>DL: Setup Distributed Locker
    DL->>Redis: Connect for locking
    FC->>DC: Register IDistributedCache
    DC->>Redis: Use Redis backend (if available)
    DC->>MC: Fallback to in-memory MemoryDistributedCache
    
    App->>HC: Request cached data
    HC->>FC: Forward to FusionCache
    FC->>MC: Check L1 (memory cache)
    alt Cache Hit
        MC-->>FC: Return cached value
    else Cache Miss
        FC->>DL: Acquire distributed lock
        DL->>Redis: Lock key
        FC->>DC: Fetch from distributed cache
        alt Redis available
            DC->>Redis: Get value
            Redis-->>DC: Return value
        else Redis unavailable
            DC->>MC: Get from in-memory fallback
            MC-->>DC: Return value
        end
        DC-->>FC: Return value
        FC->>MC: Update L1 cache
    end
    FC-->>HC: Return value
    HC-->>App: Return to application
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hops with joy through caching layers
Hybrid, distributed, locks so fair
Redis guards the stampede's way,
Memory keeps the speed at bay,
Cloning unity at last, hooray! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: improving caching configuration in the Bit Boilerplate template, which aligns with the primary objective of enhancing FusionCache setup with hybrid cache mode, distributed locking, and memory cache integration.
Linked Issues check ✅ Passed All coding objectives from issue #12184 are addressed: FusionCache hybrid cache mode enabled, IDistributedCache support added via Redis and in-memory fallback, distributed locking via RedisDistributedLocker implemented, IMemoryCache reused for FusionCache L1 cache, and both caches configured with memory limits.
Out of Scope Changes check ✅ Passed All changes are directly aligned with the linked issue objectives. Minor documentation updates and dependency version bumps support the caching configuration improvements; no extraneous modifications detected.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Bit.Boilerplate template’s caching setup to better integrate FusionCache with Microsoft’s caching abstractions (HybridCache/IDistributedCache) and improve stampede protection, aligning with issue #12184.

Changes:

  • Configure and register IMemoryCache in a way that can be reused by FusionCache’s L1 cache.
  • Configure FusionCache as HybridCache, add Redis distributed locking (when Redis is enabled), and register IDistributedCache for library compatibility.
  • Add a shared GetRequiredConnectionString helper and update FusionCache-related package references (including the distributed locker package).

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Infrastructure/Extensions/ISharedServiceCollectionExtensions.cs Binds MemoryCacheOptions from configuration and registers IMemoryCache separately.
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Infrastructure/Extensions/WebApplicationBuilderExtensions.cs Configures FusionCache as HybridCache, reuses registered IMemoryCache, adds Redis distributed locker, and registers IDistributedCache.
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Infrastructure/Extensions/IConfigurationExtensions.cs Adds GetRequiredConnectionString extension to enforce required connection strings.
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Boilerplate.Server.Shared.csproj Adds package reference for FusionCache Redis distributed locking.
src/Templates/Boilerplate/Bit.Boilerplate/src/Directory.Packages.props Bumps FusionCache packages to 2.6.0 and adds version for the distributed locker package.

You can also share your feedback on Copilot code review. Take the survey.

@yasmoradi yasmoradi merged commit 13256f1 into bitfoundation:develop Mar 17, 2026
7 checks passed
@yasmoradi yasmoradi deleted the 12184 branch March 17, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bit Boilerplate caching configuration needs improvements

2 participants