加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

[WCF REST] 通过ASP.NET Output Caching实现声明式缓存

发布时间:2020-12-16 09:05:41 所属栏目:asp.Net 来源:网络整理
导读:ASP.NET的输出缓存(Output Caching)机制允许我们针对整个Web页面或者页面的某个部分(主要针对用户控件)最终呈现的HTML进行缓存。对于后续针对相同资源的请求,只需要直接将缓存的HTML予以回复而无须按照页面处理生命周期对每次请求进行重复处理。WCF通过

ASP.NET的输出缓存(Output Caching)机制允许我们针对整个Web页面或者页面的某个部分(主要针对用户控件)最终呈现的HTML进行缓存。对于后续针对相同资源的请求,只需要直接将缓存的HTML予以回复而无须按照页面处理生命周期对每次请求进行重复处理。WCF通过操作行为AspNetCacheProfileAttribute利用ASP.NET的输出缓存提供一种针对于某个操作的声明式缓存机制。[源代码从这里下载]

一、AspNetCacheProfileAttribute

WCF对ASP.NET缓存的支持是通过AspNetCacheProfileAttribute特性来实现的。通过如下的代码我们不难看出AspNetCacheProfileAttribute是实现了IOperationBehavior接口的操作行为,我们可以直接将其应用到契约接口/类中的某个具有缓存需要的操作方法上。

   1: [AttributeUsage(AttributeTargets.Method)]
   3: {
   5:     public AspNetCacheProfileAttribute(string cacheProfileName);
   7: }

AspNetCacheProfileAttribute构造函数参数cacheProfileName表示的CacheProfile的配置名称,目标操作按照定义在相应CacheProfile的缓存策略实施缓存。CacheProfile配置在<system.web>/<caching>/<outputCacheSettings>/<outputCacheProfiles>节点下。

   2:   connectionStrings>
   4:          connectionString="Server=.; Database=TestDb; Uid=sa; Pwd=password"
   6:   </   7:   system.web   8:     caching   9:       outputCacheSettings  10:         outputCacheProfiles  11:           ="default" 
  13:                varyByParam="none" 
  15:           16:         17:       sqlCacheDependency  18:         databases  19:           ="TestDb" connectionStringName="localDb"  20:           21:         22:       23:     24: >

在如上所示的配置片断中,我们定义了一个名称为default的CacheProfile。代表缓存时间的duration属性被设置为60,意味着缓存项在被存储之后1分钟之后实失效;属性varyByParam被设置为none表示缓存项与请求的查询字符串无关。此外,该CacheProfile还设置针对某个本地数据库中的TestTable表的SQL依赖(SQL Dependency)。关于CacheProfile的配置属于ASP.NET的范畴,在这里我们不会作过多的讨论。

既然是采用ASP.NET输出缓存,WCF服务自然需要采用IIS寄宿并采用ASP.NET 兼容模式。值得一提的是,基于AspNetCacheProfileAttribute的输出缓存仅仅针对HTTP-GET

二、实例演示:创建采用输出缓存的服务

接下来我们通过一个简单的实例来演示如何通过操作行为对某个操作的返回值实施缓存,为此我们创建一个用于返回当前时间的服务。如下所示的是作为服务契约的ITime接口的定义,AspNetCacheProfileAttribute特性被应用到了用于返回当前时间的操作方法GetCurrentTime上。

using System.ServiceModel;
   4: namespace Artech.WcfServices.Service.Interface
   6:     [ServiceContract(Namespace = "http://www.artech.com/")]
   8:     {
  10:         [AspNetCacheProfile("default")]
  12:     }
using System.ServiceModel.Activation;
namespace Artech.WcfServices.Service
   7:????? RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
   9:     {
  11:         {
  13:         }
  15: }

在一个Web项目中我们为通过IIS寄宿的服务TimeService添加一个对应的.svc文件(TimeService.svc),如下所示的是<%@ServiceHost%>指令的定义。表示ServiceHostFactory类型的指令属性Factory被设置为System.ServiceModel.Activation.WebServiceHostFactory.

2: system.serviceModel 3: services 4: service ="Artech.WcfServices.Service.TimeService" 5: endpoint binding="webHttpBinding"
  11:     12:       13:         14:           15:           ="default" ="60" ="none"  16:         >      
using (ChannelFactory<ITime> channelFactory =  new ChannelFactory<ITime>("timeService"))
   3:     ITime proxy = channelFactory.CreateChannel();
   5:     { 
   7:         Thread.Sleep(1000);
   9: }

客户端代码执行之后会在控制台上输出如下的结果。由于服务端通过ASP.NET的输出缓存对第一次执行GetCurrentTime操作的结果进行了缓存,所以客户端返回的时间都是相同的。