Data caching in asp net example with Add and Insert methods in C#

Lionsure 2020-09-19 Original by the website

Proper use of Cache can effectively improve program execution efficiency in the process of program development. For example, some frequently called system public variables are cached in the memory, and when they are needed, they are read directly from the memory, instead of reading from the database or file every time, thereby reducing server pressure and improving program efficiency.

How to create cache in asp.net in C#? There are usually two ways to create Cache, namely Add() and Insert(). Let's see how they are used and what are the similarities and differences.

 

1. Data caching in asp net example with Add method

This method is to create a new Cache object. If the object to be created already exists, an exception is thrown and the creation fails. Examples are as follows:

/// <summary>
       /// C# cache example with Add method
       /// </summary>

       public void NewCacheAsAdd()
       {
              string strSysConfig = "Website configuration information";
              if (Cache["sysConfig"] == null)
                     Cache.Add("sysConfig", strSysConfig, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero, CacheItemPriority.High, null);
       }

In the above method, a new Cache object whose Key is "sysConfig" and the value is "strSysConfig" is created; the absolute expiration time(i.e. DateTime.Now.AddMinute(30)) is used, which means that the cache expires after 30 minutes; the cache level is high(i.e. CacheItemPriority.High ), which means that when the memory is insufficient, it will not be cleared first.

 

2. Data caching in asp net example with Insert method(Dependent cache)

This method is to newly create or overwrite a Cache object. If the object to be created does not exist, create a new object; if it already exists, overwrite it. Examples are as follows:

/// <summary>
       /// C# cache example with Insert method
       /// </summary>

       public void NewCacheAsInsert()
       {
              ProductClass proClass = New ProductClass();
              IList<ProductClassInfo> pci = proClass.GetProductClasses();

       ProductClassCacheDependency pccd = new ProductClassCacheDependency();
              CacheDependency cacheDepend = pccd.GetProductClassDependency();

       Cache.Insert("ProductClass", pci, cacheDepend, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30), CacheItemPriority.Normal, null);
       }

In the above method, a new Cache object whose Key is "ProductClass" and value is "pci" is created; and the cache depends on the database(cacheDepend), that is, if the database is updated, the cache is automatically updated; use relative expiration time(i.e. TimeSpan.FromMinut(30)), it means that the cache has not been accessed for more than 30 minutes, and it is automatically cleared; the cache level is normal(i.e. CacheItemPriority.Normal), which means that it will be cleared first when the memory is insufficient.