. NET Cache-file-based caching

Source: Internet
Author: User




one,. The basics of caching in net



Two types of dependencies are supported in. NET:

CacheDependency

SqlDependency

Indicates a dependency on a file or directory

Represents a dependency on a SQL database

Expiry time


Absolute Expiration Time

Sliding Expiration Time

A specific point in time, type datetime

A time interval with a timespan of type

Priority: CacheItemPriority


Because we need to cache a large amount of data, in the case of limited memory, we must prioritize the cached data so that unimportant data can be removed from the cache when needed. Priority is used to specify the importance of cached data, and important data can be stored in memory for longer periods of time.

Delete notification


When the cached data is removed from memory, a notification mechanism can be provided to back-tune the user-defined method, which must conform to the definition of the CacheItemRemovedCallback delegate.

Special attention:


1, the timing of the callback is unpredictable and cannot be assumed when the callback occurs, the execution thread of the callback method has a HttpContext context, in order to obtain a reference to the cache object without requesting the context. The application's cache can be used through the HttpRuntime cache property.

2, can not use the instance method on the page as a callback method, block on the page when using the callback method, because the reference to the callback method will block the garbage collection mechanism, it will cause the memory to consume light quickly.

3, it is generally implemented by implementing a callback method in a static method of a custom class, or by using a static method of a Page object.





two, file-based caching example







First, add a label to the test page:


  <asp:label text= "" runat= "Server" id= "Label1"/>

Next, add the cache management class:



<span style= "FONT-SIZE:14PX;"                >namespace file-based cache dependency {public class CacheManager {public static string Message {get {                HttpContext context = HttpContext.Current; First get the string message = context from the cache.                cache["Message"] as String; If the cache does not have data if (message==null) {string path = context.                    Server.MapPath ("TestFileCache.txt");                    message = System.IO.File.ReadAllText (path); Save the information to the cache, and set the response to expire in 1 minute context. Cache.Add ("message", message, new System.Web.Caching .                        CacheDependency (path),//dependent on file or directory system.web.caching.cache.noabsoluteexpiration,//do not use absolute expiration time New TimeSpan (0, 0, 5),//Cache time System.Web.Caching.CacheItemPriority.Normal,//Cached Priority level Callback); When it expires.callback} return message;        }}///<summary>//callbacks the specified key. </summary>//<param name= "key" >the key.</param>//<param name= "value" >the VA lue.</param>//<param name= "Reason" >the reason.</param>//<remarks>editor:v-liuh            CH CREATETIME:2015/5/26 20:13:22</remarks> private static void Callback (string key,                Object value, System.Web.Caching.CacheItemRemovedReason reason//reason) { if (reason = = System.Web.Caching.CacheItemRemovedReason.Expired) {httpruntime.ca Che.                                    Insert ("Message", "time passed, cache expired"); }}}}</span>


Here, the Add method and insert method of the cache are distinguished:


When add is called, the call fails if the cache entry to be added exists, but if insert is inserted, the newly inserted value value overrides the original value value.



In WebForm1, add a line of tests:



protected void Page_Load (object sender, EventArgs e)        {                this.label1.Text = cachemanager.message;   Get Cache        }





Can see the contents of the file has been read, because the setting is 5s after expiration, so 5s after we refresh:





But many tests we will find that the correct time to expire after the refresh is not 5s, while verifying the timing of the callback is unpredictable.

























. NET Cache-file-based caching

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.