zoukankan      html  css  js  c++  java
  • 学习笔记48_Memcache跟用户登录模块结合

    public interface ICacheWriter

    {

       void AddCache(string key,object value, DateTime expDate);

       void AddCache(string key,object value);

       object GetCache(string key);  

       T GetCache<T>(string key);

       void SetCache(string key,object value,DateTime extDate);

       void SetCache(string key,object value);

    }

    //实现一:

    public class HttpRuntimeCacheWritter : ICacheWriter

    {

      public  void AddCache(string key,object value,DataTime expDate)

      {

          HttpRuntime.Cache.Insert(key,value,null,expDate,TimeSpan.Zero);

      }

      public   void AddCache(string key,object value)

      {

         HttpRuntime.Cache.Insert(key,value);

      }

      public   object GetCache(string key)

      {

        HttpRuntime.Cache[Key];

      }

      public T GetCache<T> (string key )

      {

        return (T)HttpRuntime.cache[Key];

      }

         public  void SetCache(string key,object value,DateTime extDate)

      {

         HttpRuntime.Cache.Remove(key);

         AddCache(key,value,extDate);

      }

        public  void SetCache(string key,object value)

      {

        HttpRuntime.Cache.Remove(key);

         AddCache(key,value);

      }

    }

    //实现二:

    可以在Web.config中,配置可以连接那些服务器:

    //

    <configuration>

      <appSettings>

        <add key= "MemcachedServerList" value="192.168.1.100:11211,192.168.1.118:11211"/>

      </appSettings>

    </configuration>

    //

    public class MemCacheWritter : ICacheWriter

    {

      private MemCachedClient memcachedClient;

      //

      public MemcacheWritter()

      { 

        // 分布Memcachedf服务IP 端口 

        //string[] servers = {"192.168.1.100:11211","192.168.1.118:11211"};//意思是可以将数据放到两台电脑当中

        string[] servers = System.configuration.ConfigurationManager.AppSettings["MemcachedServerList"].Split(",")

        //初始化池

        SockIOPool pool = SockIOPool.GetInstance();

        pool.SetServers(servers);

        pool.InitConnections = 3;

        pool.MinConnections = 3;

        pool.MaxConnections = 5;

        pool.SocketConnectTimeout = 1000;

        pool.SocketTimeout = 3000;

        pool.MaintenancelSleep = 30;

        pool.Failover = true;

        pool.Nagle = false;

        pool.Initialize();

        //客户端实例化

        memcachedClient = new MemcacheClient();

        mc.EnableCompression = false;

        mc.Add("keyddd","sssssssssss"); //进行哈希算法,以决定存到哪台机器上

      }

      

           //

      public  void AddCache(string key,object value,DataTime expDate)

      {

          //注意,传入的value是要有标记为[Serialzable]的

          memcachedClient.Add(key,value,expDate);

      }

      public   void AddCache(string key,object value)

      {

          memcachedClient.Add(key,value);

      }

      public   object GetCache(string key)

      {

           memcacheClient.Get(key);

      }

      public T GetCache<T> (string key )

      {

         return (T)memcacheClient.Get(key);

      }

           public  void SetCache(string key,object value,DateTime extDate)

      {

        memcacheClient.Set(key,value,extDate);

      }

        public  void SetCache(string key,object value)

      {

        memcahce.set(key,value);

      }

    }

    *************************************************分割线**********************************************************

    //此类事用来提供ICacheWriter具体类以及其方法

    publc class CacheHelper

    {

      // 这里可以使用Spring 来注入

        public static ICacheWritter  CacheWritter {get;set;}

      //静态构造函数只会执行一次

      static CacheHelper()

      {

        //在String.core.dll

        Spring.Context.IApplicationContext ctx = ContextRegistry.GetContext();

        //此处不是为了获得CacheHelper对象,而是为了在实例化对象的时候,顺便

        根据<property name="CacheWriter" ref="CacheWriter"/>来设置CacheWriter

            ctx.GetObject("CacheHelper");

        //也能

        //CacheWriter = ctx.GetObject("CacheWriter") as ICacheWritter;

      }

      //

      public statci void AddCache(string key,object value,DataTime expDate)

      {

        //往缓存写:单机,分布式,修改一下配置,就能切换

        

      }

      public static void AddCache(string key,object value)

      {

        

      }

      public static object GetCache(string key)

      {

        

      }

         public  void SetCache(string key,object value,DateTime extDate)

      {

        ....

      }

        public  void SetCache(string key,object value)

      {

          .....

      }

    }

     //Spring注入,在common.xml

    <objects xmlns="...">

      <!--使用单机模式--->

      <!--<objcet name="CacheWriter"  type = "xxxx.xxx.HttpRuntimeCacheWritter(类全名),XXX.XXX.XXX(程序集名)"    singleton="false"></object>-->

      <!--使用memcahced分布式缓存--->

      <objcet name="CacheWriter(对应下面的ref)"  type = "xxxx.xxx.MemcahcedWritter(类全名),XXX.XXX.XXX(程序集                名)" singleton="false"></object>

      <!--对CacheHelper进行属性值注入,此处CacheWriter就是CacheHelper的属性-->

      <object name="CacheHelper" type=xxxx.xxx.CacheHelper,xxx.xxx>

        <property name="CacheWriter" ref="CacheWriter(对应上面的name)"/>

        </object>

    </objects>

     //Web.config中

    <configuration>

      <!--Spring.Net配置节点-->

      <Spring>

        <context>

          <resoruce url="file://~/config/common.xml"/> <!--放到第一行,是因为后边的配置文件要用到它-->

          <resoruce url="file://~/config/其他配置文件.xml"/>

        </context>

      </Spring>

    </configuration>

     ***************************************************分割线**********************************************************

    // 登录控制器

     public AciontResult ProcessLogin()//此处可以定义一个ViewMode来接收,单是ViewMode的属性名,应该和html控件的name一致

    {

      //处理验证码

      string strCode= Request["控件name"];

      string sessionCode = Session["VCode"] as string;

      Session["VCode"] = null;//**验证码取一次就应该让其为null

      if(string.IsNullOrEmpty|| sessionCode!=null )

      {

        return Content("验证码错误");//返回这串,给前台在html标签中显示

      }

      //处理用户名密码

      string uid  = Request["LoginCode"];

      string pwd = Request["LoginPwd"];

         //var userinfo =   Service.GetEntities(u=>u.uid==uid&&u.pwd==pwd).FirstOrDefault();

            **不用EF也行,反正要获取到实例

      //if(userinfo==null)

      //{

        //return Content("密码错误");//返回这串,给前台在html标签中显示

      //} 

      //session["LoginUser"] = userinfo;//是使用session来记录会话状态

           改为:

      var guid = Guid.NewGuid();

      CacheHelper .AddCache(guid ,userinfo,DataTime.Now.AddMinutes(20));//20分钟过期

      //

         Respose.Cookie.add(new cookie("UserLoginId",Guid));

      //如果正确那么跳转到首页

      return Content("ok");//后台不要直接使得页面重定向,为了接口通用性,以及前台逻辑问题

      //上边是返回 Content(""),这里应该也是返回Content("")

    }

     ***************************************************分割线**********************************************************

    public class BaseController:Controller//本来

    {

        protected bool needCheck=true;

      protected UserInfo currentUser{get;set;}

      protect override void OnActionExecuting(ActionExecutingContext filterContext)

      {

        //在当前的控制器里面所有的方法执行之前,都先执行此代码

        base.OnActionExecuting(filterContext);

        //跟上面的LoginCheckFilterAttribute的代码

        if(needCheck==false) return;

        //检验用户是否登录

        currentUser = filterContext.HttpContext.Session["LoginUser"] as UserInfo;

        //这里使用

        string userGuid = Request.Cookies["UserLoginId"].value;

        currentUser = CacheHelper.Get("userGuid") as UserInfo;

        if( currentUser ==null)

        {

          filterContext.HttpContext.Respone.Redirect("/UserLogin/Login");

        }

        else 

        {

             //过期时间更新

          CacheHelper.Set("LoginUser",currentUser,DataTime.Now.AddMinutes(20));

        }

      }

    }

  • 相关阅读:
    uboot nand erase 的显示错误修复
    Sougo for linux install.
    S3C6410移植uboot2010.3(2)基本的启动信息修改
    S3C6410移植uboot2010.3(4)uboot的dnw功能添加
    S3C6410移植uboot2010.3(3)正常化配置
    ubuntu乱码修复
    应老婆点(20070705 13:11:34)(新浪)
    克己慎独 2008923 13:32:00 (21ic)
    信任(20061229 14:16:32)(新浪)
    不要轻易承诺 2008926 14:42:00 (21ic)
  • 原文地址:https://www.cnblogs.com/pylblog/p/7466780.html
Copyright © 2011-2022 走看看