一個簡單的Memcached在Net中運用的一個demo。主要技術 Dapper+MVC+Memcached+sqlserver,
開發工具為vs2015+Sql
效果圖如下:
登錄后
解決方案
主要實現代碼
using Model; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using website.Models; namespace website.Controllers { public class LoginController : Controller { // GET: Login BLL.Users service = new BLL.Users(); public ActionResult Index() { return View(); } [HttpPost] public ActionResult Login(string username,string password) { int count = 0; try { count=service.Count(" where username='"+username+"' and password='"+password+"' "); if (count == 0) { return Json(new { success = false, msg = "用戶名或密碼不正確" }); } else { var loginUser = service.QueryList(" where username='" + username + "' and password='" + password + "' ").SingleOrDefault(); Guid sessionId = Guid.NewGuid();//申請了一個模擬的GUID:SessionId //把sessionid寫到客戶端瀏覽器里 Response.Cookies["sessionId"].Value = sessionId.ToString(); //可以緩存model 也可緩存list MemcacheHelper.Set(sessionId.ToString(), loginUser, DateTime.Now.AddMinutes(20)); return Json(new { success = true, msg = "登陸成功" }); } } catch(Exception ex) { return Json(new { success = false, msg = ex.Message }); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Model; using System.Net.Http; using website.Models; namespace website.Controllers { public class BaseController : Controller { BLL.Users service = new BLL.Users(); protected string hostUrl = ""; Users currentuser = new Users(); public ActionResult Layout() { Users user = GetCurrentUser(); ViewData["username"] = user.UserName; ViewData["TrueName"] = user.TrueName; return View("~/Views/Shared/_MyLayout.cshtml"); // return View(); } /// <summary> /// Action執行前判斷 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); //從cookie中獲取登錄的sessionId string sessionId = Request["sessionId"]; if (string.IsNullOrEmpty(sessionId)) { Response.Redirect("/Login/Index"); } object obj = MemcacheHelper.Get(sessionId); Users user = obj as Users; if (user == null) { Response.Redirect("/Login/Index"); } currentuser = user; MemcacheHelper.Set(sessionId, user, DateTime.Now.AddMinutes(20)); } /// <summary> /// 判斷是否登錄 /// </summary> protected bool checkLogin() { //HttpCookie _cookie = httpContext.Request.Cookies["CookieUser"]; if (this.Session["userinfo"] == null) { return false; } return true; } /// <summary> /// 返回當前登錄用戶 /// </summary> /// <returns></returns> protected Users GetCurrentUser() { if (checkLogin()) { currentuser = service.QueryList(" where username='" + this.Session["userinfo"].ToString() + "'").SingleOrDefault(); } return currentuser; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Memcached.ClientLibrary; namespace website.Models { public static class MemcacheHelper { private static MemcachedClient mc; static MemcacheHelper() { String[] serverlist = { "127.0.0.1:11211" }; // initialize the pool for memcache servers SockIOPool pool = SockIOPool.GetInstance("test"); pool.SetServers(serverlist); pool.Initialize(); mc = new MemcachedClient(); mc.PoolName = "test"; mc.EnableCompression = false; } public static bool Set(string key, object value,DateTime expiry){ return mc.Set(key, value, expiry); } public static object Get(string key) { return mc.Get(key); } } }
總結:自己練習的Demo,有很多地方不完善,歡迎指正!!
https://yunpan.cn/c63VD4ekxn78b 訪問密碼 2f97