Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of adding ASP.NET cache data

Shulou Source: shulou.com Published: 2022-06-02 07:47:07 09月18日 Update

This article mainly introduces "what is the method of adding ASP.NET cache data". In the daily operation, I believe that many people have doubts about what the method of adding ASP.NET cache data is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of adding ASP.NET cache data?" Next, please follow the editor to study!

Overview of ASP.NET cache data addition requirements

ASP.NET uses a caching mechanism to store objects that require a lot of server resources to create in memory. Caching these types of resources can greatly improve the performance of your application. Caching is implemented by the Cache class, and you can control the "cleanup" priority when you run out of memory by setting the priority CacheItemPriority enumeration value on the cache. You can also set expiration policies for caches and set dependencies for caches.

ASP.NET cache data add (add data items to cache)

1. Add through key-value pair

Cache ["CacheItem"] = "Cached Item"

2. Add through the Insert method

The Insert method adds an item to the cache, and an item with the same name as the existing item already exists, the existing item in the cache is replaced.

Cache.Insert ("CacheItem", "Cached Item")

3. Specify dependencies and add them (specify dependencies for data items added to the buffer)

A situation where a data item depends on a string array object:

String [] dependencies = {"Dependences"}; Cache.Insert ("CacheItem", "Cached Item", new System.Web.Caching.CacheDependency (null, dependencies))

When a data item depends on an XML file:

Cache.Insert ("CacheItem", "Cached Item", new System.Web.Caching.CacheDependency (Server.MapPath ("XMLFile.xml")

Where a data item depends on multiple dependencies:

System.Web.Caching.CacheDependency dep1 = new System.Web.Caching.CacheDependency (Server.MapPath ("XMLFile.xml")); string [] keyDependencies2 = {"CacheItem1"}; System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency (null, keyDependencies2); System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency (); aggDep.Add (dep1); aggDep.Add (dep2); Cache.Insert ("CacheItem", "Cached Item", aggDep)

4. Set the expiration policy and add

Add one minute absolute expiration time to the cache:

Cache.Insert ("CacheItem", "Cached Item", null, DateTime.Now.AddMinutes (1D), System.Web.Caching.Cache.NoSlidingExpiration)

Add 10-minute elastic expiration time to the cache:

Cache.Insert ("CacheItem", "Cached Item", null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan (0,10,0))

5. Set priority and add

Call the Insert method to specify a value from the CacheItemPriority enumeration.

Cache.Insert ("CacheItem", "Cached Item", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null)

6. Add through the Add method

The Add method returns the object you added to the cache. In addition, if the Add method is used and an item with the same name as the existing item already exists in the cache, the method does not replace the item and does not throw an exception.

String CachedItem = (string) Cache.Add ("CacheItem", "CachedItem", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null); at this point, the study on "what is the method of adding ASP.NET cached data" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Cache method data data item learning object situation priority memory time more policy resource help practical next not enough middle finger can pass multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB vpn Xiaomi Redmi