asp.netmvc用Redis实现分布式集群共享Session

asp.netmvc用Redis实现分布式集群共享Session

1、这两天研究Redis搞分布式问题,网上找的资料都是用.Redis来实现的,但是在做性能测试的时候发现最新的v4版本有限制每小时候最多请求6000次,因为官网开始商业化要收费了nservicekit.redis,好坑爹的说nservicekit.redisnservicekit.redis,还好我前期弄了个性能测试列子,不然上线以后出问题那就麻烦了。后面找了个.Redis(好像就是.Redis的v3版本)来替代v4的收费版。

2、解决方案是 Redis+方式实现记录用户登录状态

:存放用户的ID,这个ID是经过加密的,并且后台可以通过密钥解密。

Redis:key/value 方式存储,key存放比如:。  value存放用户实体对象。

3、先安装一个Redisasp.netmvc用Redis实现分布式集群共享Session,的版本在本地进行测试asp.netmvc用Redis实现分布式集群共享Session,后期上线更换linux系统的Redis替换一下ip就可以了。

4、添加一个管理类

public class SessionHelper
    {
        private const int secondsTimeOut = 60 * 20;  //默认过期时间20分钟  单位秒
        public RedisHelper Redis = new RedisHelper(false);
        public LoginUserInfo this[string key]
        {
            get
            {
                string webCookie = WebHelper.GetCookie(key);
                if (webCookie == "")
                {
                    return null;
                }
                key = key + "_" + SecureHelper.AESDecrypt(webCookie);
                //距离过期时间还有多少秒
                long l = Redis.TTL(key);
                if (l >= 0)
                {
                    Redis.Expire(key, secondsTimeOut);
                }
                
                return Redis.Get(key);
            }
            set
            {
                SetSession(key, value);
            }
        }
        public void SetSession(string key, LoginUserInfo value)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new Exception("Key is Null or Epmty");
            }
            WebHelper.SetCookie(key, SecureHelper.AESEncrypt(value.ID.ToString()));
            key = key + "_" + value.ID;
            Redis.Set(key, value, secondsTimeOut);
        }
        /// 
        /// 移除Session
        /// 
        /// 
        /// 
        public bool Remove(string key)
        {
            var rs = Redis.Remove(key + "_" + SecureHelper.AESDecrypt(WebHelper.GetCookie(key)));
            WebHelper.DeleteCookie(key);
            return rs;
        }
             
    }

5、Redis操作类

public class RedisHelper : IDisposable
    {
       private RedisClient Redis = new RedisClient("127.0.0.1", 6379);
        //缓存池
        PooledRedisClientManager prcm = new PooledRedisClientManager();
        //默认缓存过期时间单位秒
        public int secondsTimeOut = 20 * 60;
        /// 
        /// 缓冲池
        /// 
        /// 
        /// 
        /// 
        public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
        {
            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts,
                new RedisClientManagerConfig
                {
                    MaxWritePoolSize = readWriteHosts.Length * 5,
                    MaxReadPoolSize = readOnlyHosts.Length * 5,
                    AutoStart = true,
                }); 
        }
        /// 
        /// 构造函数
        /// 
        /// 是否开启缓冲池
        public RedisHelper(bool OpenPooledRedis = false)
        {
            if (OpenPooledRedis)
            {
                prcm = CreateManager(new string[] { "127.0.0.1:6379" }, new string[] { "127.0.0.1:6379" });
                Redis = prcm.GetClient() as RedisClient;
            }
        }
        /// 
        /// 距离过期时间还有多少秒
        /// 
        /// 
        /// 
        public long TTL(string key)
        {
            return Redis.Ttl(key);
        }
        /// 
        /// 设置过期时间
        /// 
        /// 
        /// 
        public void Expire(string key,int timeout = 0)
        {
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }
        }
        #region Key/Value存储
        /// 
        /// 设置缓存
        /// 
        /// 
        /// 缓存建
        /// 缓存值
        /// 过期时间,单位秒,-1:不过期,0:默认过期时间
        /// 
        public bool Set(string key, T t, int timeout = 0)
        {
            Redis.Set(key, t);
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }
            return true;
            
        }
        /// 
        /// 获取
        /// 
        /// 
        /// 
        /// 
        public T Get(string key)
        {
            return Redis.Get(key);
        }
        /// 
        /// 删除
        /// 
        /// 
        /// 
        public bool Remove(string key)
        {
            return Redis.Remove(key);
        }
        #endregion
        //释放资源
        public void Dispose()
        {
            if (Redis != null)
            {
                Redis.Dispose();
                Redis = null;
            }
            GC.Collect();
        }
    }

公告:
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299

资源库 - 资源分享下载网 » asp.netmvc用Redis实现分布式集群共享Session

常见问题FAQ

关于资源售价和售后服务的说明?
本站所有资源的标价均为本站收集资源的辛苦费,不代表资源本身的价值。软件是高智慧高价值的商品,不可能是白菜价。本站资源标价只是赞助费用,收取的赞助费仅用来维持本站的日常运营!毕竟收集整理资料需要投入云计算资源和站长大量的精力。
代码有没有售后服务和技术支持?
由于代码的运行具有不可预见性,本站不保证代码完整可运行,不提供技术支持和售后服务。 本站原创代码都是站长自己开发的,可以有偿提供技术支持服务。 网站里标明【亲测】的代码都是站长亲测过的,其他的代码由于精力有限,没有一一测试,不能保证代码就一定能够使用,更没有技术支持服务,下载前请自行斟酌。
有没有搭建服务?
由于搭建服务比较费时费力,所以本站除了原创代码外均不提供搭建服务。本站分享代码纯属兴趣爱好,不以盈利为目的,请勿咨询有没有搭建服务,谢谢理解。
链接地址失效了怎么办?
请带上资源链接地址联系客服,工作时间内我们看到后将第一时间回复。
关于解压密码
本站资源一般都没有加密,如果发现需要解压密码的,那么就输入 hao.35dc.com 试试。

发表评论

资源库,由老程序员细心甄别、精心筛选,只为提供优质的源码资源

关于我们 联系我们