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

本文介绍了如何使用Redis作为.NET应用程序的缓存解决方案,包括共享IIS集群的Session信息及缓存读多写少的数据。提供了具体的.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 的缓存

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、当前用户使用数据的数据结构。

       ....

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值