正 文

用.NET Caching Application Block简化数据缓冲


www.7dspace.com  更新日期:2005-11-28 2:52:44  七度空间


使用CAB

CAB能够被用作最简单的状态管理方案。例如最常见的小块数据缓冲在于Web服务中。因为操作要通过网络,因此将结果保存一小段时间比重复地通过网络调用一个方法要好得多。我们来看看一个温度服务的例子。

我们有一个程序提供多个城市的气温。我们想将最常使用的城市的气温值缓冲起来,这要比每次有请求都去调用CapeScience GlobalWeather服务要好。

该温度服务有一个名为temperature的私有方法,它接收airportCode参数并返回相应的温度:

private string temperature(string airportCode){

//获得缓冲对象

CacheManager cacheManager=CacheManager.GetCacheManager();

//检查该值是否已经在缓冲中

//我们将aipporCodet作为关键字

object weather=cacheManager.GetData(airportCode);

if(weather==null){

//不在缓冲区,需要获取

//获取weather对象

com.capescience.live.GlobalWeather globalWeather=new com.capescience.live.GlobalWeather();

com.capescience.live.WeatherReport weatherReport=new com.capescience.live.WeatherReport();

//获取文本框中信息

weatherReport globalWeather.getWeatherReport(aipportCode);

//从weatherReport对象获取温度

temperature=weatherReport.temperature.ambient.ToString();

//下面准备存储它

//将airportCode作为关键字

//就如同上面检查时一样

cacheManager.Add(airportCode,temperature);

}else{

//缓冲操作的结果是一个对象

//因此需要将它转换为一个字符串

temperature=weather.ToString();

}

//返回温度

return temperature;

}

上面的代码架设缓冲中存有该值,但是如果返回为空则进行新的查询。存储和获取缓冲数据就如同使用ASP.NET的ViewState对象一样简单。SqlServerCacheStorage类引用了Data Application Block的数据库功能。

3页,页码:[1] [2] [3] 

上一篇:vb:建立自定义属性以提供程序信息
下一篇:用集合程序块来简化数据处理
作者:  来源:开发者在线 ( 责任编辑:7dspace )
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐