.net ServiceStack.Redis 的使用
1.以连接池方式
PooledRedisClientManager pool = new PooledRedisClientManager(RedisConfig.ShareConfig.WriteServers
, RedisConfig.ShareConfig.ReadServers, new RedisClientManagerConfig()
{
MaxReadPoolSize = RedisConfig.ShareConfig.MaxReadPoolSize, //读节点个数
MaxWritePoolSize = RedisConfig.ShareConfig.MaxWritePoolSize, //写节点个数
AutoStart = RedisConfig.ShareConfig.AutoStart, //自动重启
});
using Xinpa.Config;
using System.Collections.Generic;
namespace Xinpa.Redis
{
/// <summary>
/// RedisConfig,添加配置文件RedisConfig.xml
/// </summary>
[Config( @"..\..\..\Conf\RedisConfig.xml","utf-8", ConfigPathModes.RELATIVE)]
public class RedisConfig : XMLConfigObject
{
/// <summary>
/// 写服务器配置
/// </summary>
public List<string> WriteServers { get; set; }
/// <summary>
/// 读服务器配置
/// </summary>
public List<string> ReadServers { get; set; }
/// <summary>
/// 最大读链接数
/// </summary>
public int MaxReadPoolSize { get; set; }
/// <summary>
/// 最大写链接数
/// </summary>
public int MaxWritePoolSize { get; set; }
/// <summary>
/// 自动重启
/// </summary>
public bool AutoStart { get; set; }
/// <summary>
/// RedisConfig
/// </summary>
private RedisConfig()
{
}
private class InternalSingleProvider
{
static InternalSingleProvider()
{
}
internal static readonly RedisConfig instance = new RedisConfig();
}
public static RedisConfig ShareConfig
{
get
{
return InternalSingleProvider.instance;
}
}
}
}
2.直接使用RedisClient
//使用第一个写节点配置
if (RedisConfig.ShareConfig.WriteServers.Count < 1)
{
throw new Exception("Redis服务器节点未配置[WriteServers]");
}
try
{
string[] confs = RedisConfig.ShareConfig.WriteServers[0].Split("@");
string[] servs = confs[1].Split(":");
instance = new RedisClient(servs[0], servs[1].IntValue(), confs[0]);
}
catch (Exception ex)
{
throw new Exception("Redis服务器节点配置不正确[WriteServers]", ex);
}
3.Redis的读写参考 IRedisClient的常用操作