.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 的缓存
using Liming.BaseSystem.Model;
using Liming.Cache;
using NServiceKit.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Liming.Web.Comm
{
public class RedisHelper
{
public static string Host = "127.0.0.1";
public const string COOKIE_KEY = "LoginInfoSessionKey";
public LoginUserInfo GetLoginUser(HttpRequestBase Request)
{
if (Request != null && Request.Cookies.Get(COOKIE_KEY) != null)
{
String SessionKeyValue = Request.Cookies.Get(COOKIE_KEY).Value;
using (RedisClient redisClient = new RedisClient(Host))
{
LoginUserInfo ulr = redisClient.Get<LoginUserInfo>(SessionKeyValue);
return ulr;
}
}
else
{
return null;
}
}
public LoginUserInfo GetLoginUser(string LoginInfoSessionKey)
{
if(LoginInfoSessionKey.Trim()=="")
{
return null;
}
using (RedisClient redisClient = new RedisClient(Host))
{
LoginUserInfo ulr = redisClient.Get<LoginUserInfo>(LoginInfoSessionKey.Trim());
return ulr;
}
}
public void LoginUserInfoRemove(HttpContextBase Context)
{
HttpCookie cookie = new HttpCookie(COOKIE_KEY,"");
Context.Response.Cookies.Set(cookie);
//Context.Response.Cookies.Remove(COOKIE_KEY);
}
public string RegistUser(HttpContextBase Context, LoginUserInfo ulr)
{
string SessionKeyValue = "";
HttpCookie cookie = new HttpCookie(COOKIE_KEY, SessionKeyValue);
if (Context.Request.Cookies.Get(COOKIE_KEY) != null)
{
SessionKeyValue = Context.Request.Cookies.Get(COOKIE_KEY).Value;
if (SessionKeyValue.Trim() == "")
{
SessionKeyValue = Guid.NewGuid().ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd"); ;
}
using (RedisClient redisClient = new RedisClient(Host))
{
ulr.LoginInfoSessionKey = SessionKeyValue;
cookie = new HttpCookie(COOKIE_KEY, SessionKeyValue);
redisClient.Set<LoginUserInfo>(SessionKeyValue, ulr, DateTime.Now.AddDays(7));//7天后过期
}
}
else
{
SessionKeyValue = Guid.NewGuid().ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd"); ;
using (RedisClient redisClient = new RedisClient(Host))
{
ulr.LoginInfoSessionKey = SessionKeyValue;
cookie = new HttpCookie(COOKIE_KEY, SessionKeyValue);
redisClient.Set<LoginUserInfo>(SessionKeyValue, ulr, DateTime.Now.AddDays(7));//7天后过期
}
}
Context.Response.Cookies.Add(cookie);
return SessionKeyValue;
}
/// <summary>
/// 保存登陆用户信息到redisServer
/// </summary>
/// <param name="ulr"></param>
public void SaveLoginUser(<pre name="code" class="csharp">public class LoginUserInfo
{
public string UserID {get;set;}
public string UserName{get;set;}
}
ulr) { using (RedisClient redisClient = new RedisClient(Host)) { redisClient.Set<LoginUserInfo>(ulr.LoginInfoSessionKey, ulr, DateTime.Now.AddDays(7));//7天后过期 } } }
二、用Redis 缓存修改少,查询多的数据。
我一般会缓存一下几方面数据
1、当前用户权限
2、当前用户模块
3、当前用户使用数据的数据结构。
....
本文介绍了如何使用Redis作为.NET应用程序的缓存解决方案,包括共享IIS集群的Session信息及缓存读多写少的数据。提供了具体的.NET代码示例。
 缓存技术&spm=1001.2101.3001.5002&articleId=50723327&d=1&t=3&u=335fe56726cb40a78365d5aa72fc9f5c)
260

被折叠的 条评论
为什么被折叠?



