WebService Cache Mechanism

Source: Internet
Author: User

From: http://blog.csdn.net/zhdd1234/archive/2009/09/15/4555472.aspx

 

WebService caches can be divided into two types: simple output caches and powerful data caches.

I. Output Cache
The use of the output cache is very simple. It is more suitable for scenarios where there are few WebService parameters and the results are relatively simple, such as stock information. You can set a cache within 5 to 10 seconds, weather forecast, you can set cache for 30 minutes or even several hours.

The usage is as follows:
Specify the cacheduration attribute on the webmethod attribute, for example

 

In this way, all the output data of this WebService will be read from the cache within 600 seconds, and no data will be processed. If the transaction code accesses the database, this method is much faster than accessing the database every time. This cache is suitable for beginners who are new to WebService.

[Webmethod (description = "test", cacheduration = 600)]
Public String test ()
{
Return "test ";
}
It should be noted that not all services are suitable for using this cache. For example, if the results are different each time and the number of services accessed is extremely high, the cache will become very large, occupying a lot of server memory, but there is no actual effect.

Ii. Data Cache
Want to save some running data of your WebService? If you do not use a local database or file, caching is the best option. This kind of cache is different from the output cache mentioned above. It needs to write code for implementation, but correspondingly, It is very powerful and can store any type of information, and you can retrieve it at any time.

Although applications can also be used to simulate cache, you need to manage the memory release and user concurrency issues by yourself. This problem has been abandoned in the. NET era. The cache under WebService uses the cache set.

 

Using system. Web. caching;
[Webmethod (description = "test")]
Public String test ()
{
String content = "just4test ";
// Create a data cache
Context. cache. insert ("test", content, null, datetime. maxvalue, timespan. Zero, cacheitempriority. notremovable, null );

String result = context. cache ["test"]. tostring ();
Return result;
}
}

Here, we use the context. cache attribute. The context. cache. insert method is used to add data to the cache. This method has four types of overloading. In this example, we use the most comprehensive feature of the overload version. We take this as an example: each parameter is a key name (the usage method is similar to session ), value, dependency, absolute expiration time, variable expiration time, cache priority, the absolute expiration time of the delegate method when the cache item is deleted is fixed, datatime. maxvalue indicates that it never expires. If the cache is not used for a certain period of time, the variable expiration time automatically expires. Here, timespan. zero indicates no variable expiration is used. Note that only one of the two can be set. If you want to use variable expiration, the absolute expiration must be datatime. maxvalue, for example

Context. cache. insert ("test", content, null, datetime. maxvalue, timespan. fromminutes (10 ));

The cache priority is the possibility that the Web server can clear it. Here cacheitempriority. notremovable indicates that it is not usually deleted from the cache, which can be understood as permanent cache.

Through dependency, You can monitor the changes to a file or other caches. If there are changes, the cache becomes invalid, which is very useful. For example:

Cachedependency de = new cachedependency (server. mappath ("1.xml "));
Context. cache. insert ("test", content, de, datetime. maxvalue, timespan. Zero, cacheitempriority. notremovable, null );

In this way, when the 1. xml file is deleted or modified, the cache will become invalid.

Iii. Practical Examples
In actual use, I use this code. When a remote file is updated, it must be downloaded to the local device, and the cache is responsible for saving the file name and modification time, each time the client requests the file name and time, it directly reads from the cache. The getfiles () method checks whether a new file exists (compared with the local cache) and then determines whether to download the program regularly (with other code.

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
{
// The background code class that provides the download and analysis functions, testop
Private testop;

Public test ()
{
Testop = newtestop ();
}

[Webmethod (description = "download file")]
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 = "check the latest file time")]
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 = "Check latest file name")]
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;
}
}

}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhdd1234/archive/2009/09/15/4555472.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.