日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net mvc 用Redis实现分布式集群共享Session。

發(fā)布時間:2023/11/30 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net mvc 用Redis实现分布式集群共享Session。 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

2、解決方案是 Redis+cookie方式實現(xiàn)記錄用戶登錄狀態(tài)

? ? ?cookie:存放用戶的ID,這個ID是經(jīng)過加密的,并且后臺可以通過密鑰解密。

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

3、先安裝一個Redis,windows的版本在本地進行測試,后期上線更換linux系統(tǒng)的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>/// 構造函數(shù)/// </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();}}

?

轉載于:https://www.cnblogs.com/valen/p/4655427.html

總結

以上是生活随笔為你收集整理的asp.net mvc 用Redis实现分布式集群共享Session。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。