项目经验之:分布式缓存HttpRuntime.cache应用到单点登陆中_优化登陆
以前的设计方案,是我们在数据库中放一个表,用作存储验证登陆成功的用户,并且生成用
户TOKEN(令牌)
分布式缓存+集群的解决方案图:
大概就是这样设计的。。。四台服务器。
我们将存储登陆成功的用户表从数据库中分离出来,放到缓存中。并利用集群建立四台服务器缓存同步更新。。。。
在登陆时,我们读了缓存中的Token,同时遍历IP集群中配置的服务器地址。同步四台服务器之间的Token缓存。。。。。
相应的代码:
DE层中配置文件:

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();
/// <summary>
/// 发送用于同步集群中的Cache数据
/// </summary>
/// <param name="param">Cache数据</param>
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;
}
}
}
/// <summary>
/// 用于同步集群中的Cache数据
/// </summary>
/// <param name="param">Cache数据</param>
/// <param name="operation">Cache操作类型</param>
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;
}
}
}
/// <summary>
/// Cache同步操作类型
/// </summary>
public enum CacheOperation
{
Add,
Edit,
Delete
}
}
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();
/// <summary>
/// 发送用于同步集群中的Cache数据
/// </summary>
/// <param name="param">Cache数据</param>
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;
}
}
}
/// <summary>
/// 用于同步集群中的Cache数据
/// </summary>
/// <param name="param">Cache数据</param>
/// <param name="operation">Cache操作类型</param>
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;
}
}
}
/// <summary>
/// Cache同步操作类型
/// </summary>
public enum CacheOperation
{
Add,
Edit,
Delete
}
}
CacheFunc.cs 缓存操作函数

青华木园
标签:
asp.net cache
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库