Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage.
And I wrote my first edition RedisHelper.Here is the code:
The Interface IRedis:
1 public interface IRedis 2 { 3 ITransaction GetTransaction(int db = 0, bool isRead = false); 4 5 #region String 6 #region get 7 /// <summary> 8 /// get the string value 9 /// </summary> 10 /// <param name="key">the key of value</param> 11 /// <param name="flag">behaviour</param> 12 /// <param name="db">index of database</param> 13 /// <returns></returns> 14 RedisValue Get(string key, CommandFlags flag = CommandFlags.None, int db = 0); 15 /// <summary> 16 /// get the string value(Asynchronous) 17 /// </summary> 18 /// <param name="key">the key of value</param> 19 /// <param name="flag">behaviour</param> 20 /// <param name="db">index of database</param> 21 /// <returns></returns> 22 Task<RedisValue> GetAsync(string key, CommandFlags flag = CommandFlags.None, int db = 0); 23 /// <summary> 24 /// get the entity by deserialization 25 /// </summary> 26 /// <param name="key">the key of value</param> 27 /// <param name="flag">behaviour</param> 28 /// <param name="db">index of database</param> 29 /// <returns></returns> 30 T Get<T>(string key, CommandFlags flags = CommandFlags.None, int db = 0); 31 /// <summary> 32 /// get the entity by deserialization(Asynchronous) 33 /// </summary> 34 /// <param name="key">the key of value</param> 35 /// <param name="flag">behaviour</param> 36 /// <param name="db">index of database</param> 37 /// <returns></returns> 38 Task<T> GetAsync<T>(string key, CommandFlags flags = CommandFlags.None, int db = 0); 39 #endregion 40 41 #region set 42 /// <summary> 43 /// set value to key 44 /// </summary> 45 /// <param name="key">the key</param> 46 /// <param name="value">the value of the key</param> 47 /// <param name="expiry">time to expiry</param> 48 /// <param name="when">when this operation should be performed</param> 49 /// <param name="flags">behaviour</param> 50 /// <param name="db">index of database</param> 51 /// <returns></returns> 52 RedisValue Set(string key, string value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 53 /// <summary> 54 /// set value to key(Asynchronous) 55 /// </summary> 56 /// <param name="key">the key</param> 57 /// <param name="value">the value of the key</param> 58 /// <param name="expiry">time to expiry</param> 59 /// <param name="when">when this operation should be performed</param> 60 /// <param name="flags">behaviour</param> 61 /// <param name="db">index of database</param> 62 /// <returns></returns> 63 Task<bool> SetAsync(string key, string value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 64 #endregion 65 66 #region mget 67 /// <summary> 68 /// get multi values 69 /// </summary> 70 /// <param name="keys">the keys of the values</param> 71 /// <param name="flags">behaviour</param> 72 /// <param name="db">index of database</param> 73 /// <returns></returns> 74 IList<RedisValue> MGet(List<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0); 75 /// <summary> 76 /// get multi values(Asynchronous) 77 /// </summary> 78 /// <param name="keys">the keys of the values</param> 79 /// <param name="flags">behaviour</param> 80 /// <param name="db">index of database</param> 81 /// <returns></returns> 82 Task<RedisValue[]> MGetAsync(List<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0); 83 #endregion 84 85 #region mset 86 /// <summary> 87 /// set multi values 88 /// </summary> 89 /// <param name="kvs">key-values</param> 90 /// <param name="when">when this operation should be performed</param> 91 /// <param name="flags">behaviour</param> 92 /// <param name="db">index of database</param> 93 /// <returns></returns> 94 bool MSet(List<KeyValuePair<RedisKey, RedisValue>> kvs, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 95 /// <summary> 96 /// set multi values(Asynchronous) 97 /// </summary> 98 /// <param name="kvs">key-values</param> 99 /// <param name="when">when this operation should be performed</param> 100 /// <param name="flags">behaviour</param> 101 /// <param name="db">index of database</param> 102 /// <returns></returns> 103 Task<bool> MSetAsync(List<KeyValuePair<RedisKey, RedisValue>> kvs, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 104 #endregion 105 106 #region incr incrby incrbyfloat decr decrby 107 /// <summary> 108 /// handle the numeric value 109 /// </summary> 110 /// <param name="key">key</param> 111 /// <param name="amount">value to increase or decrease</param> 112 /// <param name="flags">behaviour</param> 113 /// <param name="db">index of database</param> 114 /// <returns></returns> 115 double IncrOrDecrBy(RedisKey key, double value, CommandFlags flags = CommandFlags.None, int db = 0); 116 /// <summary> 117 /// handle the numeric value(Asynchronous) 118 /// </summary> 119 /// <param name="key">key</param> 120 /// <param name="amount">value to increase or decrease</param> 121 /// <param name="flags">behaviour</param> 122 /// <param name="db">index of database</param> 123 /// <returns></returns> 124 Task<double> IncrOrDecrByAsync(RedisKey key, double value, CommandFlags flags = CommandFlags.None, int db = 0); 125 #endregion 126 127 #region append 128 /// <summary> 129 /// append value to the key 130 /// </summary> 131 /// <param name="key">key</param> 132 /// <param name="value">value to append</param> 133 /// <param name="flags">behaviour</param> 134 /// <param name="db">index of database</param> 135 /// <returns></returns> 136 long Append(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0); 137 /// <summary> 138 /// append value to the key(Asynchronous) 139 /// </summary> 140 /// <param name="key">key</param> 141 /// <param name="value">value to append</param> 142 /// <param name="flags">behaviour</param> 143 /// <param name="db">index of database</param> 144 /// <returns></returns> 145 Task<long> AppendAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0); 146 #endregion 147 148 #region strlen 149 /// <summary> 150 /// get the value's length by the key 151 /// </summary> 152 /// <param name="key">key</param> 153 /// <param name="flags">behaviour</param> 154 /// <param name="db">index of database</param> 155 /// <returns></returns> 156 long StrLen(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 157 /// <summary> 158 /// get the value's length by the key(Asynchronous) 159 /// </summary> 160 /// <param name="key">key</param> 161 /// <param name="flags">behaviour</param> 162 /// <param name="db">index of database</param> 163 /// <returns></returns> 164 Task<long> StrLenAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 165 #endregion 166 #endregion 167 168 #region Hash 169 #region hget 170 /// <summary> 171 /// get the value of key's field 172 /// </summary> 173 /// <param name="key">key</param> 174 /// <param name="field">field of key</param> 175 /// <param name="flags">behaviour</param> 176 /// <param name="db">index of database</param> 177 /// <returns></returns> 178 Task<RedisValue> HGetAsync(RedisKey key, RedisValue field, CommandFlags flags = CommandFlags.None, int db = 0); 179 /// <summary> 180 /// get the value of key's field(Asynchronous) 181 /// </summary> 182 /// <param name="key">key</param> 183 /// <param name="field">field of key</param> 184 /// <param name="flags">behaviour</param> 185 /// <param name="db">index of database</param> 186 /// <returns></returns> 187 RedisValue HGet(RedisKey key, RedisValue field, CommandFlags flags = CommandFlags.None, int db = 0); 188 #endregion 189 190 #region hset 191 /// <summary> 192 /// set the field and value of the key 193 /// </summary> 194 /// <param name="key">key</param> 195 /// <param name="field">field of the key</param> 196 /// <param name="value">value of the field</param> 197 /// <param name="when">when this operation should be performed</param> 198 /// <param name="flags">behaviour</param> 199 /// <param name="db">index of database</param> 200 /// <returns></returns> 201 bool HSet(RedisKey key, RedisValue field, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 202 /// <summary> 203 /// set the field and value of the key 204 /// </summary> 205 /// <param name="key">key</param> 206 /// <param name="field">field of the key</param> 207 /// <param name="value">value of the field</param> 208 /// <param name="when">when this operation should be performed</param> 209 /// <param name="flags">behaviour</param> 210 /// <param name="db">index of database</param> 211 /// <returns></returns> 212 Task<bool> HSetAsync(RedisKey key, RedisValue field, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 213 #endregion 214 215 #region hmget 216 /// <summary> 217 /// get multi values of key's fields 218 /// </summary> 219 /// <param name="key">key</param> 220 /// <param name="fields">fields of key</param> 221 /// <param name="flags">behaviour</param> 222 /// <param name="db">index of database</param> 223 /// <returns></returns> 224 IList<RedisValue> HMGet(RedisKey key, List<RedisValue> fields, CommandFlags flags = CommandFlags.None, int db = 0); 225 /// <summary> 226 /// get multi values of key's fields(Asynchronous) 227 /// </summary> 228 /// <param name="key">key</param> 229 /// <param name="fields">fields of key</param> 230 /// <param name="flags">behaviour</param> 231 /// <param name="db">index of database</param> 232 /// <returns></returns> 233 Task<RedisValue[]> HMGetAsync(RedisKey key, List<RedisValue> fields, CommandFlags flags = CommandFlags.None, int db = 0); 234 #endregion 235 236 #region hmset 237 /// <summary> 238 /// set multi values of key's fields 239 /// </summary> 240 /// <param name="key">key</param> 241 /// <param name="entry">name/value pair</param> 242 /// <param name="flags">behaviour</param> 243 /// <param name="db">index of database</param> 244 void HMSet(RedisKey key, List<HashEntry> entry, CommandFlags flags = CommandFlags.None, int db = 0); 245 /// <summary> 246 /// set multi values of key's fields(Asynchronous) 247 /// </summary> 248 /// <param name="key">key</param> 249 /// <param name="entry">name/value pair</param> 250 /// <param name="flags">behaviour</param> 251 /// <param name="db">index of database</param> 252 Task HMSetAsync(RedisKey key, List<HashEntry> entry, CommandFlags flags = CommandFlags.None, int db = 0); 253 #endregion 254 255 #region hkeys 256 /// <summary> 257 /// get all the fields of the key 258 /// </summary> 259 /// <param name="key">key</param> 260 /// <param name="flags">behaviour</param> 261 /// <param name="db">index of database</param> 262 /// <returns></returns> 263 IList<RedisValue> HKeys(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 264 /// <summary> 265 /// get all the fields of the key(Asynchronous) 266 /// </summary> 267 /// <param name="key">key</param> 268 /// <param name="flags">behaviour</param> 269 /// <param name="db">index of database</param> 270 /// <returns></returns> 271 Task<RedisValue[]> HKeysAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 272 #endregion 273 274 #region hvals 275 /// <summary> 276 /// get all the values of key's fields 277 /// </summary> 278 /// <param name="key">key</param> 279 /// <param name="flags">behaviour</param> 280 /// <param name="db">index of database</param> 281 /// <returns></returns> 282 IList<RedisValue> HVals(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 283 /// <summary> 284 /// get all the values of key's fields(Asynchronous) 285 /// </summary> 286 /// <param name="key">key</param> 287 /// <param name="flags">behaviour</param> 288 /// <param name="db">index of database</param> 289 /// <returns></returns> 290 Task<RedisValue[]> HValsAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 291 #endregion 292 293 #region hincr hincrby hincrbyfloat 294 /// <summary> 295 /// handle the numeric value 296 /// </summary> 297 /// <param name="key">key</param> 298 /// <param name="hashField">field of key</param> 299 /// <param name="amount">value to increase or decrease</param> 300 /// <param name="flags">behaviour</param> 301 /// <param name="db">index of database</param> 302 /// <returns></returns> 303 double HIncrOrDecrBy(RedisKey key, RedisValue hashField, double amount = 1, CommandFlags flags = CommandFlags.None, int db = 0); 304 /// <summary> 305 /// handle the numeric value(Asynchronous) 306 /// </summary> 307 /// <param name="key">key</param> 308 /// <param name="hashField">field of key</param> 309 /// <param name="amount">value to increase or decrease</param> 310 /// <param name="flags">behaviour</param> 311 /// <param name="db">index of database</param> 312 /// <returns></returns> 313 Task<double> HIncrOrDecrByAsync(RedisKey key, RedisValue hashField, double amount = 1, CommandFlags flags = CommandFlags.None, int db = 0); 314 #endregion 315 316 #region hexists 317 /// <summary> 318 /// whether a field exists in the key 319 /// </summary> 320 /// <param name="key">key</param> 321 /// <param name="hashField">field of key</param> 322 /// <param name="flags">behaviour</param> 323 /// <param name="db">index of database</param> 324 /// <returns></returns> 325 bool HExists(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None, int db = 0); 326 /// <summary> 327 /// whether a field exists in the key(Asynchronous) 328 /// </summary> 329 /// <param name="key">key</param> 330 /// <param name="hashField">field of key</param> 331 /// <param name="flags">behaviour</param> 332 /// <param name="db">index of database</param> 333 /// <returns></returns> 334 Task<bool> HExistsAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None, int db = 0); 335 #endregion 336 337 #region hdel 338 /// <summary> 339 /// delete the field from the key 340 /// </summary> 341 /// <param name="key">key</param> 342 /// <param name="hashField">field of key</param> 343 /// <param name="flags">behaviour</param> 344 /// <param name="db">index of database</param> 345 /// <returns></returns> 346 bool HDel(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None, int db = 0); 347 /// <summary> 348 /// delete the field from the key(Asynchronous) 349 /// </summary> 350 /// <param name="key">key</param> 351 /// <param name="hashField">field of key</param> 352 /// <param name="flags">behaviour</param> 353 /// <param name="db">index of database</param> 354 /// <returns></returns> 355 Task<bool> HDelAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None, int db = 0); 356 /// <summary> 357 /// delete fields from the key 358 /// </summary> 359 /// <param name="key">key</param> 360 /// <param name="hashFields">fields of key</param> 361 /// <param name="flags">behaviour</param> 362 /// <param name="db">index of database</param> 363 /// <returns></returns> 364 long HDel(RedisKey key, IList<RedisValue> hashFields, CommandFlags flags = CommandFlags.None, int db = 0); 365 /// <summary> 366 /// delete fields from the key(Asynchronous) 367 /// </summary> 368 /// <param name="key">key</param> 369 /// <param name="hashFields">fields of key</param> 370 /// <param name="flags">behaviour</param> 371 /// <param name="db">index of database</param> 372 /// <returns></returns> 373 Task<long> HDelAsync(RedisKey key, IList<RedisValue> hashFields, CommandFlags flags = CommandFlags.None, int db = 0); 374 #endregion 375 #endregion 376 377 #region List 378 379 #region lpush 380 /// <summary> 381 /// insert the value to the head of list 382 /// </summary> 383 /// <param name="key">key</param> 384 /// <param name="value">node's value</param> 385 /// <param name="when">when this operation should be performed</param> 386 /// <param name="flags">behaviour</param> 387 /// <param name="db">index of database</param> 388 /// <returns></returns> 389 long LPush(RedisKey key, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 390 /// <summary> 391 /// insert the value to the head of list(Asynchronous) 392 /// </summary> 393 /// <param name="key">key</param> 394 /// <param name="value">node's value</param> 395 /// <param name="when">when this operation should be performed</param> 396 /// <param name="flags">behaviour</param> 397 /// <param name="db">index of database</param> 398 /// <returns></returns> 399 Task<long> LPushAsync(RedisKey key, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 400 #endregion 401 402 #region rpush 403 /// <summary> 404 /// insert the value to the tail of list 405 /// </summary> 406 /// <param name="key">key</param> 407 /// <param name="value">node's value</param> 408 /// <param name="when">when this operation should be performed</param> 409 /// <param name="flags">behaviour</param> 410 /// <param name="db">index of database</param> 411 /// <returns></returns> 412 long RPush(RedisKey key, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 413 /// <summary> 414 /// insert the value to the tail of list(Asynchronous) 415 /// </summary> 416 /// <param name="key">key</param> 417 /// <param name="value">node's value</param> 418 /// <param name="when">when this operation should be performed</param> 419 /// <param name="flags">behaviour</param> 420 /// <param name="db">index of database</param> 421 /// <returns></returns> 422 Task<long> RPushAsync(RedisKey key, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0); 423 #endregion 424 425 #region lpop 426 /// <summary> 427 /// removes the first element of the list 428 /// </summary> 429 /// <param name="key">key</param> 430 /// <param name="flags">behaviour</param> 431 /// <param name="db">index of database</param> 432 /// <returns></returns> 433 RedisValue LPop(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 434 /// <summary> 435 /// removes the first element of the list(Asynchronous) 436 /// </summary> 437 /// <param name="key">key</param> 438 /// <param name="flags">behaviour</param> 439 /// <param name="db">index of database</param> 440 /// <returns></returns> 441 Task<RedisValue> LPopAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 442 #endregion 443 444 #region lrem 445 /// <summary> 446 /// removes the first count occurrences of elements equal to value from the list 447 /// </summary> 448 /// <param name="key">key</param> 449 /// <param name="value">value</param> 450 /// <param name="count">amount of the node's value equal to the value</param> 451 /// <param name="flags">behaviour</param> 452 /// <param name="db">index of database</param> 453 /// <returns></returns> 454 long LRem(RedisKey key, RedisValue value, long count = 0, CommandFlags flags = CommandFlags.None, int db = 0); 455 /// <summary> 456 /// removes the first count occurrences of elements equal to value from the list(Asynchronous) 457 /// </summary> 458 /// <param name="key">key</param> 459 /// <param name="value">value</param> 460 /// <param name="count">amount of the node's value equal to the value</param> 461 /// <param name="flags">behaviour</param> 462 /// <param name="db">index of database</param> 463 /// <returns></returns> 464 Task<long> LRemAsync(RedisKey key, RedisValue value, long count = 0, CommandFlags flags = CommandFlags.None, int db = 0); 465 #endregion 466 467 #region lrange 468 /// <summary> 469 /// get the specified elements of the list stored at key 470 /// </summary> 471 /// <param name="key">key</param> 472 /// <param name="start">start index</param> 473 /// <param name="stop">stop index</param> 474 /// <param name="flags">behaviour</param> 475 /// <param name="db">index of database</param> 476 /// <returns></returns> 477 IList<RedisValue> LRange(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0); 478 /// <summary> 479 /// get the specified elements of the list stored at key(Asynchronous) 480 /// </summary> 481 /// <param name="key">key</param> 482 /// <param name="start">start index</param> 483 /// <param name="stop">stop index</param> 484 /// <param name="flags">behaviour</param> 485 /// <param name="db">index of database</param> 486 /// <returns></returns> 487 Task<RedisValue[]> LRangeAsync(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0); 488 #endregion 489 490 #region llen 491 /// <summary> 492 /// get the length of the list 493 /// </summary> 494 /// <param name="key">key</param> 495 /// <param name="flags">behaviour</param> 496 /// <param name="db">index of database</param> 497 /// <returns></returns> 498 long LLen(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 499 /// <summary> 500 /// get the length of the list(Asynchronous) 501 /// </summary> 502 /// <param name="key">key</param> 503 /// <param name="flags">behaviour</param> 504 /// <param name="db">index of database</param> 505 /// <returns></returns> 506 Task<long> LLenAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 507 #endregion 508 509 /// <summary> 510 /// get the element at index index in the list 511 /// </summary> 512 /// <param name="key">key</param> 513 /// <param name="index">index</param> 514 /// <param name="flags">behaviour</param> 515 /// <param name="db">index of database</param> 516 /// <returns></returns> 517 RedisValue LIndex(RedisKey key, long index, CommandFlags flags = CommandFlags.None, int db = 0); 518 /// <summary> 519 /// get the element at index index in the list(Asynchronous) 520 /// </summary> 521 /// <param name="key">key</param> 522 /// <param name="index">index</param> 523 /// <param name="flags">behaviour</param> 524 /// <param name="db">index of database</param> 525 /// <returns></returns> 526 Task<RedisValue> LIndexAsync(RedisKey key, long index, CommandFlags flags = CommandFlags.None, int db = 0); 527 528 #region LInsert 529 /// <summary> 530 /// inserts value in the list stored at key either before or after the reference value pivot. 531 /// </summary> 532 /// <param name="key"></param> 533 /// <param name="pivot"></param> 534 /// <param name="value"></param> 535 /// <param name="flags"></param> 536 /// <param name="isAfter"></param> 537 /// <param name="db"></param> 538 /// <returns></returns> 539 long LInsert(RedisKey key, RedisValue pivot, RedisValue value, CommandFlags flags = CommandFlags.None, bool isAfter = false, int db = 0); 540 /// <summary> 541 /// inserts value in the list stored at key either before or after the reference value pivot. 542 /// </summary> 543 /// <param name="key"></param> 544 /// <param name="pivot"></param> 545 /// <param name="value"></param> 546 /// <param name="flags"></param> 547 /// <param name="isAfter"></param> 548 /// <param name="db"></param> 549 /// <returns></returns> 550 Task<long> LInsertAsync(RedisKey key, RedisValue pivot, RedisValue value, CommandFlags flags = CommandFlags.None, bool isAfter = false, int db = 0); 551 #endregion 552 553 #endregion 554 555 #region Set 556 #region sadd 557 /// <summary> 558 /// add a member to a set 559 /// </summary> 560 /// <param name="key">key of set</param> 561 /// <param name="value">value of the key</param> 562 /// <param name="flags">behaviour</param> 563 /// <param name="db">index of database</param> 564 /// <returns></returns> 565 bool SAdd(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0); 566 /// <summary> 567 /// add a member to a set(Asynchronous) 568 /// </summary> 569 /// <param name="key">key of set</param> 570 /// <param name="value">value of the key</param> 571 /// <param name="flags">behaviour</param> 572 /// <param name="db">index of database</param> 573 /// <returns></returns> 574 Task<bool> SAddAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0); 575 /// <summary> 576 /// add multi members to a set 577 /// </summary> 578 /// <param name="key">key of set</param> 579 /// <param name="values">values of the key</param> 580 /// <param name="flags">behaviour</param> 581 /// <param name="db">index of database</param> 582 /// <returns></returns> 583 long SAdd(RedisKey key, IList<RedisValue> values, CommandFlags flags = CommandFlags.None, int db = 0); 584 /// <summary> 585 /// add multi members to a set(Asynchronous) 586 /// </summary> 587 /// <param name="key">key of set</param> 588 /// <param name="values">values of the key</param> 589 /// <param name="flags">behaviour</param> 590 /// <param name="db">index of database</param> 591 /// <returns></returns> 592 Task<long> SAddAsync(RedisKey key, IList<RedisValue> values, CommandFlags flags = CommandFlags.None, int db = 0); 593 #endregion 594 595 #region sismember 596 /// <summary> 597 /// whether member is a member of the set 598 /// </summary> 599 /// <param name="key">key of set</param> 600 /// <param name="value">value</param> 601 /// <param name="flags">behaviour</param> 602 /// <param name="db">index of database</param> 603 /// <returns></returns> 604 bool SIsMember(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0); 605 /// <summary> 606 /// whether member is a member of the set (Asynchronous) 607 /// </summary> 608 /// <param name="key">key of set</param> 609 /// <param name="value">value</param> 610 /// <param name="flags">behaviour</param> 611 /// <param name="db">index of database</param> 612 /// <returns></returns> 613 Task<bool> SIsMemberAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0); 614 #endregion 615 616 #region smembers 617 /// <summary> 618 /// get all the members of the set 619 /// </summary> 620 /// <param name="key">key of set</param> 621 /// <param name="flags">behaviour</param> 622 /// <param name="db">index of database</param> 623 /// <returns></returns> 624 IList<RedisValue> SMembers(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 625 /// <summary> 626 /// get all the members of the set(Asynchronous) 627 /// </summary> 628 /// <param name="key">key of set</param> 629 /// <param name="flags">behaviour</param> 630 /// <param name="db">index of database</param> 631 /// <returns></returns> 632 Task<RedisValue[]> SMembersAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 633 #endregion 634 635 #region spop 636 /// <summary> 637 /// Removes a random elements from the set 638 /// </summary> 639 /// <param name="key">key of set</param> 640 /// <param name="flags">behaviour</param> 641 /// <param name="db">index of database</param> 642 /// <returns></returns> 643 RedisValue SPop(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 644 /// <summary> 645 /// Removes a random elements from the set(Asynchronous) 646 /// </summary> 647 /// <param name="key">key of set</param> 648 /// <param name="flags">behaviour</param> 649 /// <param name="db">index of database</param> 650 /// <returns></returns> 651 Task<RedisValue> SPopAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 652 #endregion 653 654 #region srem 655 /// <summary> 656 /// Remove the specified members from the set 657 /// </summary> 658 /// <param name="key">key of the set</param> 659 /// <param name="value">member to remove</param> 660 /// <param name="flags">behaviour</param> 661 /// <param name="db">index of database</param> 662 /// <returns></returns> 663 bool SRem(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0); 664 /// <summary> 665 /// Remove the specified members from the set(Asynchronous) 666 /// </summary> 667 /// <param name="key">key of the set</param> 668 /// <param name="value">member to remove</param> 669 /// <param name="flags">behaviour</param> 670 /// <param name="db">index of database</param> 671 /// <returns></returns> 672 Task<bool> SRemAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0); 673 /// <summary> 674 /// Remove the specified members from the set 675 /// </summary> 676 /// <param name="key">key of the set</param> 677 /// <param name="value">member to remove</param> 678 /// <param name="flags">behaviour</param> 679 /// <param name="db">index of database</param> 680 /// <returns></returns> 681 long SRem(RedisKey key, IList<RedisValue> values, CommandFlags flags = CommandFlags.None, int db = 0); 682 /// <summary> 683 /// Remove the specified members from the set(Asynchronous) 684 /// </summary> 685 /// <param name="key">key of the set</param> 686 /// <param name="value">member to remove</param> 687 /// <param name="flags">behaviour</param> 688 /// <param name="db">index of database</param> 689 /// <returns></returns> 690 Task<long> SRemAsync(RedisKey key, IList<RedisValue> values, CommandFlags flags = CommandFlags.None, int db = 0); 691 #endregion 692 693 #region scard 694 /// <summary> 695 /// get the number of elements in the set 696 /// </summary> 697 /// <param name="key">key of set</param> 698 /// <param name="flags">behaviour</param> 699 /// <param name="db">index of database</param> 700 /// <returns></returns> 701 long SCard(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 702 /// <summary> 703 /// get the number of elements in the set(Asynchronous) 704 /// </summary> 705 /// <param name="key">key of set</param> 706 /// <param name="flags">behaviour</param> 707 /// <param name="db">index of database</param> 708 /// <returns></returns> 709 Task<long> SCardAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0); 710 #endregion 711 712 #region sinter 713 /// <summary> 714 /// get the members of the set resulting from the intersection of all the given sets. 715 /// </summary> 716 /// <param name="keys">keys</param> 717 /// <param name="flags">behaviour</param> 718 /// <param name="db">index of database</param> 719 /// <returns></returns> 720 IList<RedisValue> SInter(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0); 721 /// <summary> 722 /// get the members of the set resulting from the intersection of all the given sets.(Asynchronous) 723 /// </summary> 724 /// <param name="keys">keys</param> 725 /// <param name="flags">behaviour</param> 726 /// <param name="db">index of database</param> 727 /// <returns></returns> 728 Task<RedisValue[]> SInterAsync(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0); 729 #endregion 730 731 #region sdiff 732 /// <summary> 733 /// get the members of the set resulting from the difference between the first set and all the successive sets. 734 /// </summary> 735 /// <param name="keys">keys</param> 736 /// <param name="flags">behaviour</param> 737 /// <param name="db">index of database</param> 738 /// <returns></returns> 739 IList<RedisValue> SDiff(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0); 740 /// <summary> 741 /// get the members of the set resulting from the difference between the first set and all the successive sets.(Asynchronous) 742 /// </summary> 743 /// <param name="keys">keys</param> 744 /// <param name="flags">behaviour</param> 745 /// <param name="db">index of database</param> 746 /// <returns></returns> 747 Task<RedisValue[]> SDiffAsync(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0); 748 #endregion 749 750 #region sunion 751 /// <summary> 752 /// get the members of the set resulting from the union of all the given sets. 753 /// </summary> 754 /// <param name="keys">keys</param> 755 /// <param name="flags">behaviour</param> 756 /// <param name="db">index of database</param> 757 /// <returns></returns> 758 IList<RedisValue> SUnion(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0); 759 /// <summary> 760 /// get the members of the set resulting from the union of all the given sets.(Asynchronous) 761 /// </summary> 762 /// <param name="keys">keys</param> 763 /// <param name="flags">behaviour</param> 764 /// <param name="db">index of database</param> 765 /// <returns></returns> 766 Task<RedisValue[]> SUnionAsync(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0); 767 #endregion 768 #endregion 769 770 #region SortedSet 771 #region zadd 772 /// <summary> 773 /// Adds a member with the score to the sorted set stored at key 774 /// </summary> 775 /// <param name="key">key</param> 776 /// <param name="member">member</param> 777 /// <param name="score">score</param> 778 /// <param name="flags">behaviour</param> 779 /// <param name="db">index of database</param> 780 /// <returns></returns> 781 bool ZAdd(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None, int db = 0); 782 /// <summary> 783 /// Adds a member with the score to the sorted set stored at key(Asynchronous) 784 /// </summary> 785 /// <param name="key">key</param> 786 /// <param name="member">member</param> 787 /// <param name="score">score</param> 788 /// <param name="flags">behaviour</param> 789 /// <param name="db">index of database</param> 790 /// <returns></returns> 791 Task<bool> ZAddAsync(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None, int db = 0); 792 /// <summary> 793 /// Adds members with scores to the sorted set stored at key 794 /// </summary> 795 /// <param name="key">key</param> 796 /// <param name="values">sortedset entity</param> 797 /// <param name="flags">behaviour</param> 798 /// <param name="db">index of database</param> 799 /// <returns></returns> 800 long ZAdd(RedisKey key, IList<SortedSetEntry> values, CommandFlags flags = CommandFlags.None, int db = 0); 801 /// <summary> 802 /// Adds members with scores to the sorted set stored at key(Asynchronous) 803 /// </summary> 804 /// <param name="key">key</param> 805 /// <param name="values">sortedset entity</param> 806 /// <param name="flags">behaviour</param> 807 /// <param name="db">index of database</param> 808 /// <returns></returns> 809 Task<long> ZAddAsync(RedisKey key, IList<SortedSetEntry> values, CommandFlags flags = CommandFlags.None, int db = 0); 810 #endregion 811 812 #region zrem 813 /// <summary> 814 /// Removes a member from the sorted set stored at key 815 /// </summary> 816 /// <param name="key">key</param> 817 /// <param name="member">member</param> 818 /// <param name="flags">behaviour</param> 819 /// <param name="db">index of database</param> 820 /// <returns></returns> 821 bool ZRem(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None, int db = 0); 822 /// <summary> 823 /// Removes a member from the sorted set stored at key(Async) 824 /// </summary> 825 /// <param name="key">key</param> 826 /// <param name="member">member</param> 827 /// <param name="flags">behaviour</param> 828 /// <param name="db">index of database</param> 829 /// <returns></returns> 830 Task<bool> ZRemAsync(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None, int db = 0); 831 /// <summary> 832 /// Removes members from the sorted set stored at key 833 /// </summary> 834 /// <param name="key">key</param> 835 /// <param name="members">members</param> 836 /// <param name="flags">behaviour</param> 837 /// <param name="db">index of database</param> 838 /// <returns></returns> 839 long ZRem(RedisKey key, IList<RedisValue> members, CommandFlags flags = CommandFlags.None, int db = 0); 840 /// <summary> 841 /// Removes members from the sorted set stored at key(Asynchronous) 842 /// </summary> 843 /// <param name="key">key</param> 844 /// <param name="members">members</param> 845 /// <param name="flags">behaviour</param> 846 /// <param name="db">index of database</param> 847 /// <returns></returns> 848 Task<long> ZRemAsync(RedisKey key, IList<RedisValue> members, CommandFlags flags = CommandFlags.None, int db = 0); 849 #endregion 850 851 #region zrange 852 IList<RedisValue> ZRange(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0); 853 854 Task<RedisValue[]> ZRangeAsync(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0); 855 #endregion 856 857 #region zrevrange 858 IList<RedisValue> ZRevRange(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0); 859 860 Task<RedisValue[]> ZRevRangeAsync(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0); 861 #endregion 862 863 #region zincrby 864 double ZIncrby(RedisKey key, RedisValue member, double value = 1, CommandFlags flags = CommandFlags.None, int db = 0); 865 866 Task<double> ZIncrbyAsync(RedisKey key, RedisValue member, double value = 1, CommandFlags flags = CommandFlags.None, int db = 0); 867 #endregion 868 #endregion 869 870 #region pub/sub 871 void Subscribe(RedisChannel channel, Action<RedisChannel, RedisValue> handle, CommandFlags flags = CommandFlags.None, bool isRead = false); 872 873 long Publish(RedisChannel channel, RedisValue value, CommandFlags flags = CommandFlags.None, bool isRead = false); 874 875 void UnSubscrribe(RedisChannel channel, Action<RedisChannel, RedisValue> handle, CommandFlags flags = CommandFlags.None, bool isRead = false); 876 #endregion 877 }
The Implementation are as follow:
1 using Microsoft.Extensions.Configuration; 2 using Newtonsoft.Json; 3 using StackExchange.Redis; 4 using System; 5 using System.Collections.Generic; 6 using System.IO; 7 using System.Linq; 8 using System.Threading.Tasks; 9 10 namespace Redis01 11 { 12 public class RedisHelper : IRedis 13 { 14 private static IConfigurationRoot GetConnStr() 15 { 16 var builder = new ConfigurationBuilder(); 17 builder.SetBasePath(Directory.GetCurrentDirectory()); 18 builder.AddJsonFile("appsettings.json"); 19 var config = builder.Build(); 20 return config; 21 } 22 23 private Lazy<ConnectionMultiplexer> _writeConn = new Lazy<ConnectionMultiplexer>(() => 24 { 25 return ConnectionMultiplexer.Connect(GetConnStr().GetValue<string>("RedisConfig:MasterServer")); 26 }); 27 28 private Lazy<ConnectionMultiplexer> _readConn = new Lazy<ConnectionMultiplexer>(() => 29 { 30 return ConnectionMultiplexer.Connect(GetConnStr().GetValue<string>("RedisConfig:SlaveServer")); 31 }); 32 33 public ConnectionMultiplexer WriteConn 34 { 35 get { return _writeConn.Value; } 36 } 37 38 public ConnectionMultiplexer ReadConn 39 { 40 get { return _readConn.Value; } 41 } 42 43 private IDatabase GetDatabase(int db = 0, bool isRead = false) 44 { 45 return isRead ? 46 ReadConn.GetDatabase(db) : 47 WriteConn.GetDatabase(db); 48 } 49 50 51 private ISubscriber GetSubscriber(bool isRead = false, object asyncState = null) 52 { 53 return isRead ? 54 ReadConn.GetSubscriber(asyncState) : 55 WriteConn.GetSubscriber(asyncState); 56 } 57 58 public ITransaction GetTransaction(int db = 0, bool isRead = false) 59 { 60 return GetDatabase(db).CreateTransaction(); 61 } 62 63 #region string 64 /// <summary> 65 /// get the string value 66 /// </summary> 67 /// <param name="key">the key of value</param> 68 /// <param name="flag">behaviour</param> 69 /// <param name="db">index of database</param> 70 /// <returns></returns> 71 public RedisValue Get(string key, CommandFlags flag = CommandFlags.None, int db = 0) 72 { 73 return GetDatabase(db, true).StringGet(key, flag); 74 } 75 /// <summary> 76 /// get the string value(Asynchronous) 77 /// </summary> 78 /// <param name="key">the key of value</param> 79 /// <param name="flag">behaviour</param> 80 /// <param name="db">index of database</param> 81 /// <returns></returns> 82 public Task<RedisValue> GetAsync(string key, CommandFlags flag = CommandFlags.None, int db = 0) 83 { 84 return GetDatabase(db, true).StringGetAsync(key, flag); 85 } 86 87 /// <summary> 88 /// set value to key 89 /// </summary> 90 /// <param name="key">the key</param> 91 /// <param name="value">the value of the key</param> 92 /// <param name="expiry">time to expiry</param> 93 /// <param name="when">when this operation should be performed</param> 94 /// <param name="flags">behaviour</param> 95 /// <param name="db">index of database</param> 96 /// <returns></returns> 97 public RedisValue Set(string key, string value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 98 { 99 return GetDatabase(db).StringSet(key, value, expiry, when, flags); 100 } 101 /// <summary> 102 /// set value to key(Asynchronous) 103 /// </summary> 104 /// <param name="key">the key</param> 105 /// <param name="value">the value of the key</param> 106 /// <param name="expiry">time to expiry</param> 107 /// <param name="when">when this operation should be performed</param> 108 /// <param name="flags">behaviour</param> 109 /// <param name="db">index of database</param> 110 /// <returns></returns> 111 public Task<bool> SetAsync(string key, string value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 112 { 113 return GetDatabase(db).StringSetAsync(key, value, expiry, when, flags); 114 } 115 116 /// <summary> 117 /// get the entity by deserialization 118 /// </summary> 119 /// <param name="key">the key of value</param> 120 /// <param name="flag">behaviour</param> 121 /// <param name="db">index of database</param> 122 /// <returns></returns> 123 public T Get<T>(string key, CommandFlags flags = CommandFlags.None, int db = 0) 124 { 125 try 126 { 127 return JsonConvert.DeserializeObject<T>(GetDatabase(db, true).StringGet(key, flags)); 128 } 129 catch (Exception ex) 130 { 131 return default(T); 132 } 133 } 134 /// <summary> 135 /// get the entity by deserialization(Asynchronous) 136 /// </summary> 137 /// <param name="key">the key of value</param> 138 /// <param name="flag">behaviour</param> 139 /// <param name="db">index of database</param> 140 /// <returns></returns> 141 public Task<T> GetAsync<T>(string key, CommandFlags flags = CommandFlags.None, int db = 0) 142 { 143 try 144 { 145 var res = GetDatabase(db, true).StringGetAsync(key, flags); 146 return JsonConvert.DeserializeObject<Task<T>>(res.ToString()); 147 } 148 catch (Exception ex) 149 { 150 return default(Task<T>); 151 } 152 } 153 154 /// <summary> 155 /// get multi values 156 /// </summary> 157 /// <param name="keys">the keys of the values</param> 158 /// <param name="flags">behaviour</param> 159 /// <param name="db">index of database</param> 160 /// <returns></returns> 161 public IList<RedisValue> MGet(List<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0) 162 { 163 return GetDatabase(db, true).StringGet(keys.ToArray(), flags); 164 } 165 /// <summary> 166 /// get multi values(Asynchronous) 167 /// </summary> 168 /// <param name="keys">the keys of the values</param> 169 /// <param name="flags">behaviour</param> 170 /// <param name="db">index of database</param> 171 /// <returns></returns> 172 public Task<RedisValue[]> MGetAsync(List<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0) 173 { 174 return GetDatabase(db, true).StringGetAsync(keys.ToArray(), flags); 175 } 176 177 /// <summary> 178 /// set multi values 179 /// </summary> 180 /// <param name="kvs">key-values</param> 181 /// <param name="when">when this operation should be performed</param> 182 /// <param name="flags">behaviour</param> 183 /// <param name="db">index of database</param> 184 /// <returns></returns> 185 public bool MSet(List<KeyValuePair<RedisKey, RedisValue>> kvs, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 186 { 187 return GetDatabase(db).StringSet(kvs.ToArray(), when, flags); 188 } 189 /// <summary> 190 /// set multi values(Asynchronous) 191 /// </summary> 192 /// <param name="kvs">key-values</param> 193 /// <param name="when">when this operation should be performed</param> 194 /// <param name="flags">behaviour</param> 195 /// <param name="db">index of database</param> 196 /// <returns></returns> 197 public Task<bool> MSetAsync(List<KeyValuePair<RedisKey, RedisValue>> kvs, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 198 { 199 return GetDatabase(db).StringSetAsync(kvs.ToArray(), when, flags); 200 } 201 202 /// <summary> 203 /// handle the numeric value 204 /// </summary> 205 /// <param name="key">key</param> 206 /// <param name="amount">value to increase or decrease</param> 207 /// <param name="flags">behaviour</param> 208 /// <param name="db">index of database</param> 209 /// <returns></returns> 210 public double IncrOrDecrBy(RedisKey key, double amount, CommandFlags flags = CommandFlags.None, int db = 0) 211 { 212 return GetDatabase(db).StringIncrement(key, amount, flags); 213 } 214 /// <summary> 215 /// handle the numeric value(Asynchronous) 216 /// </summary> 217 /// <param name="key">key</param> 218 /// <param name="amount">value to increase or decrease</param> 219 /// <param name="flags">behaviour</param> 220 /// <param name="db">index of database</param> 221 /// <returns></returns> 222 public Task<double> IncrOrDecrByAsync(RedisKey key, double value, CommandFlags flags = CommandFlags.None, int db = 0) 223 { 224 return GetDatabase(db).StringIncrementAsync(key, value, flags); 225 } 226 227 /// <summary> 228 /// append value to the key 229 /// </summary> 230 /// <param name="key">key</param> 231 /// <param name="value">value to append</param> 232 /// <param name="flags">behaviour</param> 233 /// <param name="db">index of database</param> 234 /// <returns></returns> 235 public long Append(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0) 236 { 237 return GetDatabase(db).StringAppend(key, value, flags); 238 } 239 /// <summary> 240 /// append value to the key(Asynchronous) 241 /// </summary> 242 /// <param name="key">key</param> 243 /// <param name="value">value to append</param> 244 /// <param name="flags">behaviour</param> 245 /// <param name="db">index of database</param> 246 /// <returns></returns> 247 public Task<long> AppendAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0) 248 { 249 return GetDatabase(db).StringAppendAsync(key, value, flags); 250 } 251 252 /// <summary> 253 /// get the value's length by the key 254 /// </summary> 255 /// <param name="key">key</param> 256 /// <param name="flags">behaviour</param> 257 /// <param name="db">index of database</param> 258 /// <returns></returns> 259 public long StrLen(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 260 { 261 return GetDatabase(db).StringLength(key, flags); 262 } 263 /// <summary> 264 /// get the value's length by the key(Asynchronous) 265 /// </summary> 266 /// <param name="key">key</param> 267 /// <param name="flags">behaviour</param> 268 /// <param name="db">index of database</param> 269 /// <returns></returns> 270 public Task<long> StrLenAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 271 { 272 return GetDatabase(db).StringLengthAsync(key, flags); 273 } 274 #endregion 275 276 #region Hash 277 /// <summary> 278 /// get the value of key's field 279 /// </summary> 280 /// <param name="key">key</param> 281 /// <param name="field">field of key</param> 282 /// <param name="flags">behaviour</param> 283 /// <param name="db">index of database</param> 284 /// <returns></returns> 285 public RedisValue HGet(RedisKey key, RedisValue field, CommandFlags flags = CommandFlags.None, int db = 0) 286 { 287 return GetDatabase(db, true).HashGet(key, field, CommandFlags.None); 288 } 289 /// <summary> 290 /// get the value of key's field(Asynchronous) 291 /// </summary> 292 /// <param name="key">key</param> 293 /// <param name="field">field of key</param> 294 /// <param name="flags">behaviour</param> 295 /// <param name="db">index of database</param> 296 /// <returns></returns> 297 public Task<RedisValue> HGetAsync(RedisKey key, RedisValue field, CommandFlags flags = CommandFlags.None, int db = 0) 298 { 299 return GetDatabase(db, true).HashGetAsync(key, field, CommandFlags.None); 300 } 301 302 /// <summary> 303 /// set the field and value of the key 304 /// </summary> 305 /// <param name="key">key</param> 306 /// <param name="field">field of the key</param> 307 /// <param name="value">value of the field</param> 308 /// <param name="when">when this operation should be performed</param> 309 /// <param name="flags">behaviour</param> 310 /// <param name="db">index of database</param> 311 /// <returns></returns> 312 public bool HSet(RedisKey key, RedisValue field, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 313 { 314 return GetDatabase(db).HashSet(key, field, value, When.Always, CommandFlags.None); 315 } 316 /// <summary> 317 /// set the field and value of the key 318 /// </summary> 319 /// <param name="key">key</param> 320 /// <param name="field">field of the key</param> 321 /// <param name="value">value of the field</param> 322 /// <param name="when">when this operation should be performed</param> 323 /// <param name="flags">behaviour</param> 324 /// <param name="db">index of database</param> 325 /// <returns></returns> 326 public Task<bool> HSetAsync(RedisKey key, RedisValue field, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 327 { 328 return GetDatabase(db).HashSetAsync(key, field, value, When.Always, CommandFlags.None); 329 } 330 331 /// <summary> 332 /// get multi values of key's fields 333 /// </summary> 334 /// <param name="key">key</param> 335 /// <param name="fields">fields of key</param> 336 /// <param name="flags">behaviour</param> 337 /// <param name="db">index of database</param> 338 /// <returns></returns> 339 public IList<RedisValue> HMGet(RedisKey key, List<RedisValue> fields, CommandFlags flags = CommandFlags.None, int db = 0) 340 { 341 return GetDatabase(db, true).HashGet(key, fields.ToArray(), flags); 342 } 343 /// <summary> 344 /// get multi values of key's fields(Asynchronous) 345 /// </summary> 346 /// <param name="key">key</param> 347 /// <param name="fields">fields of key</param> 348 /// <param name="flags">behaviour</param> 349 /// <param name="db">index of database</param> 350 /// <returns></returns> 351 public Task<RedisValue[]> HMGetAsync(RedisKey key, List<RedisValue> fields, CommandFlags flags = CommandFlags.None, int db = 0) 352 { 353 return GetDatabase(db, true).HashGetAsync(key, fields.ToArray(), flags); 354 } 355 356 /// <summary> 357 /// set multi values of key's fields 358 /// </summary> 359 /// <param name="key">key</param> 360 /// <param name="entry">name/value pair</param> 361 /// <param name="flags">behaviour</param> 362 /// <param name="db">index of database</param> 363 public void HMSet(RedisKey key, List<HashEntry> entry, CommandFlags flags = CommandFlags.None, int db = 0) 364 { 365 GetDatabase(db).HashSet(key, entry.ToArray(), flags); 366 } 367 /// <summary> 368 /// set multi values of key's fields(Asynchronous) 369 /// </summary> 370 /// <param name="key">key</param> 371 /// <param name="entry">name/value pair</param> 372 /// <param name="flags">behaviour</param> 373 /// <param name="db">index of database</param> 374 public Task HMSetAsync(RedisKey key, List<HashEntry> entry, CommandFlags flags = CommandFlags.None, int db = 0) 375 { 376 return GetDatabase(db).HashSetAsync(key, entry.ToArray(), flags); 377 } 378 379 /// <summary> 380 /// get all the fields of the key 381 /// </summary> 382 /// <param name="key">key</param> 383 /// <param name="flags">behaviour</param> 384 /// <param name="db">index of database</param> 385 /// <returns></returns> 386 public IList<RedisValue> HKeys(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 387 { 388 return GetDatabase(db, true).HashKeys(key, flags).ToList(); 389 } 390 /// <summary> 391 /// get all the fields of the key(Asynchronous) 392 /// </summary> 393 /// <param name="key">key</param> 394 /// <param name="flags">behaviour</param> 395 /// <param name="db">index of database</param> 396 /// <returns></returns> 397 public Task<RedisValue[]> HKeysAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 398 { 399 return GetDatabase(db, true).HashKeysAsync(key, flags); 400 } 401 402 /// <summary> 403 /// get all the values of key's fields 404 /// </summary> 405 /// <param name="key">key</param> 406 /// <param name="flags">behaviour</param> 407 /// <param name="db">index of database</param> 408 /// <returns></returns> 409 public IList<RedisValue> HVals(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 410 { 411 return GetDatabase(db, true).HashValues(key, flags).ToList(); 412 } 413 /// <summary> 414 /// get all the values of key's fields(Asynchronous) 415 /// </summary> 416 /// <param name="key">key</param> 417 /// <param name="flags">behaviour</param> 418 /// <param name="db">index of database</param> 419 /// <returns></returns> 420 public Task<RedisValue[]> HValsAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 421 { 422 return GetDatabase(db, true).HashValuesAsync(key, flags); 423 } 424 425 /// <summary> 426 /// handle the numeric value 427 /// </summary> 428 /// <param name="key">key</param> 429 /// <param name="hashField">field of key</param> 430 /// <param name="amount">value to increase or decrease</param> 431 /// <param name="flags">behaviour</param> 432 /// <param name="db">index of database</param> 433 /// <returns></returns> 434 public double HIncrOrDecrBy(RedisKey key, RedisValue hashField, double amount = 1, CommandFlags flags = CommandFlags.None, int db = 0) 435 { 436 return GetDatabase(db).HashIncrement(key, hashField, amount, flags); 437 } 438 /// <summary> 439 /// handle the numeric value(Asynchronous) 440 /// </summary> 441 /// <param name="key">key</param> 442 /// <param name="hashField">field of key</param> 443 /// <param name="amount">value to increase or decrease</param> 444 /// <param name="flags">behaviour</param> 445 /// <param name="db">index of database</param> 446 /// <returns></returns> 447 public Task<double> HIncrOrDecrByAsync(RedisKey key, RedisValue hashField, double amount = 1, CommandFlags flags = CommandFlags.None, int db = 0) 448 { 449 return GetDatabase(db).HashIncrementAsync(key, hashField, amount, flags); 450 } 451 452 /// <summary> 453 /// whether a field exists in the key 454 /// </summary> 455 /// <param name="key">key</param> 456 /// <param name="hashField">field of key</param> 457 /// <param name="flags">behaviour</param> 458 /// <param name="db">index of database</param> 459 /// <returns></returns> 460 public bool HExists(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None, int db = 0) 461 { 462 return GetDatabase(0, true).HashExists(key, hashField, flags); 463 } 464 /// <summary> 465 /// whether a field exists in the key(Asynchronous) 466 /// </summary> 467 /// <param name="key">key</param> 468 /// <param name="hashField">field of key</param> 469 /// <param name="flags">behaviour</param> 470 /// <param name="db">index of database</param> 471 /// <returns></returns> 472 public Task<bool> HExistsAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None, int db = 0) 473 { 474 return GetDatabase(0, true).HashExistsAsync(key, hashField, flags); 475 } 476 477 /// <summary> 478 /// delete the field from the key 479 /// </summary> 480 /// <param name="key">key</param> 481 /// <param name="hashField">field of key</param> 482 /// <param name="flags">behaviour</param> 483 /// <param name="db">index of database</param> 484 /// <returns></returns> 485 public bool HDel(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None, int db = 0) 486 { 487 return GetDatabase(db).HashDelete(key, hashField, flags); 488 } 489 /// <summary> 490 /// delete the field from the key(Asynchronous) 491 /// </summary> 492 /// <param name="key">key</param> 493 /// <param name="hashField">field of key</param> 494 /// <param name="flags">behaviour</param> 495 /// <param name="db">index of database</param> 496 /// <returns></returns> 497 public Task<bool> HDelAsync(RedisKey key, RedisValue hashField, CommandFlags flags = CommandFlags.None, int db = 0) 498 { 499 return GetDatabase(db).HashDeleteAsync(key, hashField, flags); 500 } 501 /// <summary> 502 /// delete fields from the key 503 /// </summary> 504 /// <param name="key">key</param> 505 /// <param name="hashFields">fields of key</param> 506 /// <param name="flags">behaviour</param> 507 /// <param name="db">index of database</param> 508 /// <returns></returns> 509 public long HDel(RedisKey key, IList<RedisValue> hashFields, CommandFlags flags = CommandFlags.None, int db = 0) 510 { 511 return GetDatabase(db).HashDelete(key, hashFields.ToArray(), flags); 512 } 513 /// <summary> 514 /// delete fields from the key(Asynchronous) 515 /// </summary> 516 /// <param name="key">key</param> 517 /// <param name="hashFields">fields of key</param> 518 /// <param name="flags">behaviour</param> 519 /// <param name="db">index of database</param> 520 /// <returns></returns> 521 public Task<long> HDelAsync(RedisKey key, IList<RedisValue> hashFields, CommandFlags flags = CommandFlags.None, int db = 0) 522 { 523 return GetDatabase(db).HashDeleteAsync(key, hashFields.ToArray(), flags); 524 } 525 #endregion 526 527 #region List 528 /// <summary> 529 /// insert the value to the head of list 530 /// </summary> 531 /// <param name="key">key</param> 532 /// <param name="value">node's value</param> 533 /// <param name="when">when this operation should be performed</param> 534 /// <param name="flags">behaviour</param> 535 /// <param name="db">index of database</param> 536 /// <returns></returns> 537 public long LPush(RedisKey key, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 538 { 539 return GetDatabase(db).ListLeftPush(key, value, when, flags); 540 } 541 /// <summary> 542 /// insert the value to the head of list(Asynchronous) 543 /// </summary> 544 /// <param name="key">key</param> 545 /// <param name="value">node's value</param> 546 /// <param name="when">when this operation should be performed</param> 547 /// <param name="flags">behaviour</param> 548 /// <param name="db">index of database</param> 549 /// <returns></returns> 550 public Task<long> LPushAsync(RedisKey key, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 551 { 552 return GetDatabase(db).ListLeftPushAsync(key, value, when, flags); 553 } 554 555 /// <summary> 556 /// insert the value to the tail of list 557 /// </summary> 558 /// <param name="key">key</param> 559 /// <param name="value">node's value</param> 560 /// <param name="when">when this operation should be performed</param> 561 /// <param name="flags">behaviour</param> 562 /// <param name="db">index of database</param> 563 /// <returns></returns> 564 public long RPush(RedisKey key, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 565 { 566 return GetDatabase(db).ListRightPush(key, value, when, flags); 567 } 568 /// <summary> 569 /// insert the value to the tail of list(Asynchronous) 570 /// </summary> 571 /// <param name="key">key</param> 572 /// <param name="value">node's value</param> 573 /// <param name="when">when this operation should be performed</param> 574 /// <param name="flags">behaviour</param> 575 /// <param name="db">index of database</param> 576 /// <returns></returns> 577 public Task<long> RPushAsync(RedisKey key, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None, int db = 0) 578 { 579 return GetDatabase(db).ListRightPushAsync(key, value, when, flags); 580 } 581 582 /// <summary> 583 /// removes the first element of the list 584 /// </summary> 585 /// <param name="key">key</param> 586 /// <param name="flags">behaviour</param> 587 /// <param name="db">index of database</param> 588 /// <returns></returns> 589 public RedisValue LPop(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 590 { 591 return GetDatabase(db).ListLeftPop(key, flags); 592 } 593 /// <summary> 594 /// removes the first element of the list(Asynchronous) 595 /// </summary> 596 /// <param name="key">key</param> 597 /// <param name="flags">behaviour</param> 598 /// <param name="db">index of database</param> 599 /// <returns></returns> 600 public Task<RedisValue> LPopAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 601 { 602 return GetDatabase(db).ListLeftPopAsync(key, flags); 603 } 604 605 /// <summary> 606 /// removes the first count occurrences of elements equal to value from the list 607 /// </summary> 608 /// <param name="key">key</param> 609 /// <param name="value">value</param> 610 /// <param name="count">amount of the node's value equal to the value</param> 611 /// <param name="flags">behaviour</param> 612 /// <param name="db">index of database</param> 613 /// <returns></returns> 614 public long LRem(RedisKey key, RedisValue value, long count = 0, CommandFlags flags = CommandFlags.None, int db = 0) 615 { 616 return GetDatabase(db).ListRemove(key, value, count, flags); 617 } 618 /// <summary> 619 /// removes the first count occurrences of elements equal to value from the list(Asynchronous) 620 /// </summary> 621 /// <param name="key">key</param> 622 /// <param name="value">value</param> 623 /// <param name="count">amount of the node's value equal to the value</param> 624 /// <param name="flags">behaviour</param> 625 /// <param name="db">index of database</param> 626 /// <returns></returns> 627 public Task<long> LRemAsync(RedisKey key, RedisValue value, long count = 0, CommandFlags flags = CommandFlags.None, int db = 0) 628 { 629 return GetDatabase(db).ListRemoveAsync(key, value, count, flags); 630 } 631 632 /// <summary> 633 /// get the specified elements of the list stored at key 634 /// </summary> 635 /// <param name="key">key</param> 636 /// <param name="start">start index</param> 637 /// <param name="stop">stop index</param> 638 /// <param name="flags">behaviour</param> 639 /// <param name="db">index of database</param> 640 /// <returns></returns> 641 public IList<RedisValue> LRange(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0) 642 { 643 return GetDatabase(db, true).ListRange(key, start, stop, flags).ToList(); 644 } 645 /// <summary> 646 /// get the specified elements of the list stored at key(Asynchronous) 647 /// </summary> 648 /// <param name="key">key</param> 649 /// <param name="start">start index</param> 650 /// <param name="stop">stop index</param> 651 /// <param name="flags">behaviour</param> 652 /// <param name="db">index of database</param> 653 /// <returns></returns> 654 public Task<RedisValue[]> LRangeAsync(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0) 655 { 656 return GetDatabase(db, true).ListRangeAsync(key, start, stop, flags); 657 } 658 659 /// <summary> 660 /// get the length of the list 661 /// </summary> 662 /// <param name="key">key</param> 663 /// <param name="flags">behaviour</param> 664 /// <param name="db">index of database</param> 665 /// <returns></returns> 666 public long LLen(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 667 { 668 return GetDatabase(db, true).ListLength(key, flags); 669 } 670 /// <summary> 671 /// get the length of the list(Asynchronous) 672 /// </summary> 673 /// <param name="key">key</param> 674 /// <param name="flags">behaviour</param> 675 /// <param name="db">index of database</param> 676 /// <returns></returns> 677 public Task<long> LLenAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 678 { 679 return GetDatabase(db, true).ListLengthAsync(key, flags); 680 } 681 682 /// <summary> 683 /// get the element at index index in the list 684 /// </summary> 685 /// <param name="key">key</param> 686 /// <param name="index">index</param> 687 /// <param name="flags">behaviour</param> 688 /// <param name="db">index of database</param> 689 /// <returns></returns> 690 public RedisValue LIndex(RedisKey key, long index, CommandFlags flags = CommandFlags.None, int db = 0) 691 { 692 return GetDatabase(db, true).ListGetByIndex(key, index, flags); 693 } 694 /// <summary> 695 /// get the element at index index in the list(Asynchronous) 696 /// </summary> 697 /// <param name="key">key</param> 698 /// <param name="index">index</param> 699 /// <param name="flags">behaviour</param> 700 /// <param name="db">index of database</param> 701 /// <returns></returns> 702 public Task<RedisValue> LIndexAsync(RedisKey key, long index, CommandFlags flags = CommandFlags.None, int db = 0) 703 { 704 return GetDatabase(db, true).ListGetByIndexAsync(key, index, flags); 705 } 706 707 /// <summary> 708 /// inserts value in the list stored at key either before or after the reference value pivot. 709 /// </summary> 710 /// <param name="key"></param> 711 /// <param name="pivot"></param> 712 /// <param name="value"></param> 713 /// <param name="flags"></param> 714 /// <param name="isAfter"></param> 715 /// <param name="db"></param> 716 /// <returns></returns> 717 public long LInsert(RedisKey key, RedisValue pivot, RedisValue value, CommandFlags flags = CommandFlags.None, bool isAfter = false, int db = 0) 718 { 719 return isAfter 720 ? GetDatabase(db).ListInsertAfter(key, pivot, value, flags) 721 : GetDatabase(db).ListInsertBefore(key, pivot, value, flags); 722 } 723 /// <summary> 724 /// inserts value in the list stored at key either before or after the reference value pivot. 725 /// </summary> 726 /// <param name="key"></param> 727 /// <param name="pivot"></param> 728 /// <param name="value"></param> 729 /// <param name="flags"></param> 730 /// <param name="isAfter"></param> 731 /// <param name="db"></param> 732 /// <returns></returns> 733 public Task<long> LInsertAsync(RedisKey key, RedisValue pivot, RedisValue value, CommandFlags flags = CommandFlags.None, bool isAfter = false, int db = 0) 734 { 735 return isAfter 736 ? GetDatabase(db).ListInsertAfterAsync(key, pivot, value, flags) 737 : GetDatabase(db).ListInsertBeforeAsync(key, pivot, value, flags); 738 } 739 #endregion 740 741 #region Set 742 743 #region sadd 744 /// <summary> 745 /// add a member to a set 746 /// </summary> 747 /// <param name="key">key of set</param> 748 /// <param name="value">value of the key</param> 749 /// <param name="flags">behaviour</param> 750 /// <param name="db">index of database</param> 751 /// <returns></returns> 752 public bool SAdd(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0) 753 { 754 return GetDatabase(db).SetAdd(key, value, flags); 755 } 756 /// <summary> 757 /// add a member to a set(Asynchronous) 758 /// </summary> 759 /// <param name="key">key of set</param> 760 /// <param name="value">value of the key</param> 761 /// <param name="flags">behaviour</param> 762 /// <param name="db">index of database</param> 763 /// <returns></returns> 764 public Task<bool> SAddAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0) 765 { 766 return GetDatabase(db).SetAddAsync(key, value, flags); 767 } 768 /// <summary> 769 /// add multi members to a set 770 /// </summary> 771 /// <param name="key">key of set</param> 772 /// <param name="values">values of the key</param> 773 /// <param name="flags">behaviour</param> 774 /// <param name="db">index of database</param> 775 /// <returns></returns> 776 public long SAdd(RedisKey key, IList<RedisValue> values, CommandFlags flags = CommandFlags.None, int db = 0) 777 { 778 return GetDatabase(db).SetAdd(key, values.ToArray(), flags); 779 } 780 /// <summary> 781 /// add multi members to a set(Asynchronous) 782 /// </summary> 783 /// <param name="key">key of set</param> 784 /// <param name="values">values of the key</param> 785 /// <param name="flags">behaviour</param> 786 /// <param name="db">index of database</param> 787 /// <returns></returns> 788 public Task<long> SAddAsync(RedisKey key, IList<RedisValue> values, CommandFlags flags = CommandFlags.None, int db = 0) 789 { 790 return GetDatabase(db).SetAddAsync(key, values.ToArray(), flags); 791 } 792 #endregion 793 794 #region sismember 795 /// <summary> 796 /// whether member is a member of the set 797 /// </summary> 798 /// <param name="key">key of set</param> 799 /// <param name="value">value</param> 800 /// <param name="flags">behaviour</param> 801 /// <param name="db">index of database</param> 802 /// <returns></returns> 803 public bool SIsMember(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0) 804 { 805 return GetDatabase(db, true).SetContains(key, value, flags); 806 } 807 /// <summary> 808 /// whether member is a member of the set (Asynchronous) 809 /// </summary> 810 /// <param name="key">key of set</param> 811 /// <param name="value">value</param> 812 /// <param name="flags">behaviour</param> 813 /// <param name="db">index of database</param> 814 /// <returns></returns> 815 public Task<bool> SIsMemberAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0) 816 { 817 return GetDatabase(db, true).SetContainsAsync(key, value, flags); 818 } 819 #endregion 820 821 #region smembers 822 /// <summary> 823 /// get all the members of the set 824 /// </summary> 825 /// <param name="key">key of set</param> 826 /// <param name="flags">behaviour</param> 827 /// <param name="db">index of database</param> 828 /// <returns></returns> 829 public IList<RedisValue> SMembers(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 830 { 831 return GetDatabase(db, true).SetMembers(key, flags).ToList(); 832 } 833 /// <summary> 834 /// get all the members of the set(Asynchronous) 835 /// </summary> 836 /// <param name="key">key of set</param> 837 /// <param name="flags">behaviour</param> 838 /// <param name="db">index of database</param> 839 /// <returns></returns> 840 public Task<RedisValue[]> SMembersAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 841 { 842 return GetDatabase(db, true).SetMembersAsync(key, flags); 843 } 844 #endregion 845 846 #region spop 847 /// <summary> 848 /// Removes a random elements from the set 849 /// </summary> 850 /// <param name="key">key of set</param> 851 /// <param name="flags">behaviour</param> 852 /// <param name="db">index of database</param> 853 /// <returns></returns> 854 public RedisValue SPop(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 855 { 856 return GetDatabase(db).SetPop(key, flags); 857 } 858 /// <summary> 859 /// Removes a random elements from the set(Asynchronous) 860 /// </summary> 861 /// <param name="key">key of set</param> 862 /// <param name="flags">behaviour</param> 863 /// <param name="db">index of database</param> 864 /// <returns></returns> 865 public Task<RedisValue> SPopAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 866 { 867 return GetDatabase(db).SetPopAsync(key, flags); 868 } 869 #endregion 870 871 #region srem 872 /// <summary> 873 /// Remove the specified members from the set 874 /// </summary> 875 /// <param name="key">key of the set</param> 876 /// <param name="value">member to remove</param> 877 /// <param name="flags">behaviour</param> 878 /// <param name="db">index of database</param> 879 /// <returns></returns> 880 public bool SRem(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0) 881 { 882 return GetDatabase(db).SetRemove(key, value, flags); 883 } 884 /// <summary> 885 /// Remove the specified members from the set(Asynchronous) 886 /// </summary> 887 /// <param name="key">key of the set</param> 888 /// <param name="value">member to remove</param> 889 /// <param name="flags">behaviour</param> 890 /// <param name="db">index of database</param> 891 /// <returns></returns> 892 public Task<bool> SRemAsync(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None, int db = 0) 893 { 894 return GetDatabase(db).SetRemoveAsync(key, value, flags); 895 } 896 /// <summary> 897 /// Remove the specified members from the set 898 /// </summary> 899 /// <param name="key">key of the set</param> 900 /// <param name="value">member to remove</param> 901 /// <param name="flags">behaviour</param> 902 /// <param name="db">index of database</param> 903 /// <returns></returns> 904 public long SRem(RedisKey key, IList<RedisValue> values, CommandFlags flags = CommandFlags.None, int db = 0) 905 { 906 return GetDatabase(db).SetRemove(key, values.ToArray(), flags); 907 } 908 /// <summary> 909 /// Remove the specified members from the set(Asynchronous) 910 /// </summary> 911 /// <param name="key">key of the set</param> 912 /// <param name="value">member to remove</param> 913 /// <param name="flags">behaviour</param> 914 /// <param name="db">index of database</param> 915 /// <returns></returns> 916 public Task<long> SRemAsync(RedisKey key, IList<RedisValue> values, CommandFlags flags = CommandFlags.None, int db = 0) 917 { 918 return GetDatabase(db).SetRemoveAsync(key, values.ToArray(), flags); 919 } 920 #endregion 921 922 #region scard 923 /// <summary> 924 /// get the number of elements in the set 925 /// </summary> 926 /// <param name="key">key of set</param> 927 /// <param name="flags">behaviour</param> 928 /// <param name="db">index of database</param> 929 /// <returns></returns> 930 public long SCard(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 931 { 932 return GetDatabase(db, true).SetLength(key, flags); 933 } 934 /// <summary> 935 /// get the number of elements in the set(Asynchronous) 936 /// </summary> 937 /// <param name="key">key of set</param> 938 /// <param name="flags">behaviour</param> 939 /// <param name="db">index of database</param> 940 /// <returns></returns> 941 public Task<long> SCardAsync(RedisKey key, CommandFlags flags = CommandFlags.None, int db = 0) 942 { 943 return GetDatabase(db, true).SetLengthAsync(key, flags); 944 } 945 #endregion 946 947 #region sinter 948 /// <summary> 949 /// get the members of the set resulting from the intersection of all the given sets. 950 /// </summary> 951 /// <param name="keys">keys</param> 952 /// <param name="flags">behaviour</param> 953 /// <param name="db">index of database</param> 954 /// <returns></returns> 955 public IList<RedisValue> SInter(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0) 956 { 957 return GetDatabase(db, true).SetCombine(SetOperation.Intersect, keys.ToArray(), flags).ToList(); 958 } 959 /// <summary> 960 /// get the members of the set resulting from the intersection of all the given sets.(Asynchronous) 961 /// </summary> 962 /// <param name="keys">keys</param> 963 /// <param name="flags">behaviour</param> 964 /// <param name="db">index of database</param> 965 /// <returns></returns> 966 public Task<RedisValue[]> SInterAsync(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0) 967 { 968 return GetDatabase(db, true).SetCombineAsync(SetOperation.Intersect, keys.ToArray(), flags); 969 } 970 #endregion 971 972 #region sdiff 973 /// <summary> 974 /// get the members of the set resulting from the difference between the first set and all the successive sets. 975 /// </summary> 976 /// <param name="keys">keys</param> 977 /// <param name="flags">behaviour</param> 978 /// <param name="db">index of database</param> 979 /// <returns></returns> 980 public IList<RedisValue> SDiff(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0) 981 { 982 return GetDatabase(db, true).SetCombine(SetOperation.Difference, keys.ToArray(), flags).ToList(); 983 } 984 /// <summary> 985 /// get the members of the set resulting from the difference between the first set and all the successive sets.(Asynchronous) 986 /// </summary> 987 /// <param name="keys">keys</param> 988 /// <param name="flags">behaviour</param> 989 /// <param name="db">index of database</param> 990 /// <returns></returns> 991 public Task<RedisValue[]> SDiffAsync(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0) 992 { 993 return GetDatabase(db, true).SetCombineAsync(SetOperation.Difference, keys.ToArray(), flags); 994 } 995 #endregion 996 997 #region sunion 998 /// <summary> 999 /// get the members of the set resulting from the union of all the given sets. 1000 /// </summary> 1001 /// <param name="keys">keys</param> 1002 /// <param name="flags">behaviour</param> 1003 /// <param name="db">index of database</param> 1004 /// <returns></returns> 1005 public IList<RedisValue> SUnion(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0) 1006 { 1007 return GetDatabase(db, true).SetCombine(SetOperation.Union, keys.ToArray(), flags).ToList(); 1008 } 1009 /// <summary> 1010 /// get the members of the set resulting from the union of all the given sets.(Asynchronous) 1011 /// </summary> 1012 /// <param name="keys">keys</param> 1013 /// <param name="flags">behaviour</param> 1014 /// <param name="db">index of database</param> 1015 /// <returns></returns> 1016 public Task<RedisValue[]> SUnionAsync(IList<RedisKey> keys, CommandFlags flags = CommandFlags.None, int db = 0) 1017 { 1018 return GetDatabase(db, true).SetCombineAsync(SetOperation.Union, keys.ToArray(), flags); 1019 } 1020 #endregion 1021 #endregion 1022 1023 #region Sorted Set 1024 #region zadd 1025 /// <summary> 1026 /// Adds a member with the score to the sorted set stored at key 1027 /// </summary> 1028 /// <param name="key">key</param> 1029 /// <param name="member">member</param> 1030 /// <param name="score">score</param> 1031 /// <param name="flags">behaviour</param> 1032 /// <param name="db">index of database</param> 1033 /// <returns></returns> 1034 public bool ZAdd(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None, int db = 0) 1035 { 1036 return GetDatabase(db).SortedSetAdd(key, member, score, flags); 1037 } 1038 /// <summary> 1039 /// Adds a member with the score to the sorted set stored at key(Asynchronous) 1040 /// </summary> 1041 /// <param name="key">key</param> 1042 /// <param name="member">member</param> 1043 /// <param name="score">score</param> 1044 /// <param name="flags">behaviour</param> 1045 /// <param name="db">index of database</param> 1046 /// <returns></returns> 1047 public Task<bool> ZAddAsync(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None, int db = 0) 1048 { 1049 return GetDatabase(db).SortedSetAddAsync(key, member, score, flags); 1050 } 1051 /// <summary> 1052 /// Adds members with scores to the sorted set stored at key 1053 /// </summary> 1054 /// <param name="key">key</param> 1055 /// <param name="values">sortedset entity</param> 1056 /// <param name="flags">behaviour</param> 1057 /// <param name="db">index of database</param> 1058 /// <returns></returns> 1059 public long ZAdd(RedisKey key, IList<SortedSetEntry> values, CommandFlags flags = CommandFlags.None, int db = 0) 1060 { 1061 return GetDatabase(db).SortedSetAdd(key, values.ToArray(), flags); 1062 } 1063 /// <summary> 1064 /// Adds members with scores to the sorted set stored at key(Asynchronous) 1065 /// </summary> 1066 /// <param name="key">key</param> 1067 /// <param name="values">sortedset entity</param> 1068 /// <param name="flags">behaviour</param> 1069 /// <param name="db">index of database</param> 1070 /// <returns></returns> 1071 public Task<long> ZAddAsync(RedisKey key, IList<SortedSetEntry> values, CommandFlags flags = CommandFlags.None, int db = 0) 1072 { 1073 return GetDatabase(db).SortedSetAddAsync(key, values.ToArray(), flags); 1074 } 1075 #endregion 1076 1077 #region zrem 1078 /// <summary> 1079 /// Removes a member from the sorted set stored at key 1080 /// </summary> 1081 /// <param name="key">key</param> 1082 /// <param name="member">member</param> 1083 /// <param name="flags">behaviour</param> 1084 /// <param name="db">index of database</param> 1085 /// <returns></returns> 1086 public bool ZRem(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None, int db = 0) 1087 { 1088 return GetDatabase(db).SortedSetRemove(key, member, flags); 1089 } 1090 /// <summary> 1091 /// Removes a member from the sorted set stored at key(Async) 1092 /// </summary> 1093 /// <param name="key">key</param> 1094 /// <param name="member">member</param> 1095 /// <param name="flags">behaviour</param> 1096 /// <param name="db">index of database</param> 1097 /// <returns></returns> 1098 public Task<bool> ZRemAsync(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None, int db = 0) 1099 { 1100 return GetDatabase(db).SortedSetRemoveAsync(key, member, flags); 1101 } 1102 /// <summary> 1103 /// Removes members from the sorted set stored at key 1104 /// </summary> 1105 /// <param name="key">key</param> 1106 /// <param name="members">members</param> 1107 /// <param name="flags">behaviour</param> 1108 /// <param name="db">index of database</param> 1109 /// <returns></returns> 1110 public long ZRem(RedisKey key, IList<RedisValue> members, CommandFlags flags = CommandFlags.None, int db = 0) 1111 { 1112 return GetDatabase(db).SortedSetRemove(key, members.ToArray(), flags); 1113 } 1114 /// <summary> 1115 /// Removes members from the sorted set stored at key(Asynchronous) 1116 /// </summary> 1117 /// <param name="key">key</param> 1118 /// <param name="members">members</param> 1119 /// <param name="flags">behaviour</param> 1120 /// <param name="db">index of database</param> 1121 /// <returns></returns> 1122 public Task<long> ZRemAsync(RedisKey key, IList<RedisValue> members, CommandFlags flags = CommandFlags.None, int db = 0) 1123 { 1124 return GetDatabase(db).SortedSetRemoveAsync(key, members.ToArray(), flags); 1125 } 1126 #endregion 1127 1128 #region zrange 1129 public IList<RedisValue> ZRange(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0) 1130 { 1131 return GetDatabase(db, true).SortedSetRangeByRank(key, start, stop, Order.Ascending, flags).ToList(); 1132 } 1133 public Task<RedisValue[]> ZRangeAsync(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0) 1134 { 1135 return GetDatabase(db, true).SortedSetRangeByRankAsync(key, start, stop, Order.Ascending, flags); 1136 } 1137 #endregion 1138 1139 #region zrevrange 1140 public IList<RedisValue> ZRevRange(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0) 1141 { 1142 return GetDatabase(db, true).SortedSetRangeByRank(key, start, stop, Order.Descending, flags).ToList(); 1143 } 1144 public Task<RedisValue[]> ZRevRangeAsync(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None, int db = 0) 1145 { 1146 return GetDatabase(db, true).SortedSetRangeByRankAsync(key, start, stop, Order.Descending, flags); 1147 } 1148 #endregion 1149 1150 #region zincrby 1151 public double ZIncrby(RedisKey key, RedisValue member, double value = 1, CommandFlags flags = CommandFlags.None, int db = 0) 1152 { 1153 return GetDatabase(db).SortedSetIncrement(key, member, value, flags); 1154 } 1155 public Task<double> ZIncrbyAsync(RedisKey key, RedisValue member, double value = 1, CommandFlags flags = CommandFlags.None, int db = 0) 1156 { 1157 return GetDatabase(db).SortedSetIncrementAsync(key, member, value, flags); 1158 } 1159 #endregion 1160 #endregion 1161 1162 #region pub/sub 1163 public long Publish(RedisChannel channel, RedisValue value, CommandFlags flags = CommandFlags.None, bool isRead = false) 1164 { 1165 return GetSubscriber(isRead).Publish(channel, value, flags); 1166 } 1167 public void Subscribe(RedisChannel channel, Action<RedisChannel, RedisValue> handle, CommandFlags flags = CommandFlags.None, bool isRead = false) 1168 { 1169 GetSubscriber(isRead).Subscribe(channel, handle, flags); 1170 } 1171 public void UnSubscrribe(RedisChannel channel, Action<RedisChannel, RedisValue> handle, CommandFlags flags = CommandFlags.None, bool isRead = false) 1172 { 1173 GetSubscriber(isRead).Unsubscribe(channel, handle, flags); 1174 } 1175 #endregion 1176 } 1177 }
So much for this series. The next series will be some complex example. Thanks for your reading.
如果您认为这篇文章还不错或者有所收获,可以点击右下角的【推荐】按钮,因为你的支持是我继续写作,分享的最大动力!
声明:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果您发现博客中出现了错误,或者有更好的建议、想法,请及时与我联系!!如果想找我私下交流,可以私信或者加我微信。