Tenon.BloomFilter.RedisStackExchange 0.0.1-alpha-202502241449

This is a prerelease version of Tenon.BloomFilter.RedisStackExchange.
dotnet add package Tenon.BloomFilter.RedisStackExchange --version 0.0.1-alpha-202502241449                
NuGet\Install-Package Tenon.BloomFilter.RedisStackExchange -Version 0.0.1-alpha-202502241449                
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="Tenon.BloomFilter.RedisStackExchange" Version="0.0.1-alpha-202502241449" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tenon.BloomFilter.RedisStackExchange --version 0.0.1-alpha-202502241449                
#r "nuget: Tenon.BloomFilter.RedisStackExchange, 0.0.1-alpha-202502241449"                
#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.
// Install Tenon.BloomFilter.RedisStackExchange as a Cake Addin
#addin nuget:?package=Tenon.BloomFilter.RedisStackExchange&version=0.0.1-alpha-202502241449&prerelease

// Install Tenon.BloomFilter.RedisStackExchange as a Cake Tool
#tool nuget:?package=Tenon.BloomFilter.RedisStackExchange&version=0.0.1-alpha-202502241449&prerelease                

Tenon.BloomFilter.RedisStackExchange

NuGet version License: MIT

基于 StackExchange.Redis 的高性能布隆过滤器实现,为 .NET 应用程序提供分布式布隆过滤器解决方案。

✨ 功能特性

  • 🚀 基于 StackExchange.Redis 的高性能实现
  • 🔧 支持自定义配置选项
  • 💉 集成依赖注入框架
  • 🎯 统一的 IBloomFilter 接口
  • 🔄 支持命名服务注入
  • 📊 完整的单元测试覆盖
  • 🛡️ 异常重试和容错处理

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.BloomFilter.RedisStackExchange

🚀 快速入门

1. 配置 appsettings.json

{
  "RedisBloomFilter": {
    "Redis": {
      "ConnectionString": "localhost:6379,defaultDatabase=0,connectTimeout=4000,allowAdmin=true,abortConnect=false,syncTimeout=5000"
    }
  }
}

2. 注册服务

// 使用默认配置
services.AddRedisStackExchangeBloomFilter(
    configuration.GetSection("RedisBloomFilter:Redis"),
    options => 
    {
        options.Name = "UserFilter";
        options.Capacity = 1_000_000;  // 预期存储100万个元素
        options.ErrorRate = 0.01;      // 误判率为1%
    });

// 或使用命名服务
services.AddKeyedRedisStackExchangeBloomFilter(
    "CustomFilter",
    configuration.GetSection("RedisBloomFilter:Redis"),
    options => 
    {
        // 自定义配置选项
    });

3. 使用布隆过滤器

public class UserService
{
    private readonly IBloomFilter _filter;

    public UserService(IBloomFilter filter)
    {
        _filter = filter;
    }

    public async Task<bool> TryRegisterUserAsync(string userId)
    {
        // 检查用户ID是否已存在
        if (await _filter.ExistsAsync(userId))
            return false;  // 用户可能已存在

        // 注册新用户
        await _repository.CreateUserAsync(userId);
        
        // 添加到布隆过滤器
        await _filter.AddAsync(userId);
        
        return true;
    }
}

📖 高级用法

批量操作处理

public class ProductService
{
    private readonly IBloomFilter _filter;

    public async Task ImportProductsAsync(List<string> productIds)
    {
        // 批量检查商品是否存在
        var exists = await _filter.ExistsAsync(productIds);
        
        // 过滤出不存在的商品
        var newProducts = productIds
            .Where((id, index) => !exists[index])
            .ToList();
            
        if (newProducts.Any())
        {
            // 批量导入新商品
            await _repository.ImportProductsAsync(newProducts);
            
            // 批量添加到布隆过滤器
            await _filter.AddAsync(newProducts);
        }
    }
}

多过滤器管理

public class FilterManager
{
    private readonly IBloomFilter _userFilter;
    private readonly IBloomFilter _productFilter;

    public FilterManager(
        IBloomFilter userFilter,
        [FromKeyedServices("ProductFilter")] IBloomFilter productFilter)
    {
        _userFilter = userFilter;
        _productFilter = productFilter;
    }

    public async Task<bool> ValidateDataAsync(string userId, string productId)
    {
        var tasks = new[]
        {
            _userFilter.ExistsAsync(userId),
            _productFilter.ExistsAsync(productId)
        };

        var results = await Task.WhenAll(tasks);
        return results[0] && results[1];
    }
}

⚙️ 配置选项说明

Redis 连接配置

配置项 说明 示例值
ConnectionString Redis 连接字符串 localhost:6379
DefaultDatabase 默认数据库编号 0
ConnectTimeout 连接超时时间(ms) 4000
SyncTimeout 同步操作超时时间(ms) 5000

布隆过滤器选项

配置项 说明 说明
Name 过滤器名称 用于区分不同过滤器
Capacity 预期元素数量 影响内存使用和性能
ErrorRate 误判率 越小内存占用越大

🔨 项目依赖

  • StackExchange.Redis
  • Tenon.BloomFilter.Abstractions
  • Tenon.BloomFilter.Redis
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.DependencyInjection

📝 使用注意事项

1. 性能优化

  • 合理设置预期容量
  • 使用批量操作减少网络请求
  • 注意内存使用效率

2. 可靠性保证

  • 实现异常重试机制
  • 监控误判率变化
  • 定期维护过滤器

3. 最佳实践

  • 根据业务场景选择合适的误判率
  • 预留足够的容量空间
  • 实现监控和告警机制

🌰 应用场景示例

1. 注册查重

public class RegistrationService
{
    private readonly IBloomFilter _filter;

    public async Task<bool> IsUserIdAvailableAsync(string userId)
    {
        // 布隆过滤器返回 false 表示用户ID一定不存在
        // 返回 true 表示可能存在,需要进一步查询数据库确认
        return !await _filter.ExistsAsync(userId);
    }
}

2. 缓存穿透防护

public class CacheService
{
    private readonly IBloomFilter _filter;
    private readonly ICache _cache;

    public async Task<Product> GetProductAsync(string productId)
    {
        // 首先检查布隆过滤器
        if (!await _filter.ExistsAsync(productId))
            return null;  // 商品一定不存在
            
        // 检查缓存
        var product = await _cache.GetAsync<Product>(productId);
        if (product != null)
            return product;
            
        // 查询数据库
        product = await _repository.GetProductAsync(productId);
        if (product != null)
        {
            await _cache.SetAsync(productId, product);
            await _filter.AddAsync(productId);
        }
        
        return product;
    }
}

🤝 参与贡献

欢迎参与项目贡献!请阅读我们的贡献指南了解如何参与项目开发。

📄 开源协议

本项目采用 MIT 开源协议 - 详情请查看 LICENSE 文件。

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.1-alpha-202502241449 49 2/24/2025
0.0.1-alpha-202502101554 56 2/10/2025
0.0.1-alpha-202502101448 56 2/10/2025
0.0.1-alpha-202502101434 62 2/10/2025
0.0.1-alpha-202501130258 43 1/13/2025
0.0.1-alpha-202412311524 75 12/31/2024
0.0.1-alpha-202412061617 60 12/6/2024
0.0.1-alpha-202412051527 54 12/5/2024
0.0.1-alpha-202412051431 51 12/5/2024
0.0.1-alpha-202412041445 50 12/4/2024
0.0.1-alpha-202412021409 50 12/2/2024
0.0.1-alpha-202411301019 56 11/30/2024
0.0.1-alpha-202411170525 58 11/17/2024
0.0.1-alpha-202411161308 52 11/16/2024
0.0.1-alpha-202411131604 54 11/13/2024
0.0.1-alpha-202411111439 69 11/11/2024
0.0.1-alpha-202411051434 56 11/5/2024
0.0.1-alpha-202410281339 59 10/28/2024
0.0.1-alpha-202410131500 60 10/13/2024
0.0.1-alpha-202407261457 67 7/26/2024
0.0.1-alpha-202407261325 63 7/26/2024
0.0.1-alpha-202406271301 65 6/27/2024
0.0.1-alpha-202406251508 65 6/25/2024
0.0.1-alpha-202406251310 62 6/25/2024
0.0.1-alpha-202406141611 68 6/14/2024
0.0.1-alpha-202406141550 68 6/14/2024
0.0.1-alpha-202406121515 71 6/12/2024
0.0.1-alpha-202406061553 68 6/6/2024
0.0.1-alpha-202406041519 65 6/4/2024
0.0.1-alpha-202406011613 69 6/1/2024
0.0.1-alpha-202406011238 61 6/1/2024
0.0.1-alpha-202405311458 66 5/31/2024
0.0.1-alpha-202405291213 75 5/29/2024
0.0.1-alpha-202405190457 65 5/19/2024
0.0.1-alpha-202405161229 68 5/16/2024
0.0.1-alpha-202405141510 57 5/14/2024
0.0.1-alpha-202405101323 68 5/10/2024
0.0.1-alpha-202405081356 71 5/8/2024
0.0.1-alpha-202405021337 31 5/2/2024
0.0.1-alpha-202405021336 30 5/2/2024
0.0.1-alpha-202405020452 47 5/2/2024
0.0.1-alpha-202405011443 50 5/1/2024
0.0.1-alpha-202404291541 62 4/29/2024
0.0.1-alpha-202404281218 65 4/28/2024