VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之StackExchange.Redis 二次封装(3)

475 476 /// <summary> 477 /// 获取指定Key中最大Score值 478 /// </summary> 479 /// <param name="dbIndex">数据库</param> 480 /// <param name="key"></param> 481 /// <returns></returns> 482 public static double SortedSetMaxScore(int dbIndex, string key) 483 { 484 var db = Instance.GetDatabase(dbIndex); 485 double dValue = 0; 486 var rValue = db.SortedSetRangeByRankWithScores(key, 0, 0, Order.Descending).FirstOrDefault(); 487 dValue = rValue != null ? rValue.Score : 0; 488 return dValue; 489 } 490 491 /// <summary> 492 /// 删除Key中指定的值 493 /// </summary> 494 /// <param name="dbIndex">数据库</param> 495 /// <param name="key"></param> 496 /// <param name="value"></param> 497 public static long SortedSetRemove<T>(int dbIndex, string key, params T[] value) 498 { 499 var db = Instance.GetDatabase(dbIndex); 500 var rValue = ConvertRedisValue<T>(value); 501 return db.SortedSetRemove(key, rValue); 502 } 503 504 /// <summary> 505 /// 删除指定起始值到结束值的数据 506 /// </summary> 507 /// <typeparam name="T"></typeparam> 508 /// <param name="dbIndex">数据库</param> 509 /// <param name="key"></param> 510 /// <param name="startValue">起始值</param> 511 /// <param name="endValue">结束值</param> 512 /// <returns></returns> 513 public static long SortedSetRemoveRangeByValue<T>(int dbIndex, string key, T startValue, T endValue) 514 { 515 var db = Instance.GetDatabase(dbIndex); 516 var sValue = ConvertJson<T>(startValue); 517 var eValue = ConvertJson<T>(endValue); 518 return db.SortedSetRemoveRangeByValue(key, sValue, eValue); 519 } 520 521 /// <summary> 522 /// 删除 从 start 开始的 stop 条数据 523 /// </summary> 524 /// <param name="dbIndex">数据库</param> 525 /// <param name="key"></param> 526 /// <param name="start"></param> 527 /// <param name="stop"></param> 528 /// <returns></returns> 529 public static long SortedSetRemoveRangeByRank(int dbIndex, string key, long start, long stop) 530 { 531 var db = Instance.GetDatabase(dbIndex); 532 return db.SortedSetRemoveRangeByRank(key, start, stop); 533 } 534 535 /// <summary> 536 /// 根据排序分数Score,删除从 start 开始的 stop 条数据 537 /// </summary> 538 /// <param name="dbIndex">数据库</param> 539 /// <param name="key"></param> 540 /// <param name="start"></param> 541 /// <param name="stop"></param> 542 /// <returns></returns> 543 public static long SortedSetRemoveRangeByScore(int dbIndex, string key, double start, double stop) 544 { 545 var db = Instance.GetDatabase(dbIndex); 546 return db.SortedSetRemoveRangeByScore(key, start, stop); 547 } 548 549 /// <summary> 550 /// 获取从 start 开始的 stop 条数据 551 /// </summary> 552 /// <typeparam name="T"></typeparam> 553 /// <param name="dbIndex">数据库</param> 554 /// <param name="key"></param> 555 /// <param name="start">起始数</param> 556 /// <param name="stop">-1表示到结束,0为1条</param> 557 /// <param name="desc">是否按降序排列</param> 558 /// <returns></returns> 559 public static List<T> SortedSetRangeByRank<T>(int dbIndex, string key, long start = 0, long stop = -1, bool desc = false) 560 { 561 var db = Instance.GetDatabase(dbIndex); 562 Order orderBy = desc ? Order.Descending : Order.Ascending; 563 var rValue = db.SortedSetRangeByRank(key, start, stop, orderBy); 564 return ConvetList<T>(rValue); 565 } 566 #endregion 567 568 #region 异步方法 569 570 /// <summary> 571 /// 添加一个值到Key 572 /// </summary> 573 /// <typeparam name="T"></typeparam> 574 /// <param name="dbIndex">数据库</param> 575 /// <param name="key"></param> 576 /// <param name="value"></param> 577 /// <param name="score">排序分数,为空将获取集合中最大score加1</param> 578 /// <returns></returns> 579 public static async Task<bool> SortedSetAddAsync<T>(int dbIndex, string key, T value, double? score = null) 580 { 581 var db = Instance.GetDatabase(dbIndex); 582 double scoreNum = score ?? _GetScore(key, db); 583 return await db.SortedSetAddAsync(key, ConvertJson<T>(value), scoreNum); 584 } 585 586 /// <summary> 587 /// 添加一个集合到Key 588 /// </summary> 589 /// <typeparam name="T"></typeparam> 590 /// <param name="dbIndex">数据库</param> 591 /// <param name="key"></param> 592 /// <param name="value"></param> 593 /// <param name="score">排序分数,为空将获取集合中最大score加1</param> 594 /// <returns></returns> 595 public static async Task<long> SortedSetAddAsync<T>(int dbIndex, string key, List<T> value, double? score = null) 596 { 597 var db = Instance.GetDatabase(dbIndex); 598 double scoreNum = score ?? _GetScore(key, db); 599 SortedSetEntry[] rValue = value.Select(o => new SortedSetEntry(ConvertJson<T>(o), scoreNum++)).ToArray(); 600 return await db.SortedSetAddAsync(key, rValue); 601 } 602 603 /// <summary> 604 /// 获取集合中的数量 605 /// </summary> 606 /// <param name="dbIndex">数据库</param> 607 /// <param name="key"></param> 608 /// <returns></returns> 609 public static async Task<long> SortedSetLengthAsync(int dbIndex, string key) 610 { 611 var db = Instance.GetDatabase(dbIndex); 612 return await db.SortedSetLengthAsync(key); 613 } 614 615 /// <summary> 616 /// 获取指定起始值到结束值的集合数量 617 /// </summary> 618 /// <typeparam name="T"></typeparam> 619 /// <param name="dbIndex">数据库</param> 620 /// <param name="key"></param> 621 /// <param name="startValue">起始值</param> 622 /// <param name="endValue">结束值</param> 623 /// <returns></returns> 624 public static async Task<long> SortedSetLengthByValueAsync<T>(int dbIndex, string key, T startValue, T endValue) 625 { 626 var db = Instance.GetDatabase(dbIndex); 627 var sValue = ConvertJson<T>(startValue); 628 var eValue = ConvertJson<T>(endValue); 629 return await db.SortedSetLengthByValueAsync(key, sValue, eValue); 630 } 631 632 /// <summary> 633 /// 获取指定Key中最大Score值 634 /// </summary> 635 /// <param name="dbIndex">数据库</param> 636 /// <param name="key"></param> 637 /// <returns></returns> 638 public static async Task<double> SortedSetMaxScoreAsync(int dbIndex, string key) 639 { 640 var db = Instance.GetDatabase(dbIndex); 641 double dValue = 0; 642 var rValue = (await db.SortedSetRangeByRankWithScoresAsync(key, 0, 0, Order.Descending)).FirstOrDefault(); 643 dValue = rValue != null ? rValue.Score : 0; 644 return dValue; 645 } 646 647 /// <summary> 648 /// 删除Key中指定的值 649 /// </summary> 650 /// <param name="dbIndex">数据库</param> 651 /// <param name="key"></param> 652 /// <param name="value"></param> 653 public static async Task<long> SortedSetRemoveAsync<T>(int dbIndex, string key, params T[] value) 654 { 655 var db = Instance.GetDatabase(dbIndex); 656 var rValue = ConvertRedisValue<T>(value); 657 return await db.SortedSetRemoveAsync(key, rValue); 658 } 659 660 /// <summary> 661 /// 删除指定起始值到结束值的数据 662 /// </summary> 663 /// <typeparam name="T"></typeparam> 664 /// <param name="dbIndex">数据库</param> 665 /// <param name="key"></param> 666 /// <param name="startValue">起始值</param> 667 /// <param name="endValue">结束值</param> 668 /// <returns></returns> 669 public static async Task<long> SortedSetRemoveRangeByValueAsync<T>(int dbIndex, string key, T startValue, T endValue) 670 { 671 var db = Instance.GetDatabase(dbIndex); 672 var sValue = ConvertJson<T>(startValue); 673 var eValue = ConvertJson<T>(endValue); 674 return await db.SortedSetRemoveRangeByValueAsync(key, sValue, eValue); 675 } 676 677 /// <summary> 678 /// 删除 从 start 开始的 stop 条数据 679 /// </summary> 680 /// <param name="dbIndex">数据库</param> 681 /// <param name="key"></param> 682 /// <param name="start"></param> 683 /// <param name="stop"></param> 684 /// <returns></returns> 685 public static async Task<long> SortedSetRemoveRangeByRankAsync(int dbIndex, string key, long start, long stop) 686 { 687 var db = Instance.GetDatabase(dbIndex); 688 return await db.SortedSetRemoveRangeByRankAsync(key, start, stop); 689 } 690 691 #endregion 692 693 #region 内部辅助方法 694 695 /// <summary> 696 /// 将对象转换成string字符串 697 /// </summary> 698 /// <typeparam name="T"></typeparam> 699 /// <param name="value"></param> 700 /// <returns></returns> 701 public static string ConvertJson<T>(T value) 702 { 703

相关教程