Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. namespace StackExchange.Redis
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7.  
  8. public static class StackExchangeRedisExtensions
  9. {
  10. public static IEnumerable<IServer> GetMasterServers(this IConnectionMultiplexer connection)
  11. {
  12. var endpoints = GetFilteredEndpoints(connection);
  13.  
  14. var allServers = endpoints.Select(ep => connection.GetServer(ep));
  15.  
  16. var masters = allServers.Where(s => !s.IsSlave).OrderBy(s => s.EndPoint.GetPort().Value);
  17.  
  18. return masters;
  19. }
  20.  
  21. public static IEnumerable<EndPoint> GetFilteredEndpoints(this IConnectionMultiplexer connection)
  22. {
  23. var endpoints = connection.GetEndPoints();
  24.  
  25. if (endpoints.Count() > 1)
  26. {
  27. var instanceEndpoints = endpoints.Where(ep =>
  28. {
  29. var port = ep.GetPort();
  30. return port.HasValue && port != 6379 && port != 6380;
  31. });
  32.  
  33. if (instanceEndpoints.Any())
  34. return instanceEndpoints;
  35. }
  36.  
  37. return endpoints;
  38. }
  39.  
  40. public static int? GetPort(this EndPoint ep)
  41. {
  42. int? port = null;
  43.  
  44. if (ep is DnsEndPoint)
  45. port = ((DnsEndPoint)ep).Port;
  46. else if (ep is IPEndPoint)
  47. port = ((IPEndPoint)ep).Port;
  48.  
  49. return port;
  50. }
  51.  
  52. //AZURE REDIS SPECIFIC IMPLEMENTATION
  53. public static int GetShardIdForKey(this IConnectionMultiplexer connection, string key)
  54. {
  55. var slot = connection.HashSlot(key);
  56. var masters = connection.GetMasterServers();
  57. var x = masters.First().ClusterNodes().GetBySlot(slot);
  58. return GetShardIdFromPort(x.EndPoint.GetPort().Value);
  59. }
  60.  
  61. //AZURE REDIS SPECIFIC IMPLEMENTATION
  62. public static int GetShardIdFromPort(int port)
  63. {
  64. return (port % 100) / 2;
  65. }
  66.  
  67. public static async void FlushAllNodes(this ConnectionMultiplexer connection)
  68. {
  69. foreach (var server in connection.GetMasterServers())
  70. {
  71. await server.FlushAllDatabasesAsync();
  72. }
  73. }
  74.  
  75. public static string Info(this IServer server, string sectionName, string item)
  76. {
  77. return server.Info().SubSection(sectionName).SubItem(item);
  78. }
  79.  
  80. public static IGrouping<string, KeyValuePair<string, string>> SubSection(this IGrouping<string, KeyValuePair<string, string>>[] info, string sectionName)
  81. {
  82. var section = info.Where(g => g.Key == sectionName).FirstOrDefault();
  83. return section;
  84. }
  85.  
  86. public static string SubItem(this IGrouping<string, KeyValuePair<string, string>> section, string item)
  87. {
  88. if (section == null)
  89. return null;
  90.  
  91. var listEntry = section.Where(g => g.Key == item).FirstOrDefault();
  92.  
  93. return listEntry.Value;
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement