RedisHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using StackExchange.Redis;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SMP.Common.WebRegion
  9. {
  10. public static class RedisUnit
  11. {
  12. public static async Task<long> DeleteScanKey(this IDatabase db, IBatch batch, string pattern)
  13. {
  14. var redisResult = await db.ScriptEvaluateAsync(LuaScript.Prepare(
  15. //Redis的keys模糊查询:
  16. " local res = redis.call(\'KEYS\', @keypattern) " +
  17. " return res "), new { @keypattern = pattern });
  18. if (!redisResult.IsNull)
  19. {
  20. if (batch != null)
  21. batch.KeyDeleteAsync((RedisKey[])redisResult);
  22. else
  23. return db.KeyDelete((RedisKey[])redisResult);
  24. return 0;
  25. }
  26. else
  27. {
  28. return 0;
  29. }
  30. }
  31. }
  32. public class RedisHelper : IDisposable
  33. {
  34. //连接字符串
  35. private string _connectionString;
  36. //实例名称
  37. private string _instanceName;
  38. //密码
  39. private string _password;
  40. //默认数据库
  41. private int _defaultDB;
  42. private ConcurrentDictionary<string, ConnectionMultiplexer> _connections;
  43. public RedisHelper(string connectionString, string instanceName, string password, int defaultDB = 0)
  44. {
  45. _connectionString = connectionString;
  46. _instanceName = instanceName;
  47. _password = password;
  48. _defaultDB = defaultDB;
  49. _connections = new ConcurrentDictionary<string, ConnectionMultiplexer>();
  50. }
  51. /// <summary>
  52. /// 获取ConnectionMultiplexer
  53. /// </summary>
  54. /// <returns></returns>
  55. private ConnectionMultiplexer GetConnect()
  56. {
  57. return _connections.GetOrAdd(_instanceName, p =>
  58. ConnectionMultiplexer.Connect(new ConfigurationOptions
  59. {
  60. AbortOnConnectFail = false,
  61. Password = _password,
  62. EndPoints = {
  63. _connectionString
  64. }
  65. }));
  66. }
  67. /// <summary>
  68. /// 获取数据库
  69. /// </summary>
  70. /// <param name="configName"></param>
  71. /// <param name="db">默认为0:优先代码的db配置,其次config中的配置</param>
  72. /// <returns></returns>
  73. public IDatabase GetDatabase()
  74. {
  75. return GetConnect().GetDatabase(_defaultDB);
  76. }
  77. public IServer GetServer(string configName = null, int endPointsIndex = 0)
  78. {
  79. if (string.IsNullOrEmpty(configName))
  80. configName = _instanceName;
  81. var endpoint = _connections[configName].GetEndPoints()[0];
  82. return GetConnect().GetServer(endpoint);
  83. }
  84. public ISubscriber GetSubscriber(string configName = null)
  85. {
  86. return GetConnect().GetSubscriber();
  87. }
  88. public void Dispose()
  89. {
  90. if (_connections != null && _connections.Count > 0)
  91. {
  92. foreach (var item in _connections.Values)
  93. {
  94. item.Close();
  95. }
  96. }
  97. }
  98. }
  99. }