Tenon.BloomFilter.Redis 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.BloomFilter.Redis.
dotnet add package Tenon.BloomFilter.Redis --version 0.0.1-alpha-202502101554                
NuGet\Install-Package Tenon.BloomFilter.Redis -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.Redis" 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.Redis --version 0.0.1-alpha-202502101554                
#r "nuget: Tenon.BloomFilter.Redis, 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.Redis as a Cake Addin
#addin nuget:?package=Tenon.BloomFilter.Redis&version=0.0.1-alpha-202502101554&prerelease

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

Tenon.BloomFilter.Redis

NuGet version License: MIT

Tenon.BloomFilter.Redis 是一个 Redis 布隆过滤器抽象层实现,为 Tenon 框架提供统一的 Redis 布隆过滤器接口和基础实现。

✨ 功能特性

  • 🚀 轻量级 Redis 布隆过滤器抽象实现
  • 🔧 统一的过滤器接口定义
  • 💉 支持多种 Redis 客户端实现
  • 🎯 完整的过滤器操作支持
  • 🔄 灵活的配置选项
  • 📊 可扩展的过滤器提供者
  • 🛡️ 内置异常处理机制

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.BloomFilter.Redis

🚀 快速入门

1. 实现布隆过滤器提供者

public class CustomRedisBloomFilter : RedisBloomFilter
{
    public CustomRedisBloomFilter(
        IRedisProvider redisProvider,
        BloomFilterOptions options) 
        : base(redisProvider, options)
    {
    }

    // 可以在这里扩展或重写基类方法
    public override Task<bool> AddAsync(string value)
    {
        // 自定义实现
        return base.AddAsync(value);
    }
}

2. 注册服务

services.AddSingleton<IBloomFilter>(sp => 
{
    var redisProvider = sp.GetRequiredService<IRedisProvider>();
    var options = new BloomFilterOptions
    {
        Name = "CustomFilter",
        Capacity = 1_000_000,
        ErrorRate = 0.01
    };
    
    return new CustomRedisBloomFilter(
        redisProvider, 
        options);
});

3. 使用布隆过滤器

public class FilterService
{
    private readonly IBloomFilter _filter;

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

    public async Task<bool> IsValueUniqueAsync(string value)
    {
        if (await _filter.ExistsAsync(value))
            return false;  // 值可能已存在

        await _filter.AddAsync(value);
        return true;  // 值之前一定不存在
    }
}

📖 高级用法

自定义过滤器实现

public class ClusterRedisBloomFilter : RedisBloomFilter
{
    public ClusterRedisBloomFilter(
        IRedisProvider redisProvider,
        BloomFilterOptions options)
        : base(redisProvider, options)
    {
    }

    protected override async Task<bool> InitializeFilterAsync()
    {
        // 实现集群环境下的初始化逻辑
        return await base.InitializeFilterAsync();
    }

    protected override string GenerateKey(string value)
    {
        // 自定义键生成策略
        return $"{Options.Name}:{value}";
    }
}

过滤器管理

public class BloomFilterManager
{
    private readonly Dictionary<string, IBloomFilter> _filters;
    
    public async Task<IBloomFilter> GetOrCreateFilterAsync(
        string name,
        int capacity,
        double errorRate)
    {
        if (_filters.TryGetValue(name, out var filter))
            return filter;
            
        var options = new BloomFilterOptions
        {
            Name = name,
            Capacity = capacity,
            ErrorRate = errorRate
        };
        
        filter = new RedisBloomFilter(_redisProvider, options);
        await filter.InitAsync();
        
        _filters[name] = filter;
        return filter;
    }
}

⚙️ 接口说明

RedisBloomFilter

基础 Redis 布隆过滤器实现,包含:

  • 基础过滤器操作实现
  • Redis 键管理
  • 异常处理和重试机制
  • 过滤器初始化逻辑
  • 批量操作支持

🔨 项目依赖

  • Tenon.BloomFilter.Abstractions
  • Tenon.Infra.Redis
  • Microsoft.Extensions.DependencyInjection.Abstractions

📝 使用注意事项

1. 性能考虑

  • 选择合适的哈希函数
  • 优化 Redis 访问模式
  • 注意批量操作性能

2. 容量规划

  • 预估数据增长
  • 合理设置误判率
  • 监控内存使用

3. 最佳实践

  • 实现监控和统计
  • 定期维护过滤器
  • 做好容灾备份

🌰 实际应用示例

1. 黑名单过滤

public class BlacklistService
{
    private readonly IBloomFilter _filter;

    public async Task<bool> IsBlacklistedAsync(string ip)
    {
        return await _filter.ExistsAsync(ip);
    }

    public async Task AddToBlacklistAsync(string ip)
    {
        await _filter.AddAsync(ip);
        await _logger.LogAsync($"IP {ip} 已加入黑名单");
    }
}

2. 重复数据检测

public class DuplicateChecker
{
    private readonly IBloomFilter _filter;

    public async Task<bool> IsDataProcessedAsync(string dataId)
    {
        if (await _filter.ExistsAsync(dataId))
            return true;  // 数据可能已处理

        await _filter.AddAsync(dataId);
        return false;  // 数据一定未处理
    }
}

🔗 相关实现

🤝 参与贡献

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

📄 开源协议

本项目采用 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 (1)

Showing the top 1 NuGet packages that depend on Tenon.BloomFilter.Redis:

Package Downloads
Tenon.BloomFilter.RedisStackExchange

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

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