BugFree.Cache 1.2.2026.613-beta1618

This is a prerelease version of BugFree.Cache.
dotnet add package BugFree.Cache --version 1.2.2026.613-beta1618
                    
NuGet\Install-Package BugFree.Cache -Version 1.2.2026.613-beta1618
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BugFree.Cache" Version="1.2.2026.613-beta1618" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BugFree.Cache" Version="1.2.2026.613-beta1618" />
                    
Directory.Packages.props
<PackageReference Include="BugFree.Cache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BugFree.Cache --version 1.2.2026.613-beta1618
                    
#r "nuget: BugFree.Cache, 1.2.2026.613-beta1618"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BugFree.Cache@1.2.2026.613-beta1618
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BugFree.Cache&version=1.2.2026.613-beta1618&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=BugFree.Cache&version=1.2.2026.613-beta1618&prerelease
                    
Install as a Cake Tool

BugFree.Cache

介绍

BugFree.Cache 是一个基于 Microsoft HybridCache 的高级混合缓存库,在 HybridCache 基础上增加了 Fail-Safe(故障容错)、EagerRefresh(提前刷新)和 Factory Timeout(超时控制)等生产级缓存弹性能力。参考 FusionCache 的核心设计,为 .NET 应用提供可靠的缓存解决方案。

特性

  • 混合缓存架构:支持 L1(本地内存)+ L2(Redis 分布式)的混合缓存模式
  • Fail-Safe 故障容错:当数据源失败时返回已过期的旧值,避免故障传播到上游
  • EagerRefresh 提前刷新:在缓存项即将过期时后台提前刷新,提升用户体验
  • Factory 超时控制:软/硬超时控制,防止慢工厂方法阻塞请求
  • 基于 NewLife.Redis:使用高性能的 NewLife.Redis 作为 Redis 客户端
  • 多目标框架:支持 .NET 8.0 和 .NET 10.0
  • Fluent API:提供流畅的配置接口,易于使用

软件架构

┌─────────────────────────────────────────────────────────┐
│                    应用程序层                              │
├─────────────────────────────────────────────────────────┤
│               BugFreeHybridCache                         │
│  ┌─────────────┬─────────────┬─────────────┐            │
│  │ Fail-Safe   │EagerRefresh │  Timeout    │            │
│  │ 故障容错    │  提前刷新   │ 超时控制    │            │
│  └─────────────┴─────────────┴─────────────┘            │
├─────────────────────────────────────────────────────────┤
│              Microsoft HybridCache                       │
│  ┌─────────────────┐  ┌─────────────────┐               │
│  │ L1 本地缓存     │  │ L2 分布式缓存   │               │
│  │ (MemoryCache)   │  │ (Redis)         │               │
│  └─────────────────┘  └─────────────────┘               │
├─────────────────────────────────────────────────────────┤
│           NewLife.Redis (IDistributedCache)              │
└─────────────────────────────────────────────────────────┘

安装教程

  1. 通过 NuGet 安装 BugFree.Cache 包:
dotnet add package BugFree.Cache
  1. 或者在项目文件中添加包引用:
<PackageReference Include="BugFree.Cache" Version="*" />

使用说明

基本使用

// 在 Program.cs 中注册服务
services.AddBugFreeHybridCache();

// 在控制器或服务中使用
public class MyService
{
    private readonly BugFreeHybridCache _cache;
    
    public MyService(BugFreeHybridCache cache)
    {
        _cache = cache;
    }
    
    public async Task<User> GetUserAsync(int userId)
    {
        return await _cache.GetOrSetAsync(
            $"user:{userId}",
            async ct => await _userRepository.GetByIdAsync(userId, ct),
            opts => opts.SetDuration(TimeSpan.FromMinutes(10))
        );
    }
}

高级配置

services.AddBugFreeHybridCache(
    bfOptions: options =>
    {
        options.DefaultEntryOptions.Duration = TimeSpan.FromMinutes(5);
        options.DefaultEntryOptions.IsFailSafeEnabled = true;
        options.DefaultEntryOptions.FailSafeMaxDuration = TimeSpan.FromHours(1);
    },
    hcOptions: options =>
    {
        options.DefaultEntryOptions.Duration = TimeSpan.FromMinutes(5);
    },
    rdsOptions: options =>
    {
        options.Server = "localhost:6379";
        options.Database = 0;
    }
);

Fail-Safe 使用示例

// 启用 Fail-Safe,当数据源失败时返回过期旧值
var result = await _cache.GetOrSetAsync(
    "critical-data",
    async ct => await GetCriticalDataFromDatabaseAsync(ct),
    opts => opts.SetDuration(TimeSpan.FromMinutes(5))
                 .SetFailSafe(true, TimeSpan.FromHours(1))
);

EagerRefresh 使用示例

// 在缓存过期前 80% 时间后台提前刷新
var result = await _cache.GetOrSetAsync(
    "frequently-accessed-data",
    async ct => await ExpensiveDataOperationAsync(ct),
    opts => opts.SetDuration(TimeSpan.FromMinutes(10))
                 .SetEagerRefresh(0.8)
);

Factory 超时控制

// 设置软超时 2 秒,硬超时 5 秒
var result = await _cache.GetOrSetAsync(
    "data-with-timeout",
    async ct => await SlowDataOperationAsync(ct),
    opts => opts.SetDuration(TimeSpan.FromMinutes(5))
                 .SetFactoryTimeouts(
                     TimeSpan.FromSeconds(2),
                     TimeSpan.FromSeconds(5))
);

配置选项

BugFreeCacheEntryOptions

属性 类型 默认值 说明
Duration TimeSpan 5 分钟 缓存 Duration(L1+L2 整体过期时间)
LocalCacheDuration TimeSpan? null 本地缓存 Duration(L1 独立过期时间)
IsFailSafeEnabled bool false 是否启用 Fail-Safe 故障容错
FailSafeMaxDuration TimeSpan? null Fail-Safe 最大存留时间
FailSafeThrottleDuration TimeSpan? null Fail-Safe 激活后的限流时间
EagerRefreshThreshold double? null 提前刷新阈值(0~1,如 0.8 表示 80%)
FactorySoftTimeout TimeSpan? null Factory 软超时
FactoryHardTimeout TimeSpan? null Factory 硬超时

BugFreeHybridCacheOptions

属性 类型 默认值 说明
DefaultEntryOptions BugFreeCacheEntryOptions 见上表 默认缓存项选项
FailSafeKeyPrefix string "bc:fs:" Fail-Safe 过期值存储的键前缀
EagerRefreshMaxConcurrency int CPU 核心数 * 2 Eager Refresh 最大并发后台刷新数

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

许可证

本项目使用 MIT 许可证,详见 LICENSE 文件。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BugFree.Cache:

Package Downloads
BugFree.Controllers.Core

BugFree 控制器核心框架,提供统一的 API/MVC 控制器基类和实体 CRUD 控制器。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.2026.613-beta1618 0 6/13/2026