RedisConnection.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using StackExchange.Redis;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace OASystem.RedisRepository.Config
  8. {
  9. public class RedisConnection
  10. {
  11. private static ConnectionMultiplexer _connection;
  12. private static readonly object SyncObject = new object();
  13. /// <summary>
  14. /// redis连接对象,单例加锁不影响性能
  15. /// </summary>
  16. public static ConnectionMultiplexer GenerateConnection
  17. {
  18. get
  19. {
  20. if (_connection == null || !_connection.IsConnected)
  21. {
  22. lock (SyncObject)
  23. {
  24. var configurationOptions = new ConfigurationOptions()
  25. {
  26. Password = RedisClientConfig.RedisAuth,
  27. EndPoints =
  28. {
  29. {RedisClientConfig.Server, RedisClientConfig.Port}
  30. //,{RedisClientConfig.SlaveServer, RedisClientConfig.SlavePort}
  31. }
  32. };
  33. _connection = ConnectionMultiplexer.Connect(configurationOptions);
  34. }
  35. }
  36. return _connection;
  37. }
  38. }
  39. }
  40. }