using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
namespace Senparc.Weixin.Cache.Redis
{
public class RedisManager
{
///
/// redis配置文件信息
///
private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();
private static PooledRedisClientManager prcm;
///
/// 静态构造方法,初始化链接池管理对象
///
static RedisManager()
{
CreateManager();
}
///
/// 创建链接池管理对象
///
private static void CreateManager()
{
string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");
prcm = new PooledRedisClientManager(readServerList, writeServerList,
new RedisClientManagerConfig
{
MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
AutoStart = redisConfigInfo.AutoStart,
});
}
private static string[] SplitString(string strSource, string split)
{
return strSource.Split(split.ToArray());
}
///
/// 客户端缓存操作对象
///
public static IRedisClient GetClient()
{
if (prcm == null)
CreateManager();
return prcm.GetClient();
}
public static IRedisTypedClient GetTypedClient()
{
if (prcm == null)
CreateManager();
return prcm.GetClient().As();
}
}
}