Author: Dflying Chen ( http://dflying.cnblogs.com/ )
For some complex services that take a long time to complete and do not have high real-time requirements, choosing Cache is often an effective way to improve efficiency. .NET's Web Service implementation fully considers the need for Cache. You only need simple settings to enable Cache. The call to Web Service in Atlas can also use this Cache mechanism to reduce unnecessary overhead on the server side.
To enable the Web Service's Cache, you only need to add the following attribute to the WebMethod declaration:
[WebMethod(CacheDuration = 5)]
where the value of CacheDuration represents the Cache time, in seconds.
But this Cache method is provided by Web Service, Atlas knows nothing about it, and each call is still sent to the server. Therefore, there is no way for this Cache to improve the impact of network latency on users.
Let us learn more about the Cache of Web Service through a sample program.
First write a Web Service, return the current time, and specify CacheDuration as 5 seconds:
[WebService(Namespace = " http://tempuri.org/ ")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CachedWebService : System.Web.Services.WebService
{
[WebMethod(CacheDuration = 5)]
public DateTime GetGurrentTime()
{
return DateTime.Now;
}
}
Then create an Atlas page, add ScriptManager and reference this Web Service in it:
<atlas:ScriptManager runat="server" ID="scriptManager">
<Services>
<atlas:ServiceReference Path="CachedWebService.asmx" />
</Services>
</atlas:ScriptManager>
Add some HTML tags to call this Web Service and display the results:
<input id="btnInvoke" type="button" value="Invoke" onclick="return btnInvoke_onclick()" />
<div id="result">
</div>
The last is the JavaScript part, where we display the results of each call on the page for easy analysis:
function btnInvoke_onclick() {
CachedWebService.GetGurrentTime(onCompleted);
}
function onCompleted(result) {
$('result').innerHTML += result + "<br />";
}
Let's test it in the browser. After clicking the button multiple times, the results are as follows. You can see the role of Cache:
At the same time, you can see in Fiddler that these requests are actually sent to the server, and Cache is just the server-side implementation:
The source code for this example can be downloaded here: