Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zremrangebyscore support for both Redis and CSRedis #436

Merged
merged 1 commit into from
Dec 19, 2022
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
17 changes: 15 additions & 2 deletions src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.SortedSet.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace EasyCaching.CSRedis
{
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand Down Expand Up @@ -86,6 +85,13 @@ public List<T> ZRangeByScore<T>(string cacheKey, double min, double max, long? c
return list;
}

public long ZRangeRemByScore(string cacheKey, double min, double max)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

return _cache.ZRemRangeByScore(cacheKey, (decimal)min, (decimal)max);
}

public long? ZRank<T>(string cacheKey, T cacheValue)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
Expand Down Expand Up @@ -163,7 +169,7 @@ public async Task<double> ZIncrByAsync(string cacheKey, string field, double val
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field));

var value= await _cache.ZIncrByAsync(cacheKey, field, (decimal)val);
var value = await _cache.ZIncrByAsync(cacheKey, field, (decimal)val);
return (double)value;
}

Expand Down Expand Up @@ -207,6 +213,13 @@ public async Task<List<T>> ZRangeByScoreAsync<T>(string cacheKey, double min, do
return list;
}

public async Task<long> ZRangeRemByScoreAsync(string cacheKey, double min, double max)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

return await _cache.ZRemRangeByScoreAsync(cacheKey, (decimal)min, (decimal)max);
}

public async Task<long?> ZRankAsync<T>(string cacheKey, T cacheValue)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
Expand Down
18 changes: 18 additions & 0 deletions src/EasyCaching.Core/IRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,16 @@ public interface IRedisCachingProvider
/// <param name="offset"></param>
/// <returns></returns>
List<T> ZRangeByScore<T>(string cacheKey, double min, double max, long? count = null, long offset = 0);

/// <summary>
/// https://redis.io/commands/zremrangebyscore/
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns>The number of elements removed</returns>
long ZRangeRemByScore(string cacheKey, double min, double max);

/// <summary>
/// https://redis.io/commands/zrank
/// </summary>
Expand Down Expand Up @@ -835,6 +845,14 @@ public interface IRedisCachingProvider
/// <returns></returns>
Task<List<T>> ZRangeByScoreAsync<T>(string cacheKey, double min, double max, long? count = null, long offset = 0);
/// <summary>
/// https://redis.io/commands/zremrangebyscore/
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns>The number of elements removed</returns>
Task<long> ZRangeRemByScoreAsync(string cacheKey, double min, double max);
/// <summary>
/// https://redis.io/commands/zrank
/// </summary>
/// <typeparam name="T"></typeparam>
Expand Down
15 changes: 14 additions & 1 deletion src/EasyCaching.Redis/DefaultRedisCachingProvider.SortedSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using StackExchange.Redis;

/// <summary>
Expand Down Expand Up @@ -92,6 +91,13 @@ public List<T> ZRangeByScore<T>(string cacheKey, double min, double max, long? c
return list;
}

public long ZRangeRemByScore(string cacheKey, double min, double max)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

return _cache.SortedSetRemoveRangeByScore(cacheKey, min, max);
}

public long? ZRank<T>(string cacheKey, T cacheValue)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
Expand Down Expand Up @@ -213,6 +219,13 @@ public async Task<List<T>> ZRangeByScoreAsync<T>(string cacheKey, double min, do
return list;
}

public async Task<long> ZRangeRemByScoreAsync(string cacheKey, double min, double max)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

return await _cache.SortedSetRemoveRangeByScoreAsync(cacheKey, min, max);
}

public async Task<long?> ZRankAsync<T>(string cacheKey, T cacheValue)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Threading.Tasks;
using Xunit;



public abstract class BaseRedisFeatureCachingProviderTest
{
protected IRedisCachingProvider _provider;
Expand Down Expand Up @@ -81,7 +83,7 @@ protected virtual async Task StringSet_And_KeyExpire_And_TTL_Async_Should_Succee

await _provider.KeyDelAsync(cacheKey);
}

[Fact]
protected virtual async Task StringSetWithExpiration_And_Persist_And_TTL_Async_Should_Succeed()
{
Expand All @@ -90,7 +92,7 @@ protected virtual async Task StringSetWithExpiration_And_Persist_And_TTL_Async_S
var res = await _provider.StringSetAsync(cacheKey, "123", TimeSpan.FromSeconds(10));

Assert.True(res);

var initialTtl = await _provider.TTLAsync(cacheKey);

Assert.InRange(initialTtl, 1, 10);
Expand Down Expand Up @@ -1689,7 +1691,36 @@ protected virtual async void ZRangeByScoreAsync_Should_Succeed()
Assert.Contains("two", items);

_baseProvider.Remove(cacheKey);
}
}

[Fact]
protected virtual async void ZRangeRemByScoreAsync_Should_Succeed()
{
var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}";

var dict = new Dictionary<string, double>()
{
{ "one", 1},
{ "two", 2},
{ "three", 3},
{ "four", 4},
};

var res = await _provider.ZAddAsync(cacheKey, dict);

var deletedCount = await _provider.ZRangeRemByScoreAsync(cacheKey, 1, 2);

var items = await _provider.ZRangeByScoreAsync<string>(cacheKey, 1, 4);

Assert.Equal(2, deletedCount);
Assert.DoesNotContain("one", items);
Assert.DoesNotContain("two", items);
Assert.Contains("three", items);
Assert.Contains("four", items);

_baseProvider.Remove(cacheKey);
}

#endregion

#region Hyperloglog
Expand Down