You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace Ems.CollectService.Redis
|
|
{
|
|
public class RedisHelper
|
|
{
|
|
private static readonly ConfigurationOptions ConfigurationOptions = ConfigurationOptions.Parse("127.0.0.1:6379,password=admin123");
|
|
private static readonly object Locker = new object();
|
|
private static ConnectionMultiplexer _redisConn;
|
|
|
|
/// <summary>
|
|
/// 单例获取
|
|
/// </summary>
|
|
public static ConnectionMultiplexer RedisConn
|
|
{
|
|
get
|
|
{
|
|
if (_redisConn == null)
|
|
{
|
|
// 锁定某一代码块,让同一时间只有一个线程访问该代码块
|
|
lock (Locker)
|
|
{
|
|
if (_redisConn == null || !_redisConn.IsConnected)
|
|
{
|
|
_redisConn = ConnectionMultiplexer.Connect(ConfigurationOptions);
|
|
}
|
|
}
|
|
}
|
|
return _redisConn;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|