一个简单的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 = ;
try
{
count=service.Count(" where username='"+username+"' and password='"+password+"' "); if (count == )
{
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());
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()); } /// <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

【原创】基于Memcached 实现用户登录的Demo(附源码)的更多相关文章

  1. 一个简单的IM系统(Demo附源码)-- ESFramework 4.0 快速上手(08)

    前面的文章已经介绍完了基于ESFramework/ESPlus进行二次开发的所有要点,现在,我们可以开始小试牛刀了. 本文将介绍使用ESFramework的Rapid引擎开发的两个最简单的Demo,E ...

  2. struts1实现简单的登录功能(附源码)

    环境:MyEclipse 14                                                                                     ...

  3. 干货——基于Nop的精简版开发框架(附源码)

    .NET的开发人员应该都知道这个大名鼎鼎的高质量b2c开源项目-nopCommerce,基于EntityFramework和MVC开发,拥有透明且结构良好的解决方案,同时结合了开源和商业软件的最佳特性 ...

  4. [原创]Net实现Excel导入导出到数据库(附源码)

    关于数据库导出到Excel和SQLServer数据导出到Excel的例子,在博客园有很多的例子,自己根据网上搜集资料,自己做了亦歌简单的demo,现在分享出来供初学者学习交流使用. 一.数据库导入导出 ...

  5. 基于Python接口自动化测试框架(初级篇)附源码

    引言 很多人都知道,目前市场上很多自动化测试工具,比如:Jmeter,Postman,TestLink等,还有一些自动化测试平台,那为啥还要开发接口自动化测试框架呢?相同之处就不说了,先说一下工具的局 ...

  6. Java:基于AOP的动态数据源切换(附源码)

    1 动态数据源的必要性 我们知道,物理服务机的CPU.内存.存储空间.连接数等资源都是有限的,某个时段大量连接同时执行操作,会导致数据库在处理上遇到性能瓶颈.而在复杂的互联网业务场景下,系统流量日益膨 ...

  7. Docker Compose部署项目到容器-基于Tomcat和mysql的商城项目(附源码和sql下载)

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  8. 基于Struts2+Hibernate开发小区物业管理系统 附源码

    开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 运行效果图: 源码及原文链接:https://javadao.xyz/forum.php?mo ...

  9. 基于Python接口自动化测试框架+数据与代码分离(进阶篇)附源码

    引言 在上一篇<基于Python接口自动化测试框架(初级篇)附源码>讲过了接口自动化测试框架的搭建,最核心的模块功能就是测试数据库初始化,再来看看之前的框架结构: 可以看出testcase ...

随机推荐

  1. 设置IE兼容模式的几种方法

    一.指定文件兼容性模式要为你的网页指定文件模式,需要在你的网页中使用meta元素放入x-ua-compatible http-equiv 标头.1. 强制ie8使用ie8模式来解析,而且那个兼容性视图 ...

  2. keepalived初探

    keepalived起初是为LVS设计的,专门用来监控LVS集群系统中各个real server的健康状况的,后来又在其中实现了VRRP协议,VRRP即virtual router redundanc ...

  3. VISUAL STUDIO 调试

    调试术语 Visual Studio调试之断点基础篇 Visual Studio调试之断点进阶篇 不能设置断点的检查步骤 Visual Studio调试之断点技巧篇 Visual Studio调试之断 ...

  4. Dos

    一.简介 https://zh.wikipedia.org/wiki/DOS 二.系统下载 http://www.cn-dos.net/newdos/doswarea.htm 三.实用命令 1)查看系 ...

  5. JavaScript 题目破解过程与解析

    题目来源 https://www.hackthissite.org/missions/javascript/ HackThisSite JavaScript mission 1-7 1 我先尝试输入  ...

  6. web前端的渐进增强式开发模型

    渐进增强是前端开发的根本基础.从根本的层面上讲,它可以将HTML,CSS,JavaScript这三者的功能分离开来,这能让当前的项目开一个好头.我们在创建项目的开始要将这三者分开,它们对应的称呼是结构 ...

  7. 项目回顾1-图片上传-form表单还是base64-前端图片压缩

    第一个项目终于上线了,是一个叫亲青筹的公益众筹平台,微信端,电脑端还有后台界面大部分都是我完成的,几个月过来,感觉收获了很多,觉得要总结一下. 首先想到的是图片上传的问题.在通常表单数据都是ajax上 ...

  8. EDM总结

    1.  使用table 布局,align="center" 2.  尽量不要用padding-left ,padding-right 样式: 3.  CSS的浮动定位: 4.  不 ...

  9. HDU 4793 Collision --解方程

    题意: 给一个圆盘,圆心为(0,0),半径为Rm, 然后给一个圆形区域,圆心同此圆盘,半径为R(R>Rm),一枚硬币(圆形),圆心为(x,y),半径为r,一定在圆形区域外面,速度向量为(vx,v ...

  10. 2014 Super Training #8 C An Easy Game --DP

    原题:ZOJ 3791 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3791 题意:给定两个0-1序列s1, s2,操作t ...