布隆过滤器
主要特性:
2. 安装
go get github.com/sagoo-cloud/nexframe3. 基本用法
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"github.com/sagoo-cloud/nexframe/os/bloom"
)
func main() {
// 创建 Redis 客户端
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
// 创建布隆过滤器实例
bf, err := bloom.New(
bloom.WithRedis(redisClient), //如果不设置将使用框架默认的
bloom.WithKey("my-bloom-filter"),
)
if err != nil {
panic(err)
}
ctx := context.Background()
// 添加元素
err = bf.Add(ctx, "apple", "banana", "cherry")
if err != nil {
panic(err)
}
// 检查元素是否存在
exists, err := bf.Exist(ctx, "apple")
if err != nil {
panic(err)
}
fmt.Printf("Does 'apple' exist? %v\n", exists)
exists, err = bf.Exist(ctx, "durian")
if err != nil {
panic(err)
}
fmt.Printf("Does 'durian' exist? %v\n", exists)
}4. 配置选项
WithRedis(redis.UniversalClient)
WithKey(string)
WithExpire(time.Duration)
WithHash(...func(string) uint64)
WithTimeout(time.Duration)
5. 高级用法
使用自定义哈希函数
批量添加元素
并发使用
6. 性能考虑
7. 常见问题解答
最后更新于