.Net网站架构设计(四) 缓存技术

.Net网站架构设计(四) 缓存技术

.Net网站架构设计(三) 缓存

在.Net framework 下面有很多缓存参考

使用Asp.Net缓存;

使用Remoting Singleton缓存;

使用内存映射文件;

使用SQL Server缓存;

使用静态变量缓存;

使用Asp.net 会话状态(Session State);

使用Asp.net客户端缓存和状态;

使用Internet Explorer缓存。

有关他们的使用请参考 .net 缓存方案


而我今天要介绍的互联网下面的Redis缓存。

一、用Redis 共享IIs 集群的 Session信息。

IIs 集群 Session 会话这么保存?

当然你可以采用传统的方式,让其保存在SateManager服务器,或者缓存到数据中。

用户Cookie 把Session key 保存起来,把SessionKey 做为 Redis  存储的Key ,LoginUserInfo 作为登录用户的value;

多个IIS 集群 调用 Redis 集群。获取用户信息。

详细参考NET下Session共享的几种实现方式
v一下是笔者写的MVC 下面 Redis key 的缓存

[csharp]  view plain  copy
  1. using Liming.BaseSystem.Model;  
  2. using Liming.Cache;  
  3. using NServiceKit.Redis;  
  4. using System;  
  5. using System.Collections.Generic;  
  6. using System.Linq;  
  7. using System.Web;  
  8.   
  9. namespace Liming.Web.Comm  
  10. {  
  11.     public class RedisHelper  
  12.     {  
  13.         public static string Host = "127.0.0.1";  
  14.         public const string COOKIE_KEY = "LoginInfoSessionKey";  
  15.         public LoginUserInfo GetLoginUser(HttpRequestBase Request)  
  16.         {  
  17.             if (Request != null && Request.Cookies.Get(COOKIE_KEY) != null)  
  18.             {  
  19.                 String SessionKeyValue = Request.Cookies.Get(COOKIE_KEY).Value;  
  20.   
  21.                 using (RedisClient redisClient = new RedisClient(Host))  
  22.                 {  
  23.                     LoginUserInfo ulr = redisClient.Get<LoginUserInfo>(SessionKeyValue);  
  24.                     return ulr;  
  25.                 }  
  26.             }  
  27.             else  
  28.             {  
  29.                 return null;  
  30.             }  
  31.         }  
  32.   
  33.         public LoginUserInfo GetLoginUser(string LoginInfoSessionKey)  
  34.         {  
  35.             if(LoginInfoSessionKey.Trim()=="")  
  36.             {  
  37.                 return null;  
  38.             }  
  39.             using (RedisClient redisClient = new RedisClient(Host))  
  40.             {  
  41.                 LoginUserInfo ulr = redisClient.Get<LoginUserInfo>(LoginInfoSessionKey.Trim());  
  42.                 return ulr;  
  43.             }  
  44.         }  
  45.   
  46.         public void LoginUserInfoRemove(HttpContextBase Context)  
  47.         {  
  48.                 HttpCookie cookie = new HttpCookie(COOKIE_KEY,"");  
  49.                 Context.Response.Cookies.Set(cookie);  
  50.             //Context.Response.Cookies.Remove(COOKIE_KEY);  
  51.         }  
  52.         public string RegistUser(HttpContextBase Context, LoginUserInfo ulr)  
  53.         {  
  54.             string SessionKeyValue = "";  
  55.             HttpCookie cookie = new HttpCookie(COOKIE_KEY, SessionKeyValue);  
  56.             if (Context.Request.Cookies.Get(COOKIE_KEY) != null)  
  57.             {  
  58.                 SessionKeyValue = Context.Request.Cookies.Get(COOKIE_KEY).Value;  
  59.                 if (SessionKeyValue.Trim() == "")  
  60.                 {  
  61.                     SessionKeyValue = Guid.NewGuid().ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd"); ;  
  62.   
  63.                 }  
  64.                 using (RedisClient redisClient = new RedisClient(Host))  
  65.                 {  
  66.                     ulr.LoginInfoSessionKey = SessionKeyValue;  
  67.                     cookie = new HttpCookie(COOKIE_KEY, SessionKeyValue);  
  68.                     redisClient.Set<LoginUserInfo>(SessionKeyValue, ulr, DateTime.Now.AddDays(7));//7天后过期  
  69.                 }  
  70.             }  
  71.             else  
  72.             {  
  73.                 SessionKeyValue = Guid.NewGuid().ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd"); ;  
  74.                 using (RedisClient redisClient = new RedisClient(Host))  
  75.                 {  
  76.                     ulr.LoginInfoSessionKey = SessionKeyValue;  
  77.                     cookie = new HttpCookie(COOKIE_KEY, SessionKeyValue);  
  78.                     redisClient.Set<LoginUserInfo>(SessionKeyValue, ulr, DateTime.Now.AddDays(7));//7天后过期  
  79.                 }  
  80.             }  
  81.             Context.Response.Cookies.Add(cookie);  
  82.             return SessionKeyValue;  
  83.         }  
  84.         /// <summary>  
  85.         /// 保存登陆用户信息到redisServer  
  86.         /// </summary>  
  87.         /// <param name="ulr"></param>  
  88.         public void SaveLoginUser(<pre name="code" class="csharp">public class LoginUserInfo  
  89. {  
  90. public string UserID {get;set;}  
  91. public string UserName{get;set;}  
  92.   
  93.   
  94. }  

ulr) { using (RedisClient redisClient = new RedisClient(Host)) { redisClient.Set<LoginUserInfo>(ulr.LoginInfoSessionKey, ulr, DateTime.Now.AddDays(7));//7天后过期 } } }
 
 

二、用Redis 缓存修改少,查询多的数据。

       我一般会缓存一下几方面数据

       1、当前用户权限

       2、当前用户模块

       3、当前用户使用数据的数据结构。

       ....

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值