Using custom cache dependencies in asp.net 2.0

Source: Internet
Author: User
Tags implement insert set time time interval xsl
Asp.net| Cache

In ASP.net 1.x, we can use CacheDependency to implement the caching dependency policy, but since this class is sealed, we cannot inherit this class to implement our own strategy. But by the ASP.net 2.0, we can derive our own cache dependency classes from this class.

Suppose we want to design a page, need to get the latest post information from the homepage of the blog. To improve performance, we want the page data to be regenerated only when there is an update on the homepage of the blog, otherwise it will be retrieved directly from the cache. How to achieve it?

  First, the Design Blogcachedependency class

First of all, there is no doubt that this class should derive from CacheDependency, and then it can be used in the Cache Insert method or used in the Aggregatedependency class.

Second, from the blog to provide RSS and page design point of view, you can put RSS data in the cache, display the use of a style conversion. And when checking dependencies, we simply need to compare the current RSS to the website RSS is the same.

A more important question is: When do we check compare RSS data? At every request? Obviously not, this makes it almost indistinguishable from not using the cache, and even actually adds to the unnecessary burden. Consider checking out when you don't have a request? We can use a Timer to control, let it periodically check whether there is an update, if there is an update to notify the dependency has changed.

We know that the CacheDependency class has a haschanged attribute, but how do you tell the base class when blogcachedependency checks for dependency changes? This is the mission of the new Notifydependencychanged method in the CacheDependency class in ASP.net 2.0.

In addition, for ease of reuse, the Blogcachedependency class must have a feed data to hold the URL of the RSS data we are getting. There is also a time interval that allows you to adjust the refresh speed when used.

OK, look at the actual implementation code:

1 public class Blogcachedependency:cachedependency
2 {
3 Private Timer _ticktimer;
4 private int _timeinterval;
5 Private XPathNavigator _rss;
6 private string _feed;
7
8 Public XPathNavigator RSS
9 {
Ten get
11 {
return _rss;
13}
14}
15
Public blogcachedependency (string feed, int timeinterval)
17 {
_feed = feed;
_timeinterval = TimeInterval;
_rss = Getrss ();
_ticktimer = new Timer (new TimerCallback (Checkdependencycallback),
This, _timeinterval * 1000, _timeinterval * 1000);
23}
24
XPathNavigator Private Getrss ()
26 {
XPathDocument Rssdoc = new XPathDocument (_feed);
return Rssdoc.createnavigator ();
29}
30
to public void Checkdependencycallback (object sender)
32 {
Blogcachedependency BCD = sender as blogcachedependency;
XPathNavigator newrss = Getrss ();
if (Newrss.outerxml!= _rss). OuterXml)
36 {
Radix Notoginseng BCD. Notifydependencychanged (BCD, eventargs.empty);
38}
39}
40
protected override void Dependencydispose ()
42 {
_ticktimer = null;
Base. Dependencydispose ();
45}
46}
47
48

Here, the Blogcachedependency constructor uses _ticktimer to implement a timed check update mechanism that invokes the Checkdependencycallback method based on the set time interval.

The Checkdependencycallback method compares two RSS feeds and, if different, calls the Notifydependencychanged method to notify the base class that the corresponding cache dependencies have changed and that the data in the cache should be purged.

  Second, the page design

The following is a page code (truncated) that shows how blogcachedependency is used:

1<script runat= "Server" >
2 protected void Page_Load (object sender, EventArgs e)
3 {
4 string feed = "Http://www.cnblogs.com/RSS.aspx";
5 if (cache[feed] = = null)
6 {
7 blogcachedependency BCD = new Blogcachedependency (feed, 600);
8 Cache.Insert (Feed, BCD. RSS, BCD);
9 Label1.Text = "Current data is just acquired and has been updated into the cache!" ";
10}
One else
12 {
Label1.Text = "The current data system is obtained from the cache!" ";
14}
Rssxml.xpathnavigator = Cache[feed] as System.Xml.XPath.XPathNavigator;
Rssxml.transformsource = "translate.xsl";
17}
18</script>
19
20<body>
<form id= "Form1" runat= "Server" >
22 Blog Park Latest Posts:
<br/>
<asp:xml id= "Rssxml" runat= "Server"/>
<br/>
<asp:label id= "Label1" runat= "server" forecolor= "Red"/>
</form>
28</body>
29

This example sets the visit to the blog home the latest list of posts, the time interval is 600 seconds, that is, every 10 minutes to check the update.

A few noteworthy places:

1, pay attention to the use of rssxml.xpathnavigator properties, someone may wonder why not rssxml.document it? In fact, the Document attribute has been abolished in. NET 2.0, and the recommended alternative is the XPathNavigator property, which, as you can see from the previous Blogcachedependency class, is derived from the Xpathdocument.createnavigator () is created, as we can see from MSDN, that the XPathDocument class provides a read-only cache, which is certainly more appropriate for this example.

2. Consider the Dependencydispose method in the Blogcachedependency class. How does it differ from the Dispose method? Let's think about it: if a review of the update has found that dependencies have changed, but has not yet sent the request again, then will it always be continuous at intervals to execute the Checkdependencycallback method? If so, that would not be completely superfluous, because as long as there is a change in the search is not necessary. And if we keep track of it or log it, we can see that in fact we don't check it again until we find the dependency change. Where is the mystery? Think about it can know notifydependencychanged method is very mysterious, and the reason why there is Dependencydispose method is actually here. One of the design ideas, we should savor it.

  Third, the page uses the translate.xsl

No more say, post the main code:

1<xsl:template match= "Channel" >
2 <div>
3 <xsl:for-each select= "Item" >
4 <a>
5 <xsl:attribute name= "href" >
6 <xsl:value-of select= "link"/>
7 </xsl:attribute>
8 <xsl:value-of select= "title"/>
9 </a>
Ten <br/>
</xsl:for-each>
</div>
13</xsl:template>

  Iv. Status of implementation

This is the first screenshot of the execution:

When the Blog Park home page does not appear new posts, we refresh the page, always can get the following page:

And once you have a new post, then the refresh occurs when the previous picture.

  Do you want to be a little more advanced?

If you're as lazy or lazy as I am, then you can consider using JavaScript to write a feature that automatically refreshes the page, do you want to work on the page, or wrap it up as a reusable component on your site, or just try to do a "my most focused content set" thing in this machine? Well, I think the effect will be quite good.



Related Article

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.