Tenon.BloomFilter.RedisStackExchange 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.BloomFilter.RedisStackExchange.
dotnet add package Tenon.BloomFilter.RedisStackExchange --version 0.0.1-alpha-202502101554                
NuGet\Install-Package Tenon.BloomFilter.RedisStackExchange -Version 0.0.1-alpha-202502101554                
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-202502101554" />                
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-202502101554                
#r "nuget: Tenon.BloomFilter.RedisStackExchange, 0.0.1-alpha-202502101554"                
#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-202502101554&prerelease

// Install Tenon.BloomFilter.RedisStackExchange as a Cake Tool
#tool nuget:?package=Tenon.BloomFilter.RedisStackExchange&version=0.0.1-alpha-202502101554&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-202502101554 32 2/10/2025
0.0.1-alpha-202502101448 33 2/10/2025
0.0.1-alpha-202502101434 35 2/10/2025
0.0.1-alpha-202501130258 36 1/13/2025
0.0.1-alpha-202412311524 74 12/31/2024
0.0.1-alpha-202412061617 59 12/6/2024
0.0.1-alpha-202412051527 53 12/5/2024
0.0.1-alpha-202412051431 50 12/5/2024
0.0.1-alpha-202412041445 49 12/4/2024
0.0.1-alpha-202412021409 49 12/2/2024
0.0.1-alpha-202411301019 53 11/30/2024
0.0.1-alpha-202411170525 56 11/17/2024
0.0.1-alpha-202411161308 50 11/16/2024
0.0.1-alpha-202411131604 52 11/13/2024
0.0.1-alpha-202411111439 67 11/11/2024
0.0.1-alpha-202411051434 55 11/5/2024
0.0.1-alpha-202410281339 58 10/28/2024
0.0.1-alpha-202410131500 59 10/13/2024
0.0.1-alpha-202407261457 66 7/26/2024
0.0.1-alpha-202407261325 62 7/26/2024
0.0.1-alpha-202406271301 64 6/27/2024
0.0.1-alpha-202406251508 64 6/25/2024
0.0.1-alpha-202406251310 61 6/25/2024
0.0.1-alpha-202406141611 67 6/14/2024
0.0.1-alpha-202406141550 67 6/14/2024
0.0.1-alpha-202406121515 70 6/12/2024
0.0.1-alpha-202406061553 67 6/6/2024
0.0.1-alpha-202406041519 64 6/4/2024
0.0.1-alpha-202406011613 68 6/1/2024
0.0.1-alpha-202406011238 60 6/1/2024
0.0.1-alpha-202405311458 65 5/31/2024
0.0.1-alpha-202405291213 74 5/29/2024
0.0.1-alpha-202405190457 64 5/19/2024
0.0.1-alpha-202405161229 67 5/16/2024
0.0.1-alpha-202405141510 56 5/14/2024
0.0.1-alpha-202405101323 67 5/10/2024
0.0.1-alpha-202405081356 70 5/8/2024
0.0.1-alpha-202405021337 30 5/2/2024
0.0.1-alpha-202405021336 29 5/2/2024
0.0.1-alpha-202405020452 46 5/2/2024
0.0.1-alpha-202405011443 49 5/1/2024
0.0.1-alpha-202404291541 61 4/29/2024
0.0.1-alpha-202404281218 64 4/28/2024