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 examples
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.
The above is the. NET Cache--file-based cache content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!