Read Catalogue
- Begin
- Registering filter pipelines via IHttpModule
- by Basecontroller
- About sliding expiration
two different waysGo back to the top by registering the filter pipeline by IHttpModule the specific implementation is as follows: Declares a class CheckLoginModule.cs it inherits from the IHttpModule event that obtains the user state in the 9th event of the request pipeline to register the ONrequest event Determine if there is a corresponding client SessionID user information in the memcached does not exist then it means that the service side session fails to determine if there is a cookie reserved for one week (usually the site "Remember Me for a week" function) if there is a cookie information to obtain user information The write memcached assumes that when a user requests a/home/index page, we require that the user must log on to the server and that the user information does not exist then jump to the/home/login page for loginThe contents of the CheckLoginModule.cs file are as follows:
usingSystem;usingsystem.web;usingMvcmamcache.models;namespaceMvcmamcache.common { Public classCheckloginmodule:ihttpmodule {#regionIHttpModule Members Public voidInit (HttpApplication context) {context. AcquireRequestState+=NewEventHandler (onrequest); } Public voidONrequest (Objectsource, EventArgs E) {HttpApplication application= Source asHttpApplication;//Get ApplicationHttpContext context = Application. Context;//gets the request context.Uri URL = context. Request.url;//gets the URL of the current request if(URL.) Absolutepath.tolower () = ="/home/index") { varHttpCookie = context. request.cookies["SessionId"]; if(HttpCookie = =NULL|| Common.MemCacheHelper.GetValue (httpcookie.value) = =NULL) {//Session Sessions Expire//whether to keep 7 days if(Context. request.cookies["UI1"] ==NULL|| Context. request.cookies["Ui2"] ==NULL) {context. Response.Redirect ("/home/login"); } Else { //obtain a UserInfo object based on the user name and password stored in the cookie to the database here Direct newUserInfo UserInfo =NewUserInfo () {LoginId= Context. request.cookies["UI1"]. Value, Loginpwd= Context. request.cookies["Ui2"]. Value}; varSessionId =Guid.NewGuid (). ToString (); Context. response.cookies["SessionId"]. Value =sessionId; Common.MemCacheHelper.Insert (SessionId, userInfo); } } } } Public voidDispose () {Throw Newnotimplementedexception (); } #endregion }}
View Code
This filter module needs to be configured in the Web. config file
"checkloginmodule" type=" MvcMamcache.Common.CheckLoginModule"/>
Adjustment of Index and login method in home controller
Back to Top
by BasecontrollerCustom parent class Controller It inherits from controller initialize and declares the Loginuserinfo property to save user information for use by all child controllers
usingSystem;usingSYSTEM.WEB.MVC;usingMvcmamcache.models;namespaceMvcmamcache.common { Public classBasecontroller:controller {protectedUserInfo Loginuserinfo {Get; Set; } protected Override voidInitialize (System.Web.Routing.RequestContext requestcontext) {Base. Initialize (RequestContext); if(requestcontext.httpcontext.request.cookies["SessionId"] !=NULL) { stringGUID = requestcontext.httpcontext.request.cookies["SessionId"]. Value; Objectobj = Common.MemCacheHelper.GetValue (GUID);//Extracts data from the cache based on the value of the cookie. if(obj! =NULL) {UserInfo model= obj asUserInfo; Loginuserinfo=model; } Else{checkcookiesevenday (requestcontext);//7 Day Cookie Inspection } } Else{Response.Redirect ("/home/login"); } } Private voidCheckcookiesevenday (System.Web.Routing.RequestContext requestcontext) {varContext =Requestcontext.httpcontext; if(Context. request.cookies["UI1"] ==NULL|| Context. request.cookies["Ui2"] ==NULL) {context. Response.Redirect ("/home/login"); } Else { //obtain a UserInfo object based on the user name and password stored in the cookie to the database here Direct newUserInfo UserInfo =NewUserInfo () {LoginId= Context. request.cookies["UI1"]. Value, Loginpwd= Context. request.cookies["Ui2"]. Value}; varSessionId =Guid.NewGuid (). ToString (); Context. response.cookies["SessionId"]. Value =sessionId; Common.MemCacheHelper.Insert (SessionId, userInfo); } } }}
View CodeThe controller that requires user state verification
using SYSTEM.WEB.MVC; using Mvcmamcache.common; namespace mvcmamcache.controllers { publicclass Admincontroller:basecontroller { //// GET:/admin/public actionresult Index () { = Loginuserinfo.loginid; return View ();}} }
Back to Top
About sliding expiration in the first way we set the cache expiration time to 20 minutes but this is the absolute expiration time instead of sliding expiration if you want to implement sliding expiration, do so by memcached replace (Key,value,newtimespan) To update the expiration time is not adjusted on the original code, here are the following ideas:F (Common.MemCacheHelper.GetValue ("Getnow") ==NULL) {Common.MemCacheHelper.Insert ("Getnow", DateTime.Now, DateTime.Now.AddMinutes (1)); Viewbag.msg="New Time"+ Common.MemCacheHelper.GetValue ("Getnow"). ToString () +"the expiration time should be in:"+ (DateTime) Common.MemCacheHelper.GetValue ("Getnow")). AddMinutes (1); } Else{Common.MemCacheHelper.Replace ("Getnow", DateTime.Now, DateTime.Now.AddMinutes ( -)); Viewbag.msg="Replacement Time"+ Common.MemCacheHelper.GetValue ("Getnow"). ToString () +"the expiration time should be in:"+ (DateTime) Common.MemCacheHelper.GetValue ("Getnow")). AddMinutes ( -);
Of course, the alternative to the session is much more than just using memcached, but also through nosql non-relational databases, saying Managdb's efficiency is quite commendable.
Click to download Resources
Memcache+cookie Alternative Session solution (MVC Edition)