redis服务端的安装注意事项
1.redis最好使用linux操作系统
2.服务器需要关闭防火墙,不然需要用到SSH验证连接,参考:http://www.4byte.cn/question/290668/using-redis-over-secured-connection.html(未验证,仅供参考)
3.一些常用指令
netstat -natp | grep :6379 |wc –l 查看redis连接数
4.redis设置连接
keepalive设置60秒
ServiceStack.Redis本地测试ok,发布到服务器,服务一开启就挂掉,后来发现与net framework有关,换win2008服务器不报错。
PooledRedisClientManager设置密码auth
http://www.cnblogs.com/jiagoushi/p/4024530.html
连接池缓存
http://blog.csdn.net/qiujialongjjj/article/details/17221777
http://blog.csdn.net/cmalaya/article/details/7699807
http://www.360doc.com/content/14/1027/15/1039473_420327933.shtml
_ueditor_page_break_tag_
Redis连接方法:
RedisClient redisClient = new RedisClient("58.XX.X.XX", 6379,"XXXXX");
或新建一个RedisManage类
public class RedisManage
{
//private static string WriteServerList = ConfigHelper.GetConfigString("WriteServerList");
//private static string ReadServerList = ConfigHelper.GetConfigString("ReadServerList");
//private static int MaxWritePoolSize = Helper.Helper.GetInt(ConfigHelper.GetConfigString("MaxWritePoolSize"),0);
//private static int MaxReadPoolSize = Helper.Helper.GetInt(ConfigHelper.GetConfigString("MaxReadPoolSize"),0);
//private static bool AutoStart = ConfigHelper.GetConfigString("AutoStart").Contains("true");
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static PooledRedisClientManager prcm = RedisManage.CreateRedisManager(
new string[] { "XXXX@XX.XXX.XX.XX:6379" }, //读写服务器
new string[] { "XXXX@XX.XXX.XX.XX:6379" } //只读服务器
);
/// <summary>
/// 创建Redis连接池管理对象
/// </summary>
public static PooledRedisClientManager CreateRedisManager(string[] readWriteHosts, string[] readOnlyHosts)
{
//支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 20,
//“写”链接池数
MaxReadPoolSize = 20,
//“读”链接池数
AutoStart = true
});
}
/// <summary>
/// 添加数据
/// </summary>
public static bool Set<T>(string key, T val)
{
using (IRedisClient rds = prcm.GetClient())
{
return rds.Set<T>(key, val);
}
}
/// <summary>
/// 读取数据
/// </summary>
public static T Get<T>(string key)
{
using (IRedisClient rds = prcm.GetReadOnlyClient())
{
return rds.Get<T>(key);
}
}
/// <summary>
/// 删除数据
/// </summary>
public static bool Remove(string key)
{
using (IRedisClient rds = prcm.GetClient())
{
return rds.Remove(key);
}
}
}
<add key="WriteServerList" value="XXXX@XX.XXX.XX.XX:6379"/>
<add key="ReadServerList" value="XXXX@XX.XXX.XX.XX:6379"/>
<add key="MaxWritePoolSize" value="10"/>
<add key="MaxReadPoolSize" value="10"/>
<add key="AutoStart" value="true"/>
除了ServiceStack,还有StackExchange.Redis、Beetle.Redis