Skip to content

Commit ec3cbf0

Browse files
authored
Merge pull request #366 from dotnetcore/improve-iss363
feat: redis feature support ZRangeByScore
2 parents c48bebb + 75f7d11 commit ec3cbf0

File tree

4 files changed

+2895
-2757
lines changed

4 files changed

+2895
-2757
lines changed
Lines changed: 119 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
namespace EasyCaching.CSRedis
1+
namespace EasyCaching.CSRedis
22
{
33
using EasyCaching.Core;
4-
using EasyCaching.Core.Internal;
5-
using System.Collections.Generic;
6-
using System.Threading.Tasks;
7-
4+
using EasyCaching.Core.Internal;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
7+
88
public partial class DefaultCSRedisCachingProvider : IRedisCachingProvider
99
{
1010
public long ZAdd<T>(string cacheKey, Dictionary<T, double> cacheValues)
@@ -25,57 +25,73 @@ public long ZAdd<T>(string cacheKey, Dictionary<T, double> cacheValues)
2525

2626
public long ZCard(string cacheKey)
2727
{
28-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
29-
30-
var len = _cache.ZCard(cacheKey);
28+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
29+
30+
var len = _cache.ZCard(cacheKey);
3131
return len;
3232
}
3333

3434
public long ZCount(string cacheKey, double min, double max)
3535
{
36-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
37-
38-
var len = _cache.ZCount(cacheKey, (decimal)min, (decimal)max);
36+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
37+
38+
var len = _cache.ZCount(cacheKey, (decimal)min, (decimal)max);
3939
return len;
4040
}
4141
public double ZIncrBy(string cacheKey, string field, double val = 1)
4242
{
43-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
44-
ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field));
45-
43+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
44+
ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field));
45+
4646
var value = _cache.ZIncrBy(cacheKey, field, (decimal)val);
4747
return (double)value;
4848
}
4949
public long ZLexCount(string cacheKey, string min, string max)
5050
{
51-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
52-
53-
var len = _cache.ZLexCount(cacheKey, min, max);
51+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
52+
53+
var len = _cache.ZLexCount(cacheKey, min, max);
5454
return len;
5555
}
5656

5757
public List<T> ZRange<T>(string cacheKey, long start, long stop)
5858
{
59-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
60-
61-
var list = new List<T>();
62-
63-
var bytes = _cache.ZRange<byte[]>(cacheKey, start, stop);
64-
59+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
60+
61+
var list = new List<T>();
62+
63+
var bytes = _cache.ZRange<byte[]>(cacheKey, start, stop);
64+
65+
foreach (var item in bytes)
66+
{
67+
list.Add(_serializer.Deserialize<T>(item));
68+
}
69+
70+
return list;
71+
}
72+
73+
public List<T> ZRangeByScore<T>(string cacheKey, double min, double max, long? count = null, long offset = 0)
74+
{
75+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
76+
77+
var list = new List<T>();
78+
79+
var bytes = _cache.ZRangeByScore<byte[]>(cacheKey, (decimal)min, (decimal)max, count, offset);
80+
6581
foreach (var item in bytes)
6682
{
6783
list.Add(_serializer.Deserialize<T>(item));
68-
}
69-
84+
}
85+
7086
return list;
7187
}
7288

7389
public long? ZRank<T>(string cacheKey, T cacheValue)
7490
{
7591
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
7692

77-
var bytes = _serializer.Serialize(cacheValue);
78-
93+
var bytes = _serializer.Serialize(cacheValue);
94+
7995
var index = _cache.ZRank(cacheKey, bytes);
8096

8197
return index;
@@ -86,27 +102,27 @@ public long ZRem<T>(string cacheKey, IList<T> cacheValues)
86102
{
87103
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
88104

89-
var bytes = new List<byte[]>();
90-
105+
var bytes = new List<byte[]>();
106+
91107
foreach (var item in cacheValues)
92108
{
93109
bytes.Add(_serializer.Serialize(item));
94-
}
95-
110+
}
111+
96112
var len = _cache.ZRem<byte[]>(cacheKey, bytes.ToArray());
97113

98114
return len;
99-
}
100-
101-
public double? ZScore<T>(string cacheKey, T cacheValue)
102-
{
103-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
104-
105-
var bytes = _serializer.Serialize(cacheValue);
106-
107-
var score = _cache.ZScore(cacheKey, bytes);
108-
109-
return (double?)score;
115+
}
116+
117+
public double? ZScore<T>(string cacheKey, T cacheValue)
118+
{
119+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
120+
121+
var bytes = _serializer.Serialize(cacheValue);
122+
123+
var score = _cache.ZScore(cacheKey, bytes);
124+
125+
return (double?)score;
110126
}
111127

112128
public async Task<long> ZAddAsync<T>(string cacheKey, Dictionary<T, double> cacheValues)
@@ -128,59 +144,75 @@ public async Task<long> ZAddAsync<T>(string cacheKey, Dictionary<T, double> cach
128144

129145
public async Task<long> ZCardAsync(string cacheKey)
130146
{
131-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
132-
133-
var len = await _cache.ZCardAsync(cacheKey);
147+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
148+
149+
var len = await _cache.ZCardAsync(cacheKey);
134150
return len;
135-
}
136-
151+
}
152+
137153
public async Task<long> ZCountAsync(string cacheKey, double min, double max)
138154
{
139-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
140-
141-
var len = await _cache.ZCountAsync(cacheKey, (decimal)min, (decimal)max);
155+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
156+
157+
var len = await _cache.ZCountAsync(cacheKey, (decimal)min, (decimal)max);
142158
return len;
143159
}
144160

145161
public async Task<double> ZIncrByAsync(string cacheKey, string field, double val = 1)
146162
{
147-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
148-
ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field));
149-
163+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
164+
ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field));
165+
150166
var value= await _cache.ZIncrByAsync(cacheKey, field, (decimal)val);
151167
return (double)value;
152-
}
153-
168+
}
169+
154170
public async Task<long> ZLexCountAsync(string cacheKey, string min, string max)
155171
{
156-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
157-
158-
var len = await _cache.ZLexCountAsync(cacheKey, min, max);
172+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
173+
174+
var len = await _cache.ZLexCountAsync(cacheKey, min, max);
159175
return len;
160176
}
161177

162178
public async Task<List<T>> ZRangeAsync<T>(string cacheKey, long start, long stop)
163179
{
164-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
165-
166-
var list = new List<T>();
167-
168-
var bytes = await _cache.ZRangeAsync<byte[]>(cacheKey, start, stop);
169-
180+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
181+
182+
var list = new List<T>();
183+
184+
var bytes = await _cache.ZRangeAsync<byte[]>(cacheKey, start, stop);
185+
170186
foreach (var item in bytes)
171187
{
172188
list.Add(_serializer.Deserialize<T>(item));
173-
}
174-
189+
}
190+
191+
return list;
192+
}
193+
194+
public async Task<List<T>> ZRangeByScoreAsync<T>(string cacheKey, double min, double max, long? count = null, long offset = 0)
195+
{
196+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
197+
198+
var list = new List<T>();
199+
200+
var bytes = await _cache.ZRangeByScoreAsync<byte[]>(cacheKey, (decimal)min, (decimal)max, count, offset);
201+
202+
foreach (var item in bytes)
203+
{
204+
list.Add(_serializer.Deserialize<T>(item));
205+
}
206+
175207
return list;
176208
}
177209

178210
public async Task<long?> ZRankAsync<T>(string cacheKey, T cacheValue)
179211
{
180212
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
181213

182-
var bytes = _serializer.Serialize(cacheValue);
183-
214+
var bytes = _serializer.Serialize(cacheValue);
215+
184216
var index = await _cache.ZRankAsync(cacheKey, bytes);
185217

186218
return index;
@@ -190,28 +222,28 @@ public async Task<long> ZRemAsync<T>(string cacheKey, IList<T> cacheValues)
190222
{
191223
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
192224

193-
var bytes = new List<byte[]>();
194-
225+
var bytes = new List<byte[]>();
226+
195227
foreach (var item in cacheValues)
196228
{
197229
bytes.Add(_serializer.Serialize(item));
198-
}
199-
230+
}
231+
200232
var len = await _cache.ZRemAsync<byte[]>(cacheKey, bytes.ToArray());
201233

202234
return len;
203-
}
204-
205-
public async Task<double?> ZScoreAsync<T>(string cacheKey, T cacheValue)
206-
{
207-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
208-
209-
var bytes = _serializer.Serialize(cacheValue);
210-
211-
var score = await _cache.ZScoreAsync(cacheKey, bytes);
212-
213-
return (double?)score;
214-
}
215-
216-
}
217-
}
235+
}
236+
237+
public async Task<double?> ZScoreAsync<T>(string cacheKey, T cacheValue)
238+
{
239+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
240+
241+
var bytes = _serializer.Serialize(cacheValue);
242+
243+
var score = await _cache.ZScoreAsync(cacheKey, bytes);
244+
245+
return (double?)score;
246+
}
247+
248+
}
249+
}

0 commit comments

Comments
 (0)