Overview of ASP. NET cache data adding requirements
ASP. NET uses the cache mechanism to store objects that require a large amount of server resources in the memory. Caching these types of resources will greatly improve the application performance. Cache is implemented by the cache class. You can set the cache priority cacheitempriority to control the "clean" priority when the memory is insufficient. You can also set an expiration Policy for the cache and a dependency for the cache.
Add ASP. NET cache data (add data items to cache)
1. Add a key-Value Pair
- Cache ["cacheitem"] = "cached item ";
2. Use the insert method to add
If 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 will be replaced.
- Cache. insert ("cacheitem", "cached item ");
3. Specify and add dependencies (specify dependencies for data items added to the buffer)
When a data item depends on a string array object:
- String[] Dependencies = {"dependences "};
- Cache. insert ("cacheitem ",
- "Cached item ",
- NewSystem. Web. caching. cachedependency (Null, Dependencies ));
When a data item depends on an XML file:
- Cache. insert ("cacheitem", "cached item ",
- NewSystem. Web. caching. cachedependency (
- Server. mappath ("xmlfile. xml ")));
Where data items depend on multiple dependencies:
- System. Web. caching. cachedependency dep1 =
- NewSystem. Web. caching. cachedependency (server. mappath ("xmlfile. xml "));
- String[] Keydependencies2 = {"cacheitem1 "};
- System. Web. caching. cachedependency dep2 =
- NewSystem. Web. caching. cachedependency (Null, Keydependencies2 );
- System. Web. caching. aggregatecachedependency aggdep =
- NewSystem. Web. caching. aggregatecachedependency ();
- Aggdep. Add (dep1 );
- Aggdep. Add (dep2 );
- Cache. insert ("cacheitem", "cached item", aggdep );
4. Set an expiration Policy and add
Add an absolute expiration time of one minute to the cache:
- Cache. insert ("cacheitem", "cached item ",
- Null, Datetime. Now. addminutes (1D ),
- System. Web. caching. cache. noslidingexpiration );
Add a 10-minute elastic expiration time to the cache:
- Cache. insert ("cacheitem", "cached item ",
- Null, System. Web. caching. cache. noabsoluteexpiration,
- NewTimespan (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. Use the add method to add
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 an existing item already exists in the cache, this method will not replace this item and will not cause an exception.
- StringCacheditem = (String) Cache. Add ("cacheitem ",
- "Cached item ",Null,
- System. Web. caching. cache. noabsoluteexpiration,
- System. Web. caching. cache. noslidingexpiration,
- System. Web. caching. cacheitempriority. Default,
- Null);