C# 代理应用 - Cachable
C# 代理应用 - Cachable
放心,这次不是说设计模式中的代理模式,说的是C#的RealProxy的用法,主要用于:通过给class贴标签,让class做更多的工作,比如判断是否存在缓存,有则直接返回缓存object,没有则保存为缓存,等待下次请求是可以更快的获取数据(当然这只是其中一种常用用途,MVC的Action就是采用这种方式)
下面是序列图:

.Net Object Generation interceptor属于.NET自身行为,不需要额外写代码。
Code Consumer指想调用RealObject来进行调用的对象,比如控制台程序,或者WEB程序。
ProxyAttribute里定义了具体代理类是哪个,这个代理类是自己 继承RealProxy写的一个代理类,这个类中需要加入前置、后置、环绕等方法(具体根据需求)
下面我们来看具体如何在.Net中实现:

public class FollowAction
{
public bool StopExecute { get; set; } //用于在前置方法中,指示是否停止执行真正的方法体,比如已经找到了cache value,因此不需要继续运行方法体的情况
public object Result { get; set; } //保存cache value的变量
} public abstract class CachableRealProxy : RealProxy
{
private MarshalByRefObject target; public MyAOPRealProxy(Type objType, MarshalByRefObject obj)
: base(objType)
{
target = obj;
} public override IMessage Invoke(IMessage msg)
{
IMessage retMsg = null;
IMethodCallMessage methodCall = (IMethodCallMessage)msg;
IMethodReturnMessage methodReturn = null;
object[] copiedArgs = Array.CreateInstance(typeof(object), methodCall.Args.Length) as object[];
methodCall.Args.CopyTo(copiedArgs, 0);
if (msg is IConstructionCallMessage)
{ IConstructionCallMessage ccm = (IConstructionCallMessage)msg;
RemotingServices.GetRealProxy(target).InitializeServerObject(ccm);
ObjRef oRef = RemotingServices.Marshal(target);
RemotingServices.Unmarshal(oRef);
retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage(ccm, (MarshalByRefObject)this.GetTransparentProxy()); }
else
{
bool aopAttributeExists = false;
object[] attrs = methodCall.MethodBase.GetCustomAttributes(false);
if (attrs != null && attrs.Count() > 0)
{
foreach(object o in attrs)
{
CachableAttribute attr = o as CachableAttribute;
if (attr != null)
{
aopAttributeExists = true;
break;
}
}
}
FollowAction follow=null;
if (aopAttributeExists)
follow = this.PreProcess(msg); try
{
object returnValue = null;
if (follow != null && follow.StopExecute)
returnValue = follow.Result;
else
returnValue = methodCall.MethodBase.Invoke(this.target, copiedArgs);
methodReturn = new ReturnMessage(returnValue, copiedArgs, copiedArgs.Length, methodCall.LogicalCallContext, methodCall); if (follow == null || !follow.StopExecute)
if (aopAttributeExists)
this.PostProcess(msg, methodReturn);
}
catch (Exception ex)
{
if (null != ex.InnerException)
{
methodReturn = new ReturnMessage(ex.InnerException, methodCall);
}
else
{
methodReturn = new ReturnMessage(ex, methodCall);
}
}
retMsg = methodReturn; }
return retMsg;
} public override FollowAction PreProcess(System.Runtime.Remoting.Messaging.IMessage requestMsg) //处理前置方法
{
bool cacheDefinationTagExists = true;
CachableAttribute cacheDefine = CheckCacheDefinationTag(requestMsg, ref cacheDefinationTagExists);
if (!cacheDefinationTagExists)
return null; string cacheKey = cacheDefine.GenerateCacheKey();
object o=CacheManager.Instance().GetCacheCore().Get(cacheDefine.Location, cacheKey);
if (o != null)
{
FollowAction follow = new FollowAction();
follow.Result = o;
follow.StopExecute = true;
return follow;
} return null;
} public override void PostProcess(System.Runtime.Remoting.Messaging.IMessage requestMsg, System.Runtime.Remoting.Messaging.IMessage responseMsg)//处理后置方法
{
bool cacheDefinationTagExists = true;
CachableAttribute cacheDefine = CheckCacheDefinationTag(requestMsg, ref cacheDefinationTagExists);
if (!cacheDefinationTagExists)
return; ReturnMessage returnMsg = (ReturnMessage)responseMsg; string cacheKey = cacheDefine.GenerateCacheKey();
CacheManager.Instance().GetCacheCore().Set(cacheDefine.Location, cacheKey, returnMsg.ReturnValue);
}
private static CachableAttribute CheckCacheDefinationTag(System.Runtime.Remoting.Messaging.IMessage requestMsg, ref bool cacheDefinationTagExists)//Help函数
{
IMethodCallMessage methodCall = (IMethodCallMessage)requestMsg;
object[] attrs = methodCall.MethodBase.GetCustomAttributes(typeof(CachableAttribute), false);
if (attrs == null || attrs.Count() <= 0)
cacheDefinationTagExists = false;
CachableAttribute cacheDefine = attrs[0] as CachableAttribute;
if (cacheDefine == null)
cacheDefinationTagExists = false;
return cacheDefine;
}
}

还需要2个Attribute: 如上代码中用到的CachableAttribute和CachableEnabledAttribute
CachableAttribute用来贴在各个函数签名上,可以指定cache的key等信息(继承自普通的Attribute)
CachableEnabledAttribute用来关联 自定义proxy以及需要被代理的class的,用法是贴在被代理的class签名上(继承自ProxyAttribute)
代码如下:

public class CachableAttribute : Attribute
{
public string Location { get; set; }
public string Key { get; set; }
public CacheAction CacheAction { get; set; }
public string KeyPath { get; set; }
public object CacheObject { get; set; }
public TimeSpan ExpireTimeSpan { get; set; }
public DateTime AbsoluteExpireTime { get; set; } public string GenerateCacheKey()
{
//will be changed
return string.Format("{0}", this.Key);
}
}


public class CachableEnabledAttribute : ProxyAttribute
{
public override MarshalByRefObject CreateInstance(Type serverType)
{
MarshalByRefObject target = base.CreateInstance(serverType);
CachableRealProxy rp = new CachableRealProxy(serverType, target);
MarshalByRefObject obj = (MarshalByRefObject)rp.GetTransparentProxy();
return obj;
}
}

被代理的class需要继承自ContextBoundObject

[CachableEnabled()]
public class RealObject : ContextBoundObject
{
[Cachable(Location = "OrdersQuery", CacheAction = CacheAction.Read, Key = "OrderQueryService_QueryByFirstName_{0}")]
public override QueryResult<QueryDto.OrderDto> QueryByFirstName(string firstName, PagingInfo pgInfo)
{
QueryResult<QueryDto.OrderDto> lst = new QueryResult<QueryDto.OrderDto>();
lst.List = new List<QueryDto.OrderDto>();
lst.TotalCount = 1000;
for (int i = 0; i < 1;i++ )
{
OrderDto o = new OrderDto();
o.BuyWhat = string.Format("Buywhat {0}", DateTime.Now.ToString());
o.FirstName = string.Format("FirstName {0}", DateTime.Now.ToString());
o.LastName = string.Format("LastName {0}", DateTime.Now.ToString());
o.Email = string.Format("Email {0}", DateTime.Now.ToString());
o.OrderID = Guid.NewGuid();
lst.List.Add(o);
}
return lst;
}
}

看下控制台测试代码:

RealObject srv1 = new RealObject();
while (true)
{
Thread.Sleep(1000); //每隔1s执行调用,看看返回的时间是否相同,相同则代表已经是cachable的了
QueryResult<OrderDto> lst = srv1.QueryByFirstName("aaron", new CoreFramework.QueryService.PagingInfo() { PageIndex = 0, PageSize = 10, OrderByColumn = "FirstName", IsAscendingSort = true });
foreach (OrderDto order in lst.List)
{
string msg = string.Format("{0}-{1}-{2}-{3}", order.OrderID, order.FirstName, order.LastName, order.BuyWhat);
Console.WriteLine(msg);
}
}

运行效果图:

搞定。
心怀远大理想。
为了家庭幸福而努力。
用A2D科技,服务社会。
C# 代理应用 - Cachable的更多相关文章
- 缓存子系统如何设计(Cachable tag, Memcache/redis support, xml config support, LRU/LFU/本地缓存命中率)
大家对这段代码肯定很熟悉吧: public List<UserInfo> SearchUsers(string userName) { string cacheKey=string.For ...
- Linux实战教学笔记43:squid代理与缓存实践(二)
第6章 squid代理模式案例 6.1 squid传统正向代理生产使用案例 6.1.1 squid传统正向代理两种方案 (1)普通代理服务器 作为代理服务器,这是SQUID的最基本功能:通过在squi ...
- 【原】谈谈对Objective-C中代理模式的误解
[原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...
- nginx配置反向代理或跳转出现400问题处理记录
午休完上班后,同事说测试站点访问接口出现400 Bad Request Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...
- Visual Studio Code 代理设置
Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...
- DynamicObject - 代理对象的种类
开箱即用,DynamicProxy提供了多种代理对象,主要分成两个大类: 基于继承(Inheritance-based) 基于继承的代理是通过继承一个代理类来实现,代理拦截对类的虚(virtual)成 ...
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#signalR 后台创建了一个DntHub的集线器 前台在调用的时候出现了问题(经检查是代理对象 ...
- 实现代理设置proxy
用户在哪些情况下是需要设置网络代理呢? 1. 内网上不了外网,需要连接能上外网的内网电脑做代理,就能上外网:多个电脑共享上外网,就要用代理: 2.有些网页被封,通过国外的代理就能看到这被封的网站:3. ...
- 23种设计模式--代理模式-Proxy
一.代理模式的介绍 代理模式我们脑袋里出现第一个词语就是代购,其实就是这样通过一个中间层这个中间成是属于什么都干什么都买得,俗称"百晓生",在平时得开发中我们经常会听到 ...
随机推荐
- CSS3火焰文字特效制作教程
原文:CSS3火焰文字特效制作教程 用一句很俗气的话概括这两天的情况就是:“最近很忙”,虽然手头上有不少很酷的HTML5和CSS3资源,但确实没时间将它们的实现过程写成教程分享给大家.今天刚完成了一个 ...
- SQL2005性能分析一些细节功能你是否有用到?
原文:SQL2005性能分析一些细节功能你是否有用到? 我相信很多朋友对现在越来越大的数据量而感到苦恼,可是总要面对现实啊,包括本人在内的数据库菜鸟们在开发B/S程序时,往往只会关心自己的数据是否正确 ...
- CRUD功能的JqGrid表格
CRUD功能的JqGrid表格 之前的项目也曾用过JgGrid对它的基本功能也是略有了解,网上有个国外的开源的项目,但是不适合个人的风格,所以花了3天空余的时间封装了下JqGrid,也算是参加开发工作 ...
- installshield 32位打包和64位打包的注意事项
原文:installshield 32位打包和64位打包的注意事项 32/64位问题要把握几点:1. 明确你的产品是否需要区分32/64位2. 明确你的产品中是否有32/64位的服务注册3. 了解In ...
- SQL点滴35—SQL语句中的exists
原文:SQL点滴35-SQL语句中的exists 比如在Northwind数据库中有一个查询为 SELECT c.CustomerId,CompanyName FROM Customers c WHE ...
- jquery validate remote验证唯一性
jquery.validate.js 的 remote 后台验证 之前已经有一篇关于jquery.validate.js验证的文章,还不太理解的可以先看看:jQuery Validate 表单验证(这 ...
- 编写高效的jQuery代码
http://www.css88.com/jqapi-1.9/ 编写高效的jQuery代码 最近写了很多的js,虽然效果都实现了,但是总感觉自己写的js在性能上还能有很大的提升.本文我计划总结一些网上 ...
- 反射导出excel案例
1.代码案例: protected void btnExportExcel_Click(object sender, EventArgs e) { SetSearchValue(); Dictiona ...
- TeamCity vs Jenkins: Which is the Better Continuous Integration (CI) Server for .NET Software Development?
原文:http://www.excella.com/insights/teamcity-vs-jenkins-better-continuous-integration-server So, you’ ...
- C#事件与委托的区别
C#事件与委托的区别 1. 委托 事件是利用委托来定义的,因此先解释委托.委托是一个类,它与其他类如int,string等没有本质区别,int代表的是所有的整形,而string代表的是字符串,委托则代 ...