1. 程式人生 > >asp.net mvc 用Redis實現分散式叢集共享Session

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就可以了。

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 http://www.codesec.net/Linux/2013-06/85816.htm

Redis系列-安裝部署維護篇 http://www.codesec.net/Linux/2012-12/75627.htm

CentOS 6.3安裝Redis http://www.codesec.net/Linux/2012-12/75314.htm

Redis安裝部署學習筆記 http://www.codesec.net/Linux/2014-07/104306.htm

Redis配置檔案redis.conf 詳解 http://www.codesec.net/Linux/2013-11/92524.htm

Redis 的詳細介紹:請點這裡 
Redis 的下載地址:請點這裡

本文地址:http://www.codesec.net/Linux/2015-07/120105.htm