ASP.NET website development - data caching technology

Data caching technology

Caching Concept: Caching is a technique widely used in computers to improve performance. In the context of a web application, caches are used to persist pages or data between HTTP requests and use them multiple times without creating more.

Purpose: To save application processing time and resources

Cache system:


@outputcache directive

Httpcachepolicy class

protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToLongTimeString();
        }
        public static string GetTime(HttpContext conten)
        {
            return DateTime.Now.ToLongTimeString();
        }

Partial caching of pages

Write the @outputcache directive to the user control file


substitution control

When using substitution, first we cache the entire page, and then replace the place in the page where the content needs to be dynamically changed with the substitution control. The substitution control needs to set an important attribute methodname, which is used to get or set the method name that is called for the callback when the substitution control is executed.

The callback method must meet three points:

(1) The callback method must be defined as a static method.

(2) The method must accept a parameter of type httpcontext.

(3) The method must return a value of type string.

save after cache



application data cache

The main function of the application data cache is to store various application-related objects in memory. Usually, these energy needs to consume a lot of server resources to create, and the application data cache is implemented by the cache.




Here is the add method

protected void btnadd_Click(object sender, EventArgs e)
        {
            try
            {
                Cache.Add("aaa","ADD CACHE",null,Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration,CacheItemPriority.Default,null);

            }
            catch
            {
                Response.Write("ERROR");
                
            }
        }

Below is the insert method

protected void btninsert_Click(object sender, EventArgs e)
        {
            Cache.Insert("aaa","INSERT CACHE");
        }

The following is the get method

protected void btnget_Click(object sender, EventArgs e)
        {
            //get
            Cache.Get("");

            //index
            if (Cache["aaa"]!=null)
            {
                string str = (string)Cache["aaa"];
                Response.Write(str);
            }
            else
            {
                Response.Write("Cache is invalid!");
            }
        }

The following is the effect of the page




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324843916&siteId=291194637