首先.net需要引入如下几个文件,在gitub或者官网应该是有的:
然后配置一下redis服务器:
端口:
IP:
然后先启动 redis-server.exe:
出现如下效果表示成功
再启动:redis-cli.exe:
至此redis启动成功,关于redis的操作命令官网有详细介绍在此不作解释。
。net配置文件进行如下配置,端口号和IP要和redis的一致:
操作代码如下:
//链接redis 字符串 private static string[] ReadWriteHosts = System.Configuration.ConfigurationSettings.AppSettings["readWriteHosts"].Split(new char[] { ';' }); private static string[] ReadOnlyHosts = System.Configuration.ConfigurationSettings.AppSettings["readOnlyHosts"].Split(new char[] { ';' }); public static PooledRedisClientManager prcm = CreateManager(ReadWriteHosts, ReadOnlyHosts); public void AClick(object sender, EventArgs e) { Session["name"] = "学习nginx"; ////var client = new RedisClient("127.0.0.1", 8087); ////client.Set<string>("sName", Session["name"].ToString()); using (IRedisClient redis = prcm.GetClient()) { //判断redis 是否存在KEY if (!redis.ContainsKey(Session.SessionID)) { //设置redis的过期时间 redis.ExpireEntryAt(Session.SessionID, Convert.ToDateTime("2016-9-8")); //设置redis的值 redis.Set<object>(Session.SessionID, Session["name"]); } } } private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts) { // 支持读写分离,均衡负载 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig { MaxWritePoolSize = 5, // “写”链接池链接数 MaxReadPoolSize = 5, // “读”链接池链接数 AutoStart = true, }); }
不完善之处还请大家指出