C# WebService 的缓存机制

 
[转]WebService的缓存机制
2008年02月19日 星期二 11:22

WebService的缓存分为两种,一种是简单的输出缓存,一种是强大的数据缓存

一、输出缓存
输出缓存的使用非常简单,比较适用于WebService的参数比较少,结果比较单一的情况,例如股票信息,可以设置5-10秒的缓存,天气预报,则可以设置30分钟甚至数小时的缓存

使用方法是:
在WebMethod属性上指定CacheDuration属性即可,例如



这样,600秒内这个WebService的所有输出数据都将从缓存中读取,不会真正做数据处理,如果事务代码是访问数据库的话,现在这种方法就会比每次都访问数据库快得多。这种缓存适合初接触WebService的新手使用。

[WebMethod(Description = “Test”,CacheDuration=600)]
public string Test()
{
return “Test”;
}

要注意的是,不是所有服务都适合使用这种缓存,例如每次结果都不一样的,访问数极高的服务,缓存将会变得非常大,占用很多服务器的内存,却没有实际效果。

二、数据缓存
想将你的WebService某些运行数据保存起来?如果不使用本地的数据库或者文件,那么缓存是最好的选择。这种缓存不同于上面提到的输出缓存,它需要编写代码来实现,但是相对应的,它的功能非常强大,可以存放任何类型的信息,并且你可以在任何时候检索它。

虽然也可以使用Application来模拟缓存,但是这需要你自己管理内存释放、用户并发问题,在.net时代已经被抛弃,WebService下的缓存使用Cache这个集合

 

using System.Web.Caching;
[WebMethod(Description = “Test”)]
public string Test()
{
string Content = “just4test”;

//创建数据缓存
Context.Cache.Insert(”Test”, Content, null, DateTime.MaxValue,TimeSpan.Zero, CacheItemPriority.NotRemovable, null);

string result = Context.Cache[”Test”].ToString();
return result;
}
}

在这里,我们使用了Context.Cache属性,Context.Cache.Insert方法用于将数据加入缓存。这个方法一共有4种重载,在这个例子中,我们使用的是功能最全面的重载版本,我们以此为例:每一个参数分别是键名(使用方法类似于Session),值,依赖性,绝对过期时间,可变过期时间,缓存优先级,缓存项目删除时的委托方法绝对过期时间是固定的,DataTime.MaxValue在这里表示永不过期;可变过期时间是一定时间内该缓存没有使用则自动失效,此处TimeSpan.Zero表示不使用可变过期。注意两者只能设置一项,如果要使用可变过期,绝对过期必须是DataTime.MaxValue,例如

Context.Cache.Insert("Test", Content, null, DateTime.MaxValue, TimeSpan.FromMinutes(10));

缓存优先级是Web服务器清理它的可能性,在此的CacheItemPriority.NotRemovable表示通常不从缓存中删除,可以理解为永久性缓存

通过依赖性,可以监视某个文件或者其他缓存的改动,如果有变化,则此缓存失效,这非常有实用价值。例如:

CacheDependency de = new CacheDependency(Server.MapPath("1.xml"));
Context.Cache.Insert("Test", Content, de, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);

这样,1.xml文件被删除或者更改的时候,缓存就会失效

三、实用举例
实际使用中,我使用这样一段代码,当远程的某个文件更新时,必须下载到本地,而缓存负责保存该文件的文件名和修改时间,每次客户端请求文件名和时间的时候,直接从缓存读取。每次定时下载程序(另有代码)启动的时候,getFiles()方法先检查是否有新文件(与本地缓存比对),然后决定是否下载。

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;
using TestOp;
using System.Web.Caching;
[WebService(Namespace = "Test")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Test: System.Web.Services.WebService
{
//提供下载和分析功能的后台代码类,TestOP
private TestOp testOp;

public Test()
{
testOp = newTestOp();
}

[WebMethod(Description = "下载文件")]
public string getFiles()
{
if (Context.Cache["FileName"] != null)
{
string FN=Context.Cache["FileName"].ToString();
testOp.GetHTML();
testOp.GetMatch();
if (FN.CompareTo(testOp.CheckFileName()) != 0)
{
try
{
testOp.Download();
Context.Cache.Insert("Time", testOp.CheckTime(), null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
Context.Cache.Insert("FileName", testOp.CheckFileName(), null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);

return "ok";
}
catch
{
return "Ex";
}
}
else
{
return "ok";
}
}
else
{
try
{
testOp.GetHTML();
testOp.GetMatch();
testOp.Download();
Context.Cache.Insert("Time", testOp.CheckTime(), null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
Context.Cache.Insert("FileName", testOp.CheckFileName(), null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);

return "ok";
}
catch
{
return "Ex";
}
}

}

[WebMethod(Description = "检查最新文件时间")]
public string CheckTime()
{
if (Context.Cache["Time"] ==null)
{
try
{
this.getFiles();
string result = Context.Cache["Time"].ToString();

DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
return result;
}
catch
{
return "Ex";
}
}
else
{
string result = Context.Cache["Time"].ToString();
return result;
}

}

[WebMethod(Description = "检查最新文件名")]
public string CheckFileName()
{
if (Context.Cache["FileName"] == null)
{
try
{
this.getFiles();

string result = Context.Cache["FileName"].ToString();
return result;
}
catch
{
return "Ex";
}
}
else
{
string result = Context.Cache["FileName"].ToString();
return result;
}
}

}

C# WebService 的缓存机制的更多相关文章

  1. WebService的缓存机制

    分类: .NET ASP 2010-11-13 10:00 849人阅读 评论(0) 收藏 举报 webservicestring数据库nullapplicationweb服务 WebService的 ...

  2. webservice 缓存机制

    本文转载:http://blog.csdn.net/zhdd1234/article/details/4555472 WebService的缓存分为两种,一种是简单的输出缓存,一种是强大的数据缓存 一 ...

  3. 【腾讯Bugly干货分享】彻底弄懂 Http 缓存机制 - 基于缓存策略三要素分解法

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/qOMO0LIdA47j3RjhbCWUEQ 作者:李 ...

  4. MyCat源码分析系列之——BufferPool与缓存机制

    更多MyCat源码分析,请戳MyCat源码分析系列 BufferPool MyCat的缓冲区采用的是java.nio.ByteBuffer,由BufferPool类统一管理,相关的设置在SystemC ...

  5. Java三大框架之——Hibernate中的三种数据持久状态和缓存机制

    Hibernate中的三种状态   瞬时状态:刚创建的对象还没有被Session持久化.缓存中不存在这个对象的数据并且数据库中没有这个对象对应的数据为瞬时状态这个时候是没有OID. 持久状态:对象经过 ...

  6. Spring缓存机制的理解

    在spring缓存机制中,包括了两个方面的缓存操作:1.缓存某个方法返回的结果:2.在某个方法执行前或后清空缓存. 下面写两个类来模拟Spring的缓存机制: package com.sin90lzc ...

  7. hibernate缓存机制(转)

    原文出处:http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html 一.why(为什么要用Hibernate缓存?) Hibernate是 ...

  8. [转]Android ListView 与 RecyclerView 对比浅析—缓存机制

    从源码角度剖析ListView 与 RecyclerView 缓存机制的不同 https://zhuanlan.zhihu.com/p/23339185 原文地址:http://dev.qq.com/ ...

  9. Atitit webservice的发现机制 discover机制

    Atitit webservice的发现机制 discover机制 1.1. Ws disconvert 的组播地址和端口就是37021 1.2. Ws disconvert的发现机制建立在udp组播 ...

随机推荐

  1. CF上的3道小题(2)

    CF上的3道小题(2) T1:CF630K Indivisibility 题意:给出一个数n,求1到n的数中不能被2到9中任意一个数整除的数. 分析:容斥一下,没了. 代码: #include < ...

  2. 01_创建一个新的activity&activity配置清单文件

    今天开始学四大组件.今天是学Activity,然后是广播接收者,然后是服务,然后是内容提供者.四大组件,咱们一天一个.Activity就是跟用户交互的界面,大部分的应用都不会只有这么一个界面.创建多个 ...

  3. Maven的-pl -am -amd参数学习

    昨天maven的deploy任务需要只选择单个模块并且把它依赖的模块一起打包,第一时间便想到了-pl参数,然后就开始处理,但是因为之前只看了一下命令的介绍,竟然花了近半小时才完全跑通,故记录此文. 假 ...

  4. Android buffer_handle_t的定义(转载)

    转自:http://www.cnblogs.com/eustoma/archive/2012/06/08/2541416.html 1. buffer_handle_t 文件位置:system\cor ...

  5. Hibernate对集合属性的操作---基础学习

    1:Set集合属性操作 1).Hibernate3以后支持大部分重要的JDK集合接口映射,Set集合接口的配置:  >在xxx.hbm.xml文件中使用<set>标签 2).< ...

  6. SpringBoot项目docker化

    前言 有很多种方案构建Docker镜像,包括Dockerfile构建.maven插件构建,这里我使用了最简单的Dockerfile构建的. 一.安装Docker 我的虚拟机系统是CentOS7,需要是 ...

  7. python_os.path模块用法

    python中os.path模块用法: dirname()  用于去掉文件名,返回目录所在的路径 >>> import os >>> os.path.dirname ...

  8. HDU 5558 后缀数组

    思路: 这是一个错误的思路, 因为数据水才过= = 首先求出来后缀数组 把rank插到set里 每回差i两边离i近的rank值,更新 如果LCP相同,暴力左(右)继续更新sa的最小值 //By Sir ...

  9. 2017 JUST Programming Contest 3.0 D. Dice Game

    D. Dice Game time limit per test 1.0 s memory limit per test 256 MB input standard input output stan ...

  10. uwp选取文件夹并读取其中的图片

    uwp对文件的操作和wpf,winform等等有很大的不同,主要原因是uwp对权限的要求比较严格,不能想从前那样随心所欲的读取文件. 1.首先找到Package.appxmanifest这个文件,在功 ...