以前的设计方案,是我们在数据库中放一个表,用作存储验证登陆成功的用户,并且生成用户TOKEN(令牌)

分布式缓存+集群的解决方案图:

相应的代码:

DE层中配置文件:

receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">

maxBytesPerRead="4096" maxNameTableCharCount="16384" />

realm="" />

binding="basicHttpBinding" bindingConfiguration="CacheServiceSoap"
contract="CacheService.CacheServiceSoap" name="CacheServiceSoap" />

CacheBase.cs  缓存基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security;
using System.Web.Caching;
using System.Web;
using System.ServiceModel;
using System.Reflection;
using HttpRuntimeCacheDE.CacheService;

namespace HttpRuntimeCacheDE.Cache
{
public class CacheBase
{
private CacheServiceSoapClient client = null;
private CacheService.LoginStatusParam cParam = new CacheService.LoginStatusParam();
private CacheService.CacheOperation cOperation = new CacheService.CacheOperation();
///


/// 发送用于同步集群中的Cache数据
///

/// Cache数据
public void SendCacheData(CacheParam param, CacheOperation operation)
{

string[] ips = CacheConfig.ClusterGroupAddr;
foreach (string ip in ips)
{
try
{
client = new CacheService.CacheServiceSoapClient();
EndpointAddress address = new EndpointAddress("http://" + ip + @"/" + CacheConfig.WebSiteName + "/CacheService.asmx");

client.Endpoint.Address = address;

RemoteParamConvert(cParam, param);

switch (operation)
{
case CacheOperation.Add:
cOperation = CacheService.CacheOperation.Add;
break;
case CacheOperation.Edit:
cOperation = CacheService.CacheOperation.Edit;
break;
case CacheOperation.Delete:
cOperation = CacheService.CacheOperation.Delete;
break;
default:
break;
}

client.GetCacheData(cParam, cOperation);
}
catch
{
continue;
}
}

}
///
/// 用于同步集群中的Cache数据
///
/// Cache数据
/// Cache操作类型
public void SyncCacheData(CacheParam param, CacheOperation operation)
{
switch (operation)
{
case CacheOperation.Add:
AddCache(param);
break;
case CacheOperation.Edit:
EditCache(param);
break;
case CacheOperation.Delete:
DeleteCache(param);
break;
default:
break;
}
}
// 增加Cache数据
protected virtual void AddCache(CacheParam param)
{
string key = BuildCacheKey(param);
HttpRuntime.Cache.Add(key, param, null, DateTime.Now.AddHours(1),
System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High,
null);
}
// 修改Cache数据
protected virtual void EditCache(CacheParam param)
{
string key = BuildCacheKey(param);
HttpRuntime.Cache.Remove(key);
AddCache(param);
}

// 删除Cache数据
protected virtual void DeleteCache(CacheParam param)
{
string key = BuildCacheKey(param);
HttpRuntime.Cache.Remove(key);
}

// 生成在线的Cache Key
protected virtual string BuildCacheKey(CacheParam param)
{
return "";
}
// 将本地参数转换成远程调用的参数
private void RemoteParamConvert(object sourceObj, object targetObj)
{
try
{
PropertyInfo[] sourceInfo = sourceObj.GetType().GetProperties();
PropertyInfo[] targetInfo = targetObj.GetType().GetProperties();

for (int i = 0; i < sourceInfo.Length; i++)
{
if (sourceInfo[i].Name == targetInfo[i].Name)
{
object targetValue = targetInfo[i].GetValue(targetObj, null);

if (!ParamFunc.Judgement(targetValue))
continue;

sourceInfo[i].SetValue(sourceObj, targetValue, null);
}
}
}
catch (Exception ex)
{
throw ex;
}
}

}

///
/// Cache同步操作类型
///
public enum CacheOperation
{
Add,
Edit,
Delete
}
}

CacheFunc.cs 缓存操作函数

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace HttpRuntimeCacheDE.Cache
{
public class CacheFunc:CacheBase
{
protected override string BuildCacheKey(CacheParam param)
{
LoginStatusParam lsparam = param as LoginStatusParam;
return param.GetType().Name.ToString() + "&" + lsparam.SysLoginStatusID + "&" + lsparam.LoginName;
}
///


/// 在Cache中查询在线用户
///

/// 在线用户参数
public List QueryLoginStatus(LoginStatusParam param)
{
List result = new List();

List plist = ConvertCacheItem();

var loginResult = from c in plist
where 1 == 1
&& (!Judgement(param.SysLoginStatusID) ? true : c.SysLoginStatusID == param.SysLoginStatusID)
&& (!Judgement(param.SysOrganizationID) ? true : c.SysOrganizationID == param.SysOrganizationID)
&& (!Judgement(param.SysServiceCenterID) ? true : c.SysServiceCenterID == param.SysServiceCenterID)
&& (!Judgement(param.LoginName) ? true : c.LoginName == param.LoginName)
&& (!Judgement(param.LoginTime) ? true : c.LoginTime == param.LoginTime)
&& (!Judgement(param.LastHandleTime) ? true : c.LastHandleTime == param.LastHandleTime)
&& (!Judgement(param.FullName) ? true : c.FullName == param.FullName)
&& (!Judgement(param.LoginToken) ? true : c.LoginToken == param.LoginToken)
select c;

result = loginResult.ToList();

return result;
}

// 将Cache中的项转换成List
private List ConvertCacheItem()
{
List plist = new List();

IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();

while (CacheEnum.MoveNext())
{
if (CacheEnum.Value is LoginStatusParam)
plist.Add(CacheEnum.Value as LoginStatusParam);
}

return plist;
}
public string SysUserLogin(LoginStatusParam param)
{
AddLoginStatus(param);
param = QueryLoginStatus(param).First();

return param.LoginToken;
}

#region AddMethod
///
/// 在Cache中增加在线用户
///
/// 在线用户参数
public void AddLoginStatus(LoginStatusParam param)
{
Random random = new Random();
param.SysLoginStatusID = random.Next().ToString();
SyncCacheData(param, CacheOperation.Add);

SendCacheData(param, CacheOperation.Add);
}
#endregion
private bool Judgement(object obj)
{
try
{
if (obj != null && obj != DBNull.Value)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}

分布式缓存HttpRuntime.cache应用到单点登陆中_优化登陆的更多相关文章

  1. 服务端缓存HttpRuntime.Cache的使用

    HttpRuntime.Cache.Insert("缓存key", "缓存content", null, DateTime.Now.AddMinutes(3), ...

  2. Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache

    两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...

  3. Flink分布式缓存Distributed Cache

    1 分布式缓存 Flink提供了一个分布式缓存,类似于hadoop,可以使用户在并行函数中很方便的读取本地文件,并把它放在taskmanager节点中,防止task重复拉取. 此缓存的工作机制如下:程 ...

  4. 分布式缓存(Cache)

    1. 单层分布式cache. 如memcache. 2. 多层分布式cache. 服务端和调用者本地都存放cache, 使用udp组播解决cache同步更新问题,但不可靠. 3. 改进的多层分布式ca ...

  5. SharePoint 2013混合模式登陆中 使用 自定义登陆页

    接前一篇博客<SharePoint 2013自定义Providers在基于表单的身份验证(Forms-Based-Authentication)中的应用>,当实现混合模式登陆后,接着我们就 ...

  6. [.NET领域驱动设计实战系列]专题八:DDD案例:网上书店分布式消息队列和分布式缓存的实现

    一.引言 在上一专题中,商家发货和用户确认收货功能引入了消息队列来实现的,引入消息队列的好处可以保证消息的顺序处理,并且具有良好的可扩展性.但是上一专题消息队列是基于内存中队列对象来实现,这样实现有一 ...

  7. 5个强大的Java分布式缓存框架推荐

    在开发中大型Java软件项目时,很多Java架构师都会遇到数据库读写瓶颈,如果你在系统架构时并没有将缓存策略考虑进去,或者并没有选择更优的 缓存策略,那么到时候重构起来将会是一个噩梦.本文主要是分享了 ...

  8. 第八章 企业项目开发--分布式缓存memcached

    注意:本节代码基于<第七章 企业项目开发--本地缓存guava cache> 1.本地缓存的问题 本地缓存速度一开始高于分布式缓存,但是随着其缓存数量的增加,所占内存越来越大,系统运行内存 ...

  9. j2ee分布式缓存同步实现方案dlcache v1.0.0

    现成的分布式K/V缓存已经有很多的实现,最主要的比如redis,memcached,couchbase.那为什么我们还要自己去实现呢,在我们解决了分布式系统下大量rpc调用导致的高延时后,我们发现很多 ...

随机推荐

  1. KnockOut文档--模板绑定

    目的 模板绑定使用数据render模板,然后把渲染的结果填充到Dom树中.模板通过重复或嵌套块(通常为您的视图模型数据的函数)用一种简单,方便的方式来建立复杂的UI结构 . 有两种方式使用模板: Na ...

  2. Archives for the category: Fisheye/Crucible

    Archives for the category: Fisheye/Crucible Introducing FishEye and Crucible 3.0 – Search, visualize ...

  3. Chrome插件i18n多语言实现

    i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.Chrome插件框架中i18n的封装API: chrome.i18n.ge ...

  4. 嵌入式Linux学习(二)

    嵌入式系统和通用计算机的主要区别 嵌入式系统是指以应用为中心,以计算机技术为基础,软件硬件可裁剪,适应应用系统对功能.可靠性.成本.体积.功耗严格要求的专用计算机系统. 嵌入式系统主要由嵌入式微处理器 ...

  5. the selected server is enabled,but is not configured properly.Deployment to it will not be permitted

    用Tomcat添加部署项目的时候报错: the selected server is enabled,but is not configured properly.Deployment to it w ...

  6. Eclipse相关集锦

    开场白,之前的个人博客写过很多细小的Eclipse的东西,这里将搬过来,作为整体一篇. 1.Eclipse提示失效 解决:window->Preferences->Java->Edi ...

  7. 密码配置配置SSH免密码登陆

    在本文中,我们主要介绍密码配置的内容,自我感觉有个不错的建议和大家分享下 我的用户名是master 1.安装ssh(若没安装的话) sudo apt-get install ssh 2.配置为可以免密 ...

  8. Js-Html 前端系列--页面撑开头尾

    今天学习过程中,发现一个超实用的方法,就是当页面有尾部,但是内容又不多的情况下,让中间的内容把尾部撑到底部. <script type="text/javascript"&g ...

  9. Python小问题汇总

    现在的时间适合写点最近的小总结,这中间涉及到python/git等问题,我就从python先说起吧. 一.Python 1. Python的异常处理 因为想到自己不断尝试写小程序的话会用到抛出异常信息 ...

  10. 函数四种调用模式以及其中的this指向

    第一种:函数直接执行模式 function add(a,b){ console.log(this); return a+b; } add(10,20)//this===window 第二种:对象方法的 ...