.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
{
///
/// RedisConfig,添加配置文件RedisConfig.xml
///
[Config( @"..\..\..\Conf\RedisConfig.xml","utf-8", ConfigPathModes.RELATIVE)]
public class RedisConfig : XMLConfigObject
{
///
/// 写服务器配置
///
public List
///
/// 读服务器配置
///
public List
///
/// 最大读链接数
///
public int MaxReadPoolSize { get; set; }
///
/// 最大写链接数
///
public int MaxWritePoolSize { get; set; }
///
/// 自动重启
///
public bool AutoStart { get; set; }
///
/// RedisConfig
///
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的常用操作