{
"redis_server_session": "127.0.0.1:6379",
"redis_max_read_pool": "3",
"redis_max_write_pool": "3"
}
class RedisHelper
{
private static readonly object Locker = new object();
private ConnectionMultiplexer Redis { get; set; }
private IDatabase DB { get; set; }
public RedisHelper(string connection)
{
if (Redis == null)
{
// 锁定某一代码块,让同一时间只有一个线程访问该代码块
lock (Locker)
{
if (Redis == null || !Redis.IsConnected)
{
Redis = ConnectionMultiplexer.Connect("127.0.0.1:6379,defaultDatabase=" + connection + ",password=password");
DB = Redis.GetDatabase();
}
}
}
}
public bool SetValue(string key, string value)
{
return DB.StringSet(key, value);
}
public string GetValue(string key)
{
return DB.StringGet(key);
}
public bool DeleteKey(string key)
{
return DB.KeyDelete(key);
}
}