[. NET object-oriented programming advanced ] (15) Cache (Cache) ( two ) using cache to boost program performance
This section is guided by:
The last section says that caching is a technology that takes space for time, introduces client-side caching and two commonly used server-based ease-of-use, this section focuses on a . NET Cache of particularly important slow-cloth technologies . Use Cache Improve program performance.
1. Caching the cache namespace
two namespaces for cache in . NET
namespace 1:system.web.caching
namespace 2:system.runtime.caching
Reference scopes: Both of these namespaces can be referenced in Web and non- web Applications .
life cycle: Theoretically, we can set the expiration time. But when the memory overhead is tight, the cache frees up space. As a result, the application in our program will determine if the Cache object exists and is not present, re-create it to resolve the failure problem.
Here's how to use caching in detail.
2. Creating and Reading Caches
caching is also a pair of objects, we first create an entity class, or more familiar with the "Martial arts Master" class:
/// <summary>///class: Martial arts Master///Martialartsmaster/// </summary> Public classmartialartsmaster{/// <summary> ///numbering/// </summary> Public intID {Get;Set; } /// <summary> ///name/// </summary> Public stringName {Get;Set; } /// <summary> ///Door Faction/// </summary> Public stringMenpai {Get;Set; } /// <summary> ///Martial Arts/// </summary> Public stringKongfu {Get;Set; }}
below we create a cache, and read the cache, traverse the object
//Add a few martial arts masterslist<martialartsmaster> masterlist =NewList<martialartsmaster>() { NewMartialartsmaster () {id=1, name="Duan Yu", menpai=" Dragon Temple", kongfu="Six-Pulse sword"}, NewMartialartsmaster () {id=2, name="Qiao", menpai="confraternity", kongfu="Dragon 18 Palm"}, NewMartialartsmaster () {id=3, name="Virtual Bamboo", menpai="Carefree Pie", kongfu="Northern Underworld"}};//creation of the cacheSystem.Web.Caching.Cache Objcache =Httpruntime.cache;objcache.insert ("Mycache", masterlist);//Cached Reads(objcache["Mycache"] aslist<martialartsmaster>). ForEach (M = Console.WriteLine ("Name:"+ M.name +"Gate Faction:"+ M.menpai +"Martial Arts:"+ M.kongfu));
The results of the operation are as follows:
3. Application of common cache in factory reflection mode
We do not discuss design patterns first, but when using many code generators, when creating multiple data-tier interfaces dynamically for factory reflection mode, caching techniques are often used to cache classes created by dynamic reflection to improve the efficiency of the next visit.
Here is a snippet of the cache application code in the Simple Factory reflection mode:
object objType = GetCache (ClassName); // read from Cache if NULL { try { = Assembly.Load (assemblypath). CreateInstance (ClassName); // Reflection Creation Setcache (Classnamespace, ObjType); // Write Cache } catch(Exception ex) {}}
4. Principles of use of the cache
Caching effectively improves the efficiency of the program, but it also needs to be used reasonably, following several principles of use :
First, data may be used frequently, and this data can be cached.
Second, the frequency of access to data is very high, or the frequency of access to a data is not high, but it has a long life cycle, such data is best to cache.
The third is that in ASP., if the cache is too large, a memory overflow error is reported, especially if the large dataset object is cached.
According to the actual situation to make reasonable use. For example, using too much cache will increase the pressure on the server. Full-page output caching can also affect data updates. If you really need to cache a lot of data, you can consider static technology.
5. Universal Cache Class
Finally, sort out a common class of caches for the small partners to call
namespaces: Kajimao.common
class Name: Cachehelper
file name:cachehelper . CS
usingSystem;usingsystem.web;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacekajimao.common{/// <summary> ///Cache General Class///Yubinfeng///DATE:2015/07/16 Public classCachehelper {/// <summary> ///Get data cache/// </summary> /// <param name= "CacheKey" >Key</param> Public Static ObjectGetCache (stringCacheKey) {System.Web.Caching.Cache Objcache=Httpruntime.cache; returnObjcache[cachekey]; } /// <summary> ///setting up the data cache/// </summary> Public Static voidSetcache (stringCacheKey,Objectobjobject) {System.Web.Caching.Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, objobject); } /// <summary> ///setting up the data cache/// </summary> Public Static voidSetcache (stringCacheKey,ObjectObjobject, TimeSpan Timeout) {System.Web.Caching.Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, Objobject,NULL, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable,NULL); } /// <summary> ///setting up the data cache/// </summary> Public Static voidSetcache (stringCacheKey,ObjectObjobject, DateTime absoluteexpiration, TimeSpan slidingexpiration) {System.Web.Caching.Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, Objobject,NULL, absoluteexpiration, slidingexpiration); } /// <summary> ///remove the specified data cache/// </summary> Public Static voidRemoveallcache (stringCacheKey) {System.Web.Caching.Cache _cache=Httpruntime.cache; _cache. Remove (CacheKey); } /// <summary> ///Removing all caches/// </summary> Public Static voidRemoveallcache () {System.Web.Caching.Cache _cache=Httpruntime.cache; IDictionaryEnumerator Cacheenum=_cache. GetEnumerator (); while(Cacheenum.movenext ()) {_cache. Remove (CacheEnum.Key.ToString ()); } } }}
View Code
==============================================================================================
Back to Catalog
< If it is helpful to you, please remember to click on the recommendation Oh, if there is There is no understanding or error , please exchange >
< for those who have difficulty reading this series of articles, please read the basics of. NET object-oriented programmingfirst >
< REPRINT statement: Technology needs to share the spirit, welcome to reprint the article in this blog, but please specify copyright and url>
. NET Technology Exchange Group: 467189533
==============================================================================================
[. NET object-oriented programming advanced] (15) caching (cache) (ii) using cache to boost program performance