@@ -34,17 +34,17 @@ Assuming that you already have code that supplies you with each IP
34
34
address as a string, you could record the addresses in Redis using
35
35
a [ set] ({{< relref "/develop/data-types/sets" >}}):
36
36
37
- ``` py
38
- r.sadd (" ip_tracker" , new_ip_address)
37
+ ``` cs
38
+ db . SetAdd (" ip_tracker" , new_ip_address );
39
39
```
40
40
41
41
The set can only contain each key once, so if the same address
42
42
appears again during the day, the new instance will not change
43
43
the set. At the end of the day, you could get the exact number of
44
- distinct addresses using the ` scard ()` function:
44
+ distinct addresses using the ` SetLength ()` function:
45
45
46
- ``` py
47
- num_distinct_ips = r.scard (" ip_tracker" )
46
+ ``` cs
47
+ var num_distinct_ips = db . SetLength (" ip_tracker" );
48
48
```
49
49
50
50
This approach is simple, effective, and precise but if your website
@@ -99,16 +99,49 @@ add. The following example adds some names to a Bloom filter representing
99
99
a list of users and checks for the presence or absence of users in the list.
100
100
Note that you must use the ` BF() ` method to access the Bloom filter commands.
101
101
102
- {{< clients-example home_prob_dts bloom "C#" >}}
103
- {{< /clients-example >}}
102
+ <!-- < clients-example home_prob_dts bloom "C#" >}}
103
+ < /clients-example >}}-->
104
+ ``` cs
105
+ bool [] res1 = db .BF ().MAdd (
106
+ " recorded_users" , " andy" , " cameron" , " david" , " michelle"
107
+ );
108
+ Console .WriteLine (string .Join (" , " , res1 ));
109
+ // >>> true, true, true, true
110
+
111
+ bool res2 = db .BF ().Exists (" recorded_users" , " cameron" );
112
+ Console .WriteLine (res2 ); // >>> true
113
+
114
+ bool res3 = db .BF ().Exists (" recorded_users" , " kaitlyn" );
115
+ Console .WriteLine (res3 ); // >>> false
116
+ ```
104
117
105
118
A Cuckoo filter has similar features to a Bloom filter, but also supports
106
119
a deletion operation to remove hashes from a set, as shown in the example
107
120
below. Note that you must use the ` CF() ` method to access the Cuckoo filter
108
121
commands.
109
122
110
- {{< clients-example home_prob_dts cuckoo "C#" >}}
111
- {{< /clients-example >}}
123
+ <!-- < clients-example home_prob_dts cuckoo "C#" >}}
124
+ < /clients-example >}}-->
125
+ ``` cs
126
+ bool res4 = db .CF ().Add (" other_users" , " paolo" );
127
+ Console .WriteLine (res4 ); // >>> true
128
+
129
+ bool res5 = db .CF ().Add (" other_users" , " kaitlyn" );
130
+ Console .WriteLine (res5 ); // >>> true
131
+
132
+ bool res6 = db .CF ().Add (" other_users" , " rachel" );
133
+ Console .WriteLine (res6 ); // >>> true
134
+
135
+ bool [] res7 = db .CF ().MExists (" other_users" , " paolo" , " rachel" , " andy" );
136
+ Console .WriteLine (string .Join (" , " , res7 ));
137
+ // >>> true, true, false
138
+
139
+ bool res8 = db .CF ().Del (" other_users" , " paolo" );
140
+ Console .WriteLine (res8 ); // >>> true
141
+
142
+ bool res9 = db .CF ().Exists (" other_users" , " paolo" );
143
+ Console .WriteLine (res9 ); // >>> false
144
+ ```
112
145
113
146
Which of these two data types you choose depends on your use case.
114
147
Bloom filters are generally faster than Cuckoo filters when adding new items,
@@ -128,8 +161,35 @@ You can also merge two or more HyperLogLogs to find the cardinality of the
128
161
[ union] ( https://en.wikipedia.org/wiki/Union_(set_theory) ) of the sets they
129
162
represent.
130
163
131
- {{< clients-example home_prob_dts hyperloglog "C#" >}}
132
- {{< /clients-example >}}
164
+ <!-- < clients-example home_prob_dts hyperloglog "C#" >}}
165
+ < /clients-example >}}-->
166
+ ``` cs
167
+ bool res10 = db .HyperLogLogAdd (
168
+ " group:1" ,
169
+ new RedisValue [] { " andy" , " cameron" , " david" }
170
+ );
171
+ Console .WriteLine (res10 ); // >>> true
172
+
173
+ long res11 = db .HyperLogLogLength (" group:1" );
174
+ Console .WriteLine (res11 ); // >>> 3
175
+
176
+ bool res12 = db .HyperLogLogAdd (
177
+ " group:2" ,
178
+ new RedisValue [] { " kaitlyn" , " michelle" , " paolo" , " rachel" }
179
+ );
180
+ Console .WriteLine (res12 ); // >>> true
181
+
182
+ long res13 = db .HyperLogLogLength (" group:2" );
183
+ Console .WriteLine (res13 ); // >>> 4
184
+
185
+ db .HyperLogLogMerge (
186
+ " both_groups" ,
187
+ " group:1" , " group:2"
188
+ );
189
+
190
+ long res14 = db .HyperLogLogLength (" both_groups" );
191
+ Console .WriteLine (res14 ); // >>> 7
192
+ ```
133
193
134
194
The main benefit that HyperLogLogs offer is their very low
135
195
memory usage. They can count up to 2^64 items with less than
@@ -169,8 +229,44 @@ a Count-min sketch object, add data to it, and then query it.
169
229
Note that you must use the ` CMS() ` method to access the Count-min
170
230
sketch commands.
171
231
172
- {{< clients-example home_prob_dts cms "C#" >}}
173
- {{< /clients-example >}}
232
+ <!-- < clients-example home_prob_dts cms "C#" >}}
233
+ < /clients-example >}}-->
234
+ ``` cs
235
+ // Specify that you want to keep the counts within 0.01
236
+ // (0.1%) of the true value with a 0.005 (0.05%) chance
237
+ // of going outside this limit.
238
+ bool res15 = db .CMS ().InitByProb (" items_sold" , 0 . 01 , 0 . 005 );
239
+ Console .WriteLine (res15 ); // >>> true
240
+
241
+ long [] res16 = db .CMS ().IncrBy (
242
+ " items_sold" ,
243
+ new Tuple <RedisValue , long >[]{
244
+ new (" bread" , 300 ),
245
+ new (" tea" , 200 ),
246
+ new (" coffee" , 200 ),
247
+ new (" beer" , 100 )
248
+ }
249
+ );
250
+ Console .WriteLine (string .Join (" , " , res16 ));
251
+ // >>> 300, 200, 200, 100
252
+
253
+ long [] res17 = db .CMS ().IncrBy (
254
+ " items_sold" ,
255
+ new Tuple <RedisValue , long >[]{
256
+ new (" bread" , 100 ),
257
+ new (" coffee" , 150 ),
258
+ }
259
+ );
260
+ Console .WriteLine (string .Join (" , " , res17 ));
261
+ // >>> 400, 350
262
+
263
+ long [] res18 = db .CMS ().Query (
264
+ " items_sold" ,
265
+ " bread" , " tea" , " coffee" , " beer"
266
+ );
267
+ Console .WriteLine (string .Join (" , " , res18 ));
268
+ // >>> 400, 200, 350, 100
269
+ ```
174
270
175
271
The advantage of using a CMS over keeping an exact count with a
176
272
[ sorted set] ({{< relref "/develop/data-types/sorted-sets" >}})
@@ -202,8 +298,53 @@ shows how to merge two or more t-digest objects to query the combined
202
298
data set. Note that you must use the ` TDIGEST() ` method to access the
203
299
t-digest commands.
204
300
205
- {{< clients-example home_prob_dts tdigest "C#" >}}
206
- {{< /clients-example >}}
301
+ <!-- < clients-example home_prob_dts tdigest "C#" >}}
302
+ < /clients-example >}}-->
303
+ ``` cs
304
+ bool res19 = db .TDIGEST ().Create (" male_heights" );
305
+ Console .WriteLine (res19 ); // >>> true
306
+
307
+ bool res20 = db .TDIGEST ().Add (
308
+ " male_heights" ,
309
+ 175 . 5 , 181 , 160 . 8 , 152 , 177 , 196 , 164
310
+ );
311
+ Console .WriteLine (res20 ); // >>> true
312
+
313
+ double res21 = db .TDIGEST ().Min (" male_heights" );
314
+ Console .WriteLine (res21 ); // >>> 152.0
315
+
316
+ double res22 = db .TDIGEST ().Max (" male_heights" );
317
+ Console .WriteLine (res22 ); // >>> 196.0
318
+
319
+ double [] res23 = db .TDIGEST ().Quantile (" male_heights" , 0 . 75 );
320
+ Console .WriteLine (string .Join (" , " , res23 )); // >>> 181.0
321
+
322
+ // Note that the CDF value for 181.0 is not exactly
323
+ // 0.75. Both values are estimates.
324
+ double [] res24 = db .TDIGEST ().CDF (" male_heights" , 181 . 0 );
325
+ Console .WriteLine (string .Join (" , " , res24 )); // >>> 0.7857142857142857
326
+
327
+ bool res25 = db .TDIGEST ().Create (" female_heights" );
328
+ Console .WriteLine (res25 ); // >>> true
329
+
330
+ bool res26 = db .TDIGEST ().Add (
331
+ " female_heights" ,
332
+ 155 . 5 , 161 , 168 . 5 , 170 , 157 . 5 , 163 , 171
333
+ );
334
+ Console .WriteLine (res26 ); // >>> true
335
+
336
+ double [] res27 = db .TDIGEST ().Quantile (" female_heights" , 0 . 75 );
337
+ Console .WriteLine (string .Join (" , " , res27 )); // >>> 170.0
338
+
339
+ // Specify 0 for `compression` and false for `override`.
340
+ bool res28 = db .TDIGEST ().Merge (
341
+ " all_heights" , 0 , false , " male_heights" , " female_heights"
342
+ );
343
+ Console .WriteLine (res28 ); // >>> true
344
+
345
+ double [] res29 = db .TDIGEST ().Quantile (" all_heights" , 0 . 75 );
346
+ Console .WriteLine (string .Join (" , " , res29 )); // >>> 175.5
347
+ ```
207
348
208
349
A t-digest object also supports several other related commands, such
209
350
as querying by rank. See the
@@ -220,10 +361,59 @@ top five most popular items sold.
220
361
221
362
The example below adds several different items to a Top-K object
222
363
that tracks the top three items (this is the second parameter to
223
- the ` topk ().reserve ()` method). It also shows how to list the
364
+ the ` TOPK ().Reserve ()` method). It also shows how to list the
224
365
top * k* items and query whether or not a given item is in the
225
366
list. Note that you must use the ` TOPK() ` method to access the
226
367
Top-K commands.
227
368
228
- {{< clients-example home_prob_dts topk "C#" >}}
229
- {{< /clients-example >}}
369
+ <!-- < clients-example home_prob_dts topk "C#" >}}
370
+ < /clients-example >}}-->
371
+ ``` cs
372
+ bool res30 = db .TOPK ().Reserve (" top_3_songs" , 3 , 7 , 8 , 0 . 9 );
373
+ Console .WriteLine (res30 ); // >>> true
374
+
375
+ RedisResult [] res31 = db .TOPK ().IncrBy (
376
+ " top_3_songs" ,
377
+ new Tuple <RedisValue , long >[] {
378
+ new (" Starfish Trooper" , 3000 ),
379
+ new (" Only one more time" , 1850 ),
380
+ new (" Rock me, Handel" , 1325 ),
381
+ new (" How will anyone know?" , 3890 ),
382
+ new (" Average lover" , 4098 ),
383
+ new (" Road to everywhere" , 770 )
384
+ }
385
+ );
386
+ Console .WriteLine (
387
+ string .Join (
388
+ " , " ,
389
+ string .Join (
390
+ " , " ,
391
+ res31 .Select (
392
+ r => $" {(r .IsNull ? " Null" : r )}"
393
+ )
394
+ )
395
+ )
396
+ );
397
+ // >>> Null, Null, Null, Rock me, Handel, Only one more time, Null
398
+
399
+ RedisResult [] res32 = db .TOPK ().List (" top_3_songs" );
400
+ Console .WriteLine (
401
+ string .Join (
402
+ " , " ,
403
+ string .Join (
404
+ " , " ,
405
+ res32 .Select (
406
+ r => $" {(r .IsNull ? " Null" : r )}"
407
+ )
408
+ )
409
+ )
410
+ );
411
+ // >>> Average lover, How will anyone know?, Starfish Trooper
412
+
413
+ bool [] res33 = db .TOPK ().Query (
414
+ " top_3_songs" ,
415
+ " Starfish Trooper" , " Road to everywhere"
416
+ );
417
+ Console .WriteLine (string .Join (" , " , res33 ));
418
+ // >>> true, false
419
+ ```
0 commit comments