0.什麼是Redis
Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基於內存亦可持久化的日志型、Key-Value數據庫,並提供多種語言的API
1.與其他用戶狀態保存方案比較
一般開發中用戶狀態使用session或者cookie,兩種方式各種利弊。
Session:在InProc模式下容易丟失,並且引起並發問題。如果使用SQLServer或者SQLServer模式又消耗了性能
Cookie則容易將一些用戶信息暴露,加解密同樣也消耗了性能。
Redis采用這樣的方案解決了幾個問題,
①.Redis存取速度快。
②.用戶數據不容易丟失。
③.用戶多的情況下容易支持集群。
④.能夠查看在線用戶。
⑤.能夠實現用戶一處登錄。(通過代碼實現,後續介紹)
⑥.支持持久化。(當然可能沒什麼用)
2.實現思路
1.我們知道session其實是在cookie中保存了一個sessionid,用戶每次訪問都將sessionid發給服務器,服務器通過ID查找用戶對應的狀態數據。
在這裡我的處理方式也是在cookie中定義一個sessionid,程序需要取得用戶狀態時將sessionid做為key在Redis中查找。
2.同時session支持用戶在一定時間不訪問將session回收。
借用Redis中Keys支持過期時間的特性支持這個功能,但是在續期方面需要程序自行攔截請求調用這個方法(demo有例子)
下面開始代碼說明
3.Redis調用接口
首先引用ServiceStack相關DLL。
在web.config添加配置,這個配置用來設置Redis調用地址每台服務用【,】隔開。主機寫在第一位
<appSettings> <!--每台Redis之間用,分割.第一個必須為主機--> <add key="SessionRedis" value="127.0.0.1:6384,127.0.0.1:6384"/> </appSettings>
初始化配置
static Managers()
{
string sessionRedis= ConfigurationManager.AppSettings["SessionRedis"];
string timeOut = ConfigurationManager.AppSettings["SessionRedisTimeOut"];
if (string.IsNullOrEmpty(sessionRedis))
{
throw new Exception("web.config 缺少配置SessionRedis,每台Redis之間用,分割.第一個必須為主機");
}
if (string.IsNullOrEmpty(timeOut)==false)
{
TimeOut = Convert.ToInt32(timeOut);
}
var host = sessionRedis.Split(char.Parse(","));
var writeHost = new string[] { host[0] };
var readHosts = host.Skip(1).ToArray();
ClientManagers = new PooledRedisClientManager(writeHost, readHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = writeReadCount,//“寫”鏈接池鏈接數
MaxReadPoolSize = writeReadCount,//“讀”鏈接池鏈接數
AutoStart = true
});
}
為了控制方便寫了一個委托
/// <summary>
/// 寫入
/// </summary>
/// <typeparam name="F"></typeparam>
/// <param name="doWrite"></param>
/// <returns></returns>
public F TryRedisWrite<F>(Func<IRedisClient, F> doWrite)
{
PooledRedisClientManager prcm = new Managers().GetClientManagers();
IRedisClient client = null;
try
{
using (client = prcm.GetClient())
{
return doWrite(client);
}
}
catch (RedisException)
{
throw new Exception("Redis寫入異常.Host:" + client.Host + ",Port:" + client.Port);
}
finally
{
if (client != null)
{
client.Dispose();
}
}
}
一個調用的例子其他的具體看源碼
/// <summary>
/// 以Key/Value的形式存儲對象到緩存中
/// </summary>
/// <typeparam name="T">對象類別</typeparam>
/// <param name="value">要寫入的集合</param>
public void KSet(Dictionary<string, T> value)
{
Func<IRedisClient, bool> fun = (IRedisClient client) =>
{
client.SetAll<T>(value);
return true;
};
TryRedisWrite(fun);
}
4.實現Session
按上面說的給cookie寫一個sessionid
/// <summary>
/// 用戶狀態管理
/// </summary>
public class Session
{
/// <summary>
/// 初始化
/// </summary>
/// <param name="_context"></param>
public Session(HttpContextBase _context)
{
var context = _context;
var cookie = context.Request.Cookies.Get(SessionName);
if (cookie == null || string.IsNullOrEmpty(cookie.Value))
{
SessionId = NewGuid();
context.Response.Cookies.Add(new HttpCookie(SessionName, SessionId));
context.Request.Cookies.Add(new HttpCookie(SessionName, SessionId));
}
else
{
SessionId = cookie.Value;
}
}
}
去存取用戶的方法
/// <summary>
/// 獲取當前用戶信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public object Get<T>() where T:class,new()
{
return new RedisClient<T>().KGet(SessionId);
}
/// <summary>
/// 用戶是否在線
/// </summary>
/// <returns></returns>
public bool IsLogin()
{
return new RedisClient<object>().KIsExist(SessionId);
}
/// <summary>
/// 登錄
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
public void Login<T>(T obj) where T : class,new()
{
new RedisClient<T>().KSet(SessionId, obj, new TimeSpan(0, Managers.TimeOut, 0));
}
6.續期
默認用戶沒訪問超過30分鐘注銷用戶的登錄狀態,所以用戶每次訪問都要將用戶的注銷時間推遲30分鐘
這需要調用Redis的續期方法
/// <summary>
/// 延期
/// </summary>
/// <param name="key"></param>
/// <param name="expiresTime"></param>
public void KSetEntryIn(string key, TimeSpan expiresTime)
{
Func<IRedisClient, bool> fun = (IRedisClient client) =>
{
client.ExpireEntryIn(key, expiresTime);
return false;
};
TryRedisWrite(fun);
}
封裝以後
/// <summary>
/// 續期
/// </summary>
public void Postpone()
{
new RedisClient<object>().KSetEntryIn(SessionId, new TimeSpan(0, Managers.TimeOut, 0));
}
這裡我利用了MVC3中的ActionFilter,攔截用戶的所有請求
namespace Test
{
public class SessionFilterAttribute : ActionFilterAttribute
{
/// <summary>
/// 每次請求都續期
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
new Session(filterContext.HttpContext).Postpone();
}
}
}
在Global.asax中要注冊一下
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new SessionFilterAttribute());
}
protected void Application_Start()
{
RegisterGlobalFilters(GlobalFilters.Filters);
}
5.調用方式
為了方便調用借用4.0中的新特性,把Controller添加一個擴展屬性
public static class ExtSessions
{public static Session SessionExt(this Controller controller)
{
return new Session(controller.HttpContext);
}
}
調用方法
public class HomeController : Controller
{
public ActionResult Index()
{
this.SessionExt().IsLogin();
return View();
}
}
6.代碼下載
點擊下載
7.後續
SessionManager包含 獲取用戶列表數量,注銷某個用戶,根據用戶ID獲取用戶信息,在線用戶對象列表,在線用戶SessionId列表等方法
後續將實現用戶一處登錄功能
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。