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. 2018.10.20 XMYZ Day1总结

    上周的忘写了……题目没有作者…… T1.backpack 期望得分100,实际得分100. 感觉我自己真是不如以前了……以前做这种题都是秒掉的,现在怎么想了10分钟啊…… 因为物品的体积和价值都非常小 ...

  2. 洛谷P2679 子串——DP

    题目:https://www.luogu.org/problemnew/show/P2679 DP水题: 然而被摆了一道,下面加 // 的地方都是一开始没写好的地方...还是不周密: 仔细审题啊... ...

  3. JAVA JVM 流程一

    JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的.Java虚拟机 ...

  4. 【转】浏览器中输入url后发生了什么

    原文地址:http://www.jianshu.com/p/c1dfc6caa520 在学习前端的过程中经常看到这样一个问题:当你在浏览器中输入url后发生了什么?下面是个人学习过程中的总结,供个人复 ...

  5. VScode相关

    这就是我想要的 VSCode 插件! VS Code 快捷键(中英文对照版) visual studio code 配置vue开发环境 vscode 这样的注释怎么生成? 能让你开发效率翻倍的 VSC ...

  6. 476. Number Complement(补数)

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  7. 【转载】CAS操作

    [本文转载]http://blog.csdn.net/hsuxu/article/details/9467651 CAS CAS:Compare and Swap, 翻译成比较并交换. java.ut ...

  8. Gym 100512B Betting Fast (题意+概率)

    题意:你开始有 s 元钱,然后你要在 t 场内赚到 n 元,每次赢的概率是 p,并且要越快越好. 析:当时没注意这个条件,要越快越好,然后写概率dp,怎么看也不像是对.其实是每次赌 min(s, n- ...

  9. Python机器学习算法 — 朴素贝叶斯算法(Naive Bayes)

    朴素贝叶斯算法 -- 简介 朴素贝叶斯法是基于贝叶斯定理与特征条件独立假设的分类方法.最为广泛的两种分类模型是决策树模型(Decision Tree Model)和朴素贝叶斯模型(Naive Baye ...

  10. [App Store Connect帮助]七、在 App Store 上发行(2.3)设定价格与销售范围:为您的 App 选择地区

    您可以选择希望您的 App 在 App Store 上可用的地区.默认情况下,所有地区都被选中,但您可以取消选中您不想销售您 App 的地区.新地区或已更改地区的 App Store 会在 24 小时 ...