Skip to content

Commit 4cc0ab9

Browse files
amiru3famirsolhi
andauthored
zremrangebyscore support for both Redis and CSRedis (#436)
Co-authored-by: amirsolhi <a.solhi@alibaba.ir>
1 parent 62fe591 commit 4cc0ab9

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.SortedSet.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace EasyCaching.CSRedis
22
{
33
using EasyCaching.Core;
4-
using EasyCaching.Core.Internal;
54
using System.Collections.Generic;
65
using System.Threading.Tasks;
76

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

88+
public long ZRangeRemByScore(string cacheKey, double min, double max)
89+
{
90+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
91+
92+
return _cache.ZRemRangeByScore(cacheKey, (decimal)min, (decimal)max);
93+
}
94+
8995
public long? ZRank<T>(string cacheKey, T cacheValue)
9096
{
9197
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
@@ -163,7 +169,7 @@ public async Task<double> ZIncrByAsync(string cacheKey, string field, double val
163169
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
164170
ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field));
165171

166-
var value= await _cache.ZIncrByAsync(cacheKey, field, (decimal)val);
172+
var value = await _cache.ZIncrByAsync(cacheKey, field, (decimal)val);
167173
return (double)value;
168174
}
169175

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

216+
public async Task<long> ZRangeRemByScoreAsync(string cacheKey, double min, double max)
217+
{
218+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
219+
220+
return await _cache.ZRemRangeByScoreAsync(cacheKey, (decimal)min, (decimal)max);
221+
}
222+
210223
public async Task<long?> ZRankAsync<T>(string cacheKey, T cacheValue)
211224
{
212225
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

src/EasyCaching.Core/IRedisCachingProvider.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,16 @@ public interface IRedisCachingProvider
752752
/// <param name="offset"></param>
753753
/// <returns></returns>
754754
List<T> ZRangeByScore<T>(string cacheKey, double min, double max, long? count = null, long offset = 0);
755+
756+
/// <summary>
757+
/// https://redis.io/commands/zremrangebyscore/
758+
/// </summary>
759+
/// <param name="cacheKey"></param>
760+
/// <param name="min"></param>
761+
/// <param name="max"></param>
762+
/// <returns>The number of elements removed</returns>
763+
long ZRangeRemByScore(string cacheKey, double min, double max);
764+
755765
/// <summary>
756766
/// https://redis.io/commands/zrank
757767
/// </summary>
@@ -835,6 +845,14 @@ public interface IRedisCachingProvider
835845
/// <returns></returns>
836846
Task<List<T>> ZRangeByScoreAsync<T>(string cacheKey, double min, double max, long? count = null, long offset = 0);
837847
/// <summary>
848+
/// https://redis.io/commands/zremrangebyscore/
849+
/// </summary>
850+
/// <param name="cacheKey"></param>
851+
/// <param name="min"></param>
852+
/// <param name="max"></param>
853+
/// <returns>The number of elements removed</returns>
854+
Task<long> ZRangeRemByScoreAsync(string cacheKey, double min, double max);
855+
/// <summary>
838856
/// https://redis.io/commands/zrank
839857
/// </summary>
840858
/// <typeparam name="T"></typeparam>

src/EasyCaching.Redis/DefaultRedisCachingProvider.SortedSet.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
55
using EasyCaching.Core;
6-
using EasyCaching.Core.Internal;
76
using StackExchange.Redis;
87

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

94+
public long ZRangeRemByScore(string cacheKey, double min, double max)
95+
{
96+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
97+
98+
return _cache.SortedSetRemoveRangeByScore(cacheKey, min, max);
99+
}
100+
95101
public long? ZRank<T>(string cacheKey, T cacheValue)
96102
{
97103
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
@@ -213,6 +219,13 @@ public async Task<List<T>> ZRangeByScoreAsync<T>(string cacheKey, double min, do
213219
return list;
214220
}
215221

222+
public async Task<long> ZRangeRemByScoreAsync(string cacheKey, double min, double max)
223+
{
224+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
225+
226+
return await _cache.SortedSetRemoveRangeByScoreAsync(cacheKey, min, max);
227+
}
228+
216229
public async Task<long?> ZRankAsync<T>(string cacheKey, T cacheValue)
217230
{
218231
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Threading.Tasks;
88
using Xunit;
99

10+
11+
1012
public abstract class BaseRedisFeatureCachingProviderTest
1113
{
1214
protected IRedisCachingProvider _provider;
@@ -81,7 +83,7 @@ protected virtual async Task StringSet_And_KeyExpire_And_TTL_Async_Should_Succee
8183

8284
await _provider.KeyDelAsync(cacheKey);
8385
}
84-
86+
8587
[Fact]
8688
protected virtual async Task StringSetWithExpiration_And_Persist_And_TTL_Async_Should_Succeed()
8789
{
@@ -90,7 +92,7 @@ protected virtual async Task StringSetWithExpiration_And_Persist_And_TTL_Async_S
9092
var res = await _provider.StringSetAsync(cacheKey, "123", TimeSpan.FromSeconds(10));
9193

9294
Assert.True(res);
93-
95+
9496
var initialTtl = await _provider.TTLAsync(cacheKey);
9597

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

16911693
_baseProvider.Remove(cacheKey);
1692-
}
1694+
}
1695+
1696+
[Fact]
1697+
protected virtual async void ZRangeRemByScoreAsync_Should_Succeed()
1698+
{
1699+
var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}";
1700+
1701+
var dict = new Dictionary<string, double>()
1702+
{
1703+
{ "one", 1},
1704+
{ "two", 2},
1705+
{ "three", 3},
1706+
{ "four", 4},
1707+
};
1708+
1709+
var res = await _provider.ZAddAsync(cacheKey, dict);
1710+
1711+
var deletedCount = await _provider.ZRangeRemByScoreAsync(cacheKey, 1, 2);
1712+
1713+
var items = await _provider.ZRangeByScoreAsync<string>(cacheKey, 1, 4);
1714+
1715+
Assert.Equal(2, deletedCount);
1716+
Assert.DoesNotContain("one", items);
1717+
Assert.DoesNotContain("two", items);
1718+
Assert.Contains("three", items);
1719+
Assert.Contains("four", items);
1720+
1721+
_baseProvider.Remove(cacheKey);
1722+
}
1723+
16931724
#endregion
16941725

16951726
#region Hyperloglog

0 commit comments

Comments
 (0)