Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

c# - Pros/Cons of different ASP.NET Caching Options

I recently asked a question about caching application data in an ASP.NET MVC WebAPI application and it led me to a new question. What are the pros/cons of different caching methods available in ASP.NET?

I have come upon:

  • Memory Cache

    http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

  • Using Static Member Variables:

    private static Northwind.SuppliersDataTable suppliers = null;
    
  • Application State:

     HttpContext.Current.Application["key"] ="Value"
    
  • Data Cache:

    HttpRuntime.Cache.Insert(
      /* key */                "key", 
      /* value */              "value", 
      /* dependencies */       null, 
      /* absoluteExpiration */ Cache.NoAbsoluteExpiration, 
      /* slidingExpiration */  Cache.NoSlidingExpiration, 
      /* priority */           CacheItemPriority.NotRemovable, 
      /* onRemoveCallback */   null);
    

I am sure there are others, and I know they all technically store the data in memory...so any idea what I should use for an ASP.NET MVC webapi?

My Previous Question: Caching application data in memory: MVC Web API

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Each Caching technology/methods have their own set of features. These features may seem to be of disadvantage in one application requirements but can be advantageous in other application requirements.

So, in short , depending on your requirements decide which Caching technology and what features are best for you.

For example, Let us discuss some client side Caching techniques.

MSDN says that we can also use HiddenField to store only small amounts of frequently changing data in hidden fields because this data is included in the roundtrips to the server on every postback.

Advantage of this feature: Reduces the workload on your server by storing page information using client-side options.

However, MSDN says clearly that : This approach has minimal security support.

Thus, one may or may not use this feature always as security considerations are also there.

Consider one more example, Page Output caching : it is of 2 types, page output caching and page fragment caching.

Page output caching caches an entire Web page and is suitable only when the content of that page is fairly static. If parts of the page are changing, you can wrap the static sections as user controls and cache the user controls using page fragment caching.

And one last comment on Application vs HttpRuntime.cache:

Application is not a cache, its a global named value collection. if you add an object to Application it will stay until a an appdomain recycle.

  • Application variables are shared variables among all users of a web application
  • Application variables behave like static variables and they are substitute of static variables as static variables are stateless in web applications
  • Only shared values should be persisted in Application variables, and as soon as they are not in use they should be removed explicitly.

Cache :It is possible to obtain significant performance improvements in ASP.NET applications by caching frequently requested objects and data in either the Application or Cache classes. While the Cache class certainly offers far more flexibility and control, it only appears to offer a marginal advantage in terms of increased throughput over the Application class for caching. It would be very difficult to develop a testing scheme that could accurately measure the potential advantages of the Cache class's built - in management of lesser-used objects through the scavenging process as opposed to the fact that Application does not offer this feature. The developer needs to take decision in this case and should be based on the needs and convenience of the project and its usage patterns. Check this link for more.

Refer this MSDN article for a complete great explanation on all Caching technologies in Asp.net with discusiion on the features of each technology.

Also, these 2 links are a great source to start with:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...