Redis+cookie方式實現記錄用戶登錄狀態


1、這兩天研究Redis搞分布式session問題,網上找的資料都是用ServiceStack.Redis來實現的,但是在做性能測試的時候 發現最新的v4版本有限制每小時候最多請求6000次,因為官網開始商業化要收費了,好坑爹的說,還好我前期弄了個性能測試列子,不然上線以后出問題那就 麻煩了。后面找了個NServiceKit.Redis(好像就是ServiceStack.Redis的v3版本)來替代v4的收費版。

2、解決方案是 Redis+cookie方式實現記錄用戶登錄狀態

cookie:存放用戶的ID,這個ID是經過加密的,並且后台可以通過密鑰解密。

Redis:key/value 方式存儲,key存放比如:user_1。  value存放用戶實體對象。

3、先安裝一個Redis,windows的版本在本地進行測試,后期上線更換linux系統的Redis替換一下ip就可以了。

4、添加一個Session管理類

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<LoginUserInfo>(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<LoginUserInfo>(key, value, secondsTimeOut);
        }

        /// <summary>
        /// 移除Session
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        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;

        /// <summary>
        /// 緩沖池
        /// </summary>
        /// <param name="readWriteHosts"></param>
        /// <param name="readOnlyHosts"></param>
        /// <returns></returns>
        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,
                });
        }
        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="OpenPooledRedis">是否開啟緩沖池</param>
        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;
            }
        }
        /// <summary>
        /// 距離過期時間還有多少秒
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long TTL(string key)
        {
            return Redis.Ttl(key);
        }
        /// <summary>
        /// 設置過期時間
        /// </summary>
        /// <param name="key"></param>
        /// <param name="timeout"></param>
        public void Expire(string key,int timeout = 0)
        {
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }
        }

        #region Key/Value存儲
        /// <summary>
        /// 設置緩存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">緩存建</param>
        /// <param name="t">緩存值</param>
        /// <param name="timeout">過期時間,單位秒,-1:不過期,0:默認過期時間</param>
        /// <returns></returns>
        public bool Set<T>(string key, T t, int timeout = 0)
        {
            Redis.Set<T>(key, t);
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }
            return true;
           
        }
        /// <summary>
        /// 獲取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            return Redis.Get<T>(key);
        }
        /// <summary>
        /// 刪除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(string key)
        {
            return Redis.Remove(key);
        }
        #endregion

        //釋放資源
        public void Dispose()
        {
            if (Redis != null)
            {
                Redis.Dispose();
                Redis = null;
            }
            GC.Collect();

        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM