Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/NRedisStack/Bloom/BloomCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public static SerializedCommand Add(RedisKey key, RedisValue item)
return new SerializedCommand(BF.ADD, key, item);
}

public static SerializedCommand Card(RedisKey key)
{
return new SerializedCommand(BF.CARD, key);
}

public static SerializedCommand Exists(RedisKey key, RedisValue item)
{
return new SerializedCommand(BF.EXISTS, key, item);
Expand Down
12 changes: 12 additions & 0 deletions src/NRedisStack/Bloom/BloomCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public async Task<bool> AddAsync(RedisKey key, RedisValue item)
return (await _db.ExecuteAsync(BloomCommandBuilder.Add(key, item))).ToString() == "1";
}

/// <inheritdoc/>
public long Card(RedisKey key)
{
return _db.Execute(BloomCommandBuilder.Card(key)).ToLong();
}

/// <inheritdoc/>
public async Task<long> CardAsync(RedisKey key)
{
return (await _db.ExecuteAsync(BloomCommandBuilder.Card(key))).ToLong();
}

/// <inheritdoc/>
public bool Exists(RedisKey key, RedisValue item)
{
Expand Down
16 changes: 16 additions & 0 deletions src/NRedisStack/Bloom/IBloomCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public interface IBloomCommands
/// <remarks><seealso href="https://redis.io/commands/bf.add"/></remarks>
Task<bool> AddAsync(RedisKey key, RedisValue item);

/// <summary>
/// Returns the cardinality of a Bloom filter.
/// </summary>
/// <param name="key">The name of the filter.</param>
/// <returns>number of items that were added to a Bloom filter and detected as unique.</returns>
/// <remarks><seealso href="https://redis.io/commands/bf.card"/></remarks>
long Card(RedisKey key);

/// <summary>
/// Returns the cardinality of a Bloom filter.
/// </summary>
/// <param name="key">The name of the filter.</param>
/// <returns>number of items that were added to a Bloom filter and detected as unique.</returns>
/// <remarks><seealso href="https://redis.io/commands/bf.card"/></remarks>
Task<long> CardAsync(RedisKey key);

/// <summary>
/// Checks whether an item exist in the Bloom Filter or not.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/NRedisStack/Bloom/Literals/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
internal class BF
{
public const string ADD = "BF.ADD";
public const string CARD = "BF.CARD";
public const string EXISTS = "BF.EXISTS";
public const string INFO = "BF.INFO";
public const string INSERT = "BF.INSERT";
Expand Down
38 changes: 38 additions & 0 deletions tests/NRedisStack.Tests/Bloom/BloomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,44 @@ public void TestModulePrefixs()
Assert.NotEqual(bf1.GetHashCode(), bf2.GetHashCode());
}

[Fact]
public void TestCard()
{
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var bf = db.BF();

// return 0 if the key does not exist:
Assert.Equal(0, bf.Card("notExist"));

// Store a filter:
Assert.True(bf.Add("bf1", "item_foo"));
Assert.Equal(1, bf.Card("bf1"));

// Error when key is of a type other than Bloom filter:
db.StringSet("setKey", "value");
Assert.Throws<RedisServerException>(() => bf.Card("setKey"));
}

[Fact]
public async Task TestCardAsync()
{
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var bf = db.BF();

// return 0 if the key does not exist:
Assert.Equal(0, await bf.CardAsync("notExist"));

// Store a filter:
Assert.True(await bf.AddAsync("bf1", "item_foo"));
Assert.Equal(1, await bf.CardAsync("bf1"));

// Error when key is of a type other than Bloom filter:
db.StringSet("setKey", "value");
await Assert.ThrowsAsync<RedisServerException>(() => bf.CardAsync("setKey"));
}

[Fact]
public void TestModulePrefixs1()
{
Expand Down