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