Asp.net 静态值的Web服务缓存
有没有理由缓存这样一个返回非数据库所需响应的服务WebMethod?移动应用程序将使用此服务呼叫每天检索一次代码。假设服务器负载为100万用户。缓存当前设置为一小时Asp.net 静态值的Web服务缓存,asp.net,.net,vb.net,caching,iis,Asp.net,.net,Vb.net,Caching,Iis,有没有理由缓存这样一个返回非数据库所需响应的服务WebMethod?移动应用程序将使用此服务呼叫每天检索一次代码。假设服务器负载为100万用户。缓存当前设置为一小时 <WebMethod(CacheDuration:=3600)> _ Public Function GetIDs() As String Dim myArray(4) As Object myArray(0) = New With {Key .id = "one", .name = "
<WebMethod(CacheDuration:=3600)> _
Public Function GetIDs() As String
Dim myArray(4) As Object
myArray(0) = New With {Key .id = "one", .name = "1", .passcode = ""}
myArray(1) = New With {Key .id = "two", .name = "2", .passcode = ""}
myArray(2) = New With {Key .id = "three", .name = "3", .passcode = ""}
myArray(3) = New With {Key .id = "four", .name = "4", .passcode = ""}
myArray(4) = New With {Key .id = "five", .name = "5", .passcode = ""}
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Dim sJSON As String = js.Serialize(myArray)
Return sJSON
End Function
_
作为字符串的公共函数GetIDs()
Dim myArray(4)作为对象
myArray(0)=使用{Key.id=“one”、.name=“1”、.passcode=”“}新建
myArray(1)=使用{Key.id=“two”、.name=“2”、.passcode=”“}新建
myArray(2)=使用{Key.id=“three”、.name=“3”、.passcode=”“}新建
myArray(3)=新建,带有{Key.id=“four”、.name=“4”、.passcode=”“}
myArray(4)=使用{Key.id=“five”、.name=“5”、.passcode=”“}新建
Dim js As JavaScriptSerializer=新JavaScriptSerializer()
Dim sJSON As String=js.Serialize(myArray)
返回sJSON
端函数
是
我的直觉是肯定的。序列化需要花费cpu时间,缓存命中发生在pipleline的早期,因此在缓存这些请求时,您肯定会看到改进,即使它们没有命中数据库。将这样的数据存储在缓存中只会占用很少的内存,所以任何改进都已经足够了
我相信你不相信我的直觉,你也没有理由相信。我编写了一个小的测试webservice,其中包含您的测试方法(相同)和要比较的非缓存版本(相同,但针对CacheDuration属性)。我使用以下客户端来比较这两种方法的性能:
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
var tasks = new List<Task>();
stopwatch.Start();
for (var x = 0; x < 10; x++)
{
tasks.Add(Task.Factory.StartNew(() =>
{
using (var service = new localService.TestService())
{
for (var i = 0; i < 100; i++)
{
var ids = service.GetIDs();
}
}
}));
}
// completion order should be about the same as order in the list
// let's not worry too much
tasks.ForEach(d => d.Wait());
stopwatch.Stop();
var timeElapsed = stopwatch.ElapsedMilliseconds;
Console.WriteLine(timeElapsed);
}
static void Main(字符串[]args)
{
var stopwatch=新秒表();
var tasks=新列表();
秒表。开始();
对于(变量x=0;x<10;x++)
{
tasks.Add(Task.Factory.StartNew(()=>
{
使用(var service=new localService.TestService())
{
对于(变量i=0;i<100;i++)
{
var id=service.GetIDs();
}
}
}));
}
//完成顺序应与列表中的顺序大致相同
//我们不要太担心
tasks.ForEach(d=>d.Wait());
秒表;
var timeappeased=stopwatch.elapsedmillisons;
控制台写入线(时间流逝);
}
在我的旧笔记本电脑上的结果如下:
- 非缓存方法:8秒
- 缓存方法:1.5秒
另外:如果该方法没有命中数据库,我猜这些值会被编译到程序集中。在这种情况下,我认为没有理由将cacheDuration设置为仅一小时:将其设置为最大值。替换程序集时,应用程序池将进行回收,缓存仍需要重建。作为旁注:您正在双重序列化返回值。我理解您的意思,但我无法想出不这样做如何获得所需的输出。修复了双重序列化,以防有人使用代码