asp.net mvc 用Redis實現分布式叢集共用Session
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();
}
}
Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis串連Redis
Redis系列-安裝部署維護篇
CentOS 6.3安裝Redis
Redis安裝部署學習筆記
Redis設定檔redis.conf 詳解
Redis 的詳細介紹:請點這裡
Redis 的:請點這裡
本文永久更新連結地址: