(三)Redis for StackExchange.Redis
目录
(三)Redis for StackExchange.Redis
StackExchange.Redis是.NET目前比较流行的操作Redis的框架,园子里有很多介绍的文章,这里仅仅是做一些笔记
声明配置项
private static ConfigurationOptions configurationOptions = ConfigurationOptions.Parse("IP地址,password=密码,connectTimeout=6000");
其中
IP地址需要包含端口号,如1.1.1.1:6379
密码是Redis的登录密码
封装Redis(单例)
public class Redis
{
private static ConfigurationOptions configurationOptions = ConfigurationOptions.Parse("IP地址,password=密码,connectTimeout=过期时间(秒)");
private static readonly object locker = new object();
private static ConnectionMultiplexer conn;
/// <summary>
/// 获取实例(单例)
/// </summary>
/// <returns></returns>
public static ConnectionMultiplexer GetInstance()
{
if (conn == null)
{
lock (locker)
{
if (conn == null || !conn.IsConnected)
{
conn = ConnectionMultiplexer.Connect(configurationOptions);
}
}
}
return conn;
}
public void Test()
{
var r = Redis.GetInstance();
var db = r.GetDatabase();
db.StringGet("Name");
}
}
安装与用法
到这里,就可以在本地通过代码以及redis-cli来调试和操作redis了
...