ASP. net mvc 1.0 StaticCacheAttribute

Source: Internet
Author: User

Static cache is different from OutputCache. It is applicable to websites with a large number of accessible pages (such as news sites ). You need to know that our website will be inspected by many silly spider and Water Filling Machine with low IQ every day. These guys will often traverse more than n pages in depth, it is easy to cause our limited "dynamic cache" to be frequently refreshed, losing the value of the cache. Another point is that static cache is often stored independently, which can be multiple Memchaced or DFS systems. In a Server Load balancer environment, this cross-process and machine cache mode is more effective.

First, we define a cache interface so that multiple cache policies can be implemented in the future.

Public interface IStaticCache
{
Void Set (string key, object value, DateTime absoluteExpiration );
Void Set (string key, object value, TimeSpan slidingExpiration );
T Get <T> (string key );
Void Delete (string key );
}

Next, we will intercept the ActionResult Render output, save it to the cache, and use it for the next access. The principle is very simple, that is, call Response. SwitchWriter to replace the output device.

Public sealed class HttpResponse
{
Internal TextWriter SwitchWriter (TextWriter writer)
{
TextWriter writer2 = this. _ writer;
This. _ writer = writer;
Return writer2;
}
}

Although internal, reflection is easy to achieve.

Private TextWriter SwitchWriter (TextWriter writer)
{
Var method = typeof (HttpResponse). GetMethod ("SwitchWriter ",
BindingFlags. NonPublic | BindingFlags. Instance );

Return method. Invoke (HttpContext. Current. Response, new object [] {writer}) as TextWriter;
}

Next, we intercept the output results.

Public class StaticCacheAttribute: ActionFilterAttribute
{
Private TextWriter origin;
Private StringWriter writer;

Public override void OnActionExecuting (ActionExecutingContext filterContext)
{
Writer = new StringWriter ();
Origin = SwitchWriter (writer );
}

Public override void OnResultExecuted (ResultExecutedContext filterContext)
{
If (writer! = Null)
{
Writer. Flush ();
Var html = writer. ToString ();

// Output content to the client.
SwitchWriter (origin );
FilterContext. HttpContext. Response. Write (html );
FilterContext. HttpContext. Response. Flush ();
FilterContext. HttpContext. Response. End ();
}
}
}

After intercept, do not forget to change the original TextWriter back and output the intercepted content.

With this data, you can use the cache. Note that, after hitting the cache, you should use "filterContext. Result = new EmptyResult ();" to prevent Action Method execution; otherwise, the cache will be lost.

Cause: ControllerActionInvoker. InvokeActionMethodFilter () Determines "preContext. Result! = Null "to determine whether to execute subsequent calls.

Public class ControllerActionInvoker
{
Internal static ActionExecutedContext InvokeActionMethodFilter (...)
{
Filter. OnActionExecuting (preContext );

If (preContext. Result! = Null)
{
Return new ActionExecutedContext (...)
{
Result = preContext. Result
};
}

...
}
}

Let's take a look at a complete example of StaticCacheAttribute.

Public class StaticCacheAttribute: ActionFilterAttribute
{
Private TextWriter origin;
Private StringWriter writer;
Private String key;

Public StaticCacheAttribute ()
{
Duration = 60; // second
ContentType = "text/html ";
Cache = StaticHtmlCache. Instance;

This. Order =-1;
}

Public int Duration {get; set ;}
Public string ContentType {get; set ;}
Public IStaticCache Cache {get; set ;}

Private TextWriter SwitchWriter (TextWriter writer)
{
Var method = typeof (HttpResponse). GetMethod ("SwitchWriter ",
BindingFlags. NonPublic | BindingFlags. Instance );

Return method. Invoke (HttpContext. Current. Response, new object [] {writer}) as TextWriter;
}

Public override void OnActionExecuting (ActionExecutingContext filterContext)
{
Key = filterContext. HttpContext. Request. RawUrl;
Var html = Cache. Get <string> (key );

If (String. IsNullOrEmpty (html ))
{
Writer = new StringWriter ();
Origin = SwitchWriter (writer );
}
Else
{
FilterContext. Result = new EmptyResult ();

FilterContext. HttpContext. Response. ContentType = ContentType;
FilterContext. HttpContext. Response. Write (html );
FilterContext. HttpContext. Response. Flush ();
FilterContext. HttpContext. Response. End ();
}
}

Public override void OnResultExecuted (ResultExecutedContext filterContext)
{
If (writer! = Null)
{
Writer. Flush ();
Var html = writer. ToString ();

// Save the result to the cache.
Cache. Set (key, html, DateTime. Now. AddSeconds (Duration ));

// Output content to the client.
SwitchWriter (origin );
FilterContext. HttpContext. Response. Write (html );
FilterContext. HttpContext. Response. Flush ();
FilterContext. HttpContext. Response. End ();
}
}
}

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.