C # Simple file-dependent cache usage

Source: Internet
Author: User

One, filecache.aspx page

<%@ Page language="C #"autoeventwireup="true"Codebehind="FileCache.aspx.cs"inherits="Webapplication1.filecache"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"> <meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div> <asp:label id="Label1"runat="Server"/> </div> </form></body>

Two, FileCache.aspx.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Xml.Linq;namespacewebapplication1{ Public Partial classFileCache:System.Web.UI.Page {/// <summary>        ///Gets the cache object value for the current application specified CacheKey/// </summary>        /// <param name= "CacheKey" >index key value</param>        /// <returns>Returning cached Objects</returns>         Public Static ObjectGetCache (stringCacheKey) {System.Web.Caching.Cache Objcache=Httpruntime.cache; returnObjcache[cachekey]; }        /// <summary>        ///set caching data in a cache-dependent way/// </summary>        /// <param name= "CacheKey" >index key value</param>        /// <param name= "Objobject" >Cache Objects</param>        /// <param name= "DEP" ></param>         Public Static voidSetcache (stringCacheKey,ObjectObjobject, System.Web.Caching.CacheDependency CADEP) {System.Web.Caching.Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, Objobject, CADEP, System.Web.Cachin G.cache.noabsoluteexpiration,//never ExpiresSystem.Web.Caching.Cache.NoSlidingExpiration,//Disable Adjustable expirationSystem.Web.Caching.CacheItemPriority.Default,NULL); }        protected voidPage_Load (Objectsender, EventArgs e) {Dictionary<string,string> dic =Newdictionary<string,string>(); stringCacheKey ="CacheKey"; Dictionary<string,string> Objmodel = GetCache (CacheKey) asdictionary<string,string>;//getting from the cache            if(Objmodel = =NULL)//not in the cache.            {                stringPath = Server.MapPath ("Sx.xml");//read the XML file under the server path, generate the dictionary store to the cache, and rely on this fileXElement root =xelement.load (path); foreach(varIteminchRoot. Elements ("Settings"). Elements ("Row"))                {                    varXAttribute = Item. Attribute ("Name"); if(XAttribute! =NULL&&!DiC. ContainsKey (Xattribute.value)) {dic. ADD (Xattribute.value, item. Attribute ("Value").                    Value); }} System.Web.Caching.CacheDependency dep=NewSystem.Web.Caching.CacheDependency (path); Setcache (CacheKey, DIC, DEP);//write cache and rely on the Sx.xml file} Objmodel= GetCache (CacheKey) asdictionary<string,string>; if(Objmodel! =NULL) Label1.Text = objmodel["V"];//When you change the value of the server's Sx.xml name=v, the cache automatically updates the values        }    }}

Third, the sx.xml under the server

<?xml version="1.0"encoding="Utf-8"?><root> <Settings> <row name="V"Value=" -"></row> <row name="V1"Value=" One"></row> <row name="V2"Value=" A"></row> </Settings></root>

The idea is to read the XML file, store it in dictionary<string,string>, and store the dictionary in the cache and rely on the XML file, and when the XML file changes, the cache automatically updates the changed values.

C # Simple file-dependent cache usage

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.