RedisHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 XYY.Core.Standard.Data.Redis
  9. {
  10. public static class RedisUnit
  11. {
  12. public static async Task<long> DeleteScanKey(this IDatabase db, 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. return db.KeyDelete((RedisKey[])redisResult);
  21. }
  22. else
  23. {
  24. return 0;
  25. }
  26. }
  27. public static Task<long> DeleteScanKey(this IDatabase db, IBatch batch, string pattern)
  28. {
  29. var redisResult = db.ScriptEvaluate(LuaScript.Prepare(
  30. //Redis的keys模糊查询:
  31. " local res = redis.call(\'KEYS\', @keypattern) " +
  32. " return res "), new { @keypattern = pattern });
  33. if (!redisResult.IsNull)
  34. {
  35. return batch.KeyDeleteAsync((RedisKey[])redisResult);
  36. }
  37. else
  38. {
  39. return null;
  40. }
  41. }
  42. }
  43. public class RedisHelper : IDisposable
  44. {
  45. //连接字符串
  46. private string _connectionString;
  47. //实例名称
  48. private string _instanceName;
  49. //密码
  50. private string _password;
  51. //默认数据库
  52. private int _defaultDB;
  53. private ConcurrentDictionary<string, ConnectionMultiplexer> _connections;
  54. public RedisHelper(string connectionString, string instanceName, string password, int defaultDB = 0)
  55. {
  56. _connectionString = connectionString;
  57. _instanceName = instanceName;
  58. _password = password;
  59. _defaultDB = defaultDB;
  60. _connections = new ConcurrentDictionary<string, ConnectionMultiplexer>();
  61. }
  62. /// <summary>
  63. /// 获取ConnectionMultiplexer
  64. /// </summary>
  65. /// <returns></returns>
  66. private ConnectionMultiplexer GetConnect()
  67. {
  68. Console.WriteLine(_connectionString + ",password=" + _password);
  69. return _connections.GetOrAdd(_instanceName, p => ConnectionMultiplexer.Connect(_connectionString + ",password=" + _password));
  70. }
  71. /// <summary>
  72. /// 获取数据库
  73. /// </summary>
  74. /// <param name="configName"></param>
  75. /// <param name="db">默认为0:优先代码的db配置,其次config中的配置</param>
  76. /// <returns></returns>
  77. public IDatabase GetDatabase()
  78. {
  79. return GetConnect().GetDatabase(_defaultDB);
  80. }
  81. public IDatabase GetDatabase(int dbIndex)
  82. {
  83. return GetConnect().GetDatabase(dbIndex);
  84. }
  85. public IServer GetServer(string configName = null, int endPointsIndex = 0)
  86. {
  87. if (string.IsNullOrEmpty(configName))
  88. configName = _instanceName;
  89. var endpoint = _connections[configName].GetEndPoints()[0];
  90. return GetConnect().GetServer(endpoint);
  91. }
  92. public ISubscriber GetSubscriber(string configName = null)
  93. {
  94. return GetConnect().GetSubscriber();
  95. }
  96. public void Dispose()
  97. {
  98. if (_connections != null && _connections.Count > 0)
  99. {
  100. foreach (var item in _connections.Values)
  101. {
  102. item.Close();
  103. }
  104. }
  105. }
  106. }
  107. }