Start writing Enterprise Library 2.0 learning experience, ready to first write the entry part of each part, then continue to study each part, I hope to get the master's guidance and recommendations. Today is written in Enterprise Library 2.0, the Caching Application Block in Enterprise Library 2.0, in the previous introduction, refers to the help of Enterprise Library 2.0 comes.
First, why use cache technology?
When we build an enterprise-class distributed application, designers and developers will face a lot of challenges. Caching can help them overcome this
Some difficulties, including: 1, performance (Performance): Caching is primarily to improve the performance of the application by as much as possible to improve the performance of the application, avoiding repeated access data from the database. 2, variability: Use the cache to store data to save system resources, and can increase variability as the application needs increases. 3, Availability: By storing data to a local cache, it is also possible to make the application work in a system failure, including hardware, network failure, etc.
Second, in what circumstances? 1. When you need frequent access to static data or access, it is rarely changed; 2. When you access the data, you need to spend a lot of time; 3, the data that is often used needs to enter the cache.
Third, can the cache are in which projects? 1, WinForm 2, Console Application3, Windows Service4, COM Server5, Web Service6, ASP.NET Web Applicatio, etc. ... Of course, we should also understand that each application can use multiple cache, but cannot be different A Cache is also enjoyed in the app. The performance of Caching Application Block in Enterprise Library has been highly optimized, and thread is secure and exception, we can also extend it according to your needs.
Fourth, the requirements for the system 1.Microsoft Windows 2000, Windows XP Professional, or Windows Server 2003 Operating System 2.MICROSoft .NET Framework 2.0 3.VS2005 5. Preliminary Experience Caching Application Block
Let's take a look at how to use the Caching Application Block: First add a reference to Microsoft.Practices.EnterpriseLibrary.caching.
[TestMethod]
public
Void
Usecaching ()
{INT ID = 0; string name = "shy520"; // Create a cache manager cacheManager testCaching = cachefactory.getCachemanager (); // Add data to cached data TestCaching.Add (id.toString (), name); // Remove the data in the cache String RName = (String) TestCaching.getdata (id.Tostring ()); assert.Arequal (name, rname);}
I believe that through the above example, you have a preliminary understanding of the Caching Application Block in Enterprise Library2.0, let's talk about how to configure Caching Application Block. 6. Configuring the Caching Application Block first opens our project, I use a Test Project when I am debugging, and create a new configuration file app.config. At this time, we select App.config in the project to right click, select Open with ..., as shown below: Point Add in the pop-up Choose Program dialog box, and select the bin directory in our Enterprise Library2.0 installation directory. Entlibconfig.exe file, as shown below:
At this point, the following picture will appear after the app.config file: then click File -> Save all, so we completed the configuration of the Caching Application Block, then we found more about our app.config files:
XML Version = "1.0" encoding = "UTF-8"
?>
<
CONFIGURATION
>
<
Configsections
>
<
section
Name
= "CachingConfiguration"
Type
= "Microsoft.Practices.enterpriselibrary.Caching.Configuration.cachemanagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version = 2.0.0.0, Culture = neutral, publickeyToken = NULL
/>
Configsections
>
<
CachingConfiguration
DefaultCachemanager
= "Cache Manager"
>
<
CacheManagers
>
<
Add
ExpirationPollfrequencyInseconds
= "60"
MaximumeLementsIncacchebeForescavenging
= "1000"
Numbertoremovewhenscavenging
= "10"
Backingstorename
= "Null Storage"
Name
= "Cache Manager"
/>
CacheManagers
>
<
Backingstores
>
<
Add
EncryptionProvidername
= ""
Type
= "Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = null"
Name
= "Null Storage"
/>
BACKINGSTORES>
CachingConfiguration
>
CONFIGURATION
>
7. Simple operation of Caching Application Block 1. ADDING ITEMS to the Cache The following code uses the cachemanager's add method, which creates a string type object and save it to a cache, a cache time is 10 minutes, in which 10 Minutes, we can get the value stored in the cache by getData ().
[TestMethod]
public
Void
Usecaching ()
{INT ID = 0; string name = "shy520"; // Create a cache manager cacheManager testcaching = cachefactory.getCachemanager (); // Add data to be cached, cache time is 10 minutes TestCaching.Add (id.toString (), Name, CacheItemPriority.Normal, Null, New SlidingTime (Timespan.Fromminutes (10)))); // Remove the cached data string rname = (String) TestCaching.getdata (id.tostring ()); assert.Arequal Name, RNAME);
2.Loading The cache loading cache has two situations: Proactive Loading and Reactive Loading. I understand that the active cache is the data that will be used throughout the entire system in the project or the data that is often used in the project, and the data that is often used frequently, is starting from the database. Take out, load it into the cache, and the passive cache will temporarily load the cache when you need to respond to a page or a request request. The two have their own advantages and disadvantages. The advantages of active cache are: 1. Because this cache has been put into cache when the initialization of the application is initialized, we don't need to judge whether it exists in the future (of course, I recommend using it. " Before the cache should be judged as null); 2. Since the data we often need to use is stored in the active cache, it will greatly improve our program's response speed. But too much use of this class cache will occupy a lot of resources in our system, affecting the speed of the system, so reasonable use of active cache is very important. The advantage of passive cache is to load the data into the cache, very flexible, and it will not take up the system too many resources. Disadvantages is because it is flexible to make us unclear it, when it is used, it must first check Is it existing. For example, let's say it (maybe it is not very appropriate), when we do a MIS system, the landing is essential, and we generally store some of the necessary information to a certain place when the user logs in, so that When you operate, you can use the active cache at this time.
[TestMethod]
public
Void
Loadingcache ()
{// Assume that the previous is successful, then we get some information of the login user int ID = 100; string name = "shy520"; string sex = "female"; Person Person = New Person (Name, ID, SEX); / / Load user information to the cache cacheManager logincacache = cachefactory.getcacheManager (); logincache.add (id.tostring (), person); // Remove the login user information from the cache // pay attention to force conversion type Person Outperson = (Person ); id.tostring ()); assert.arequal (outperson.sex, sex); assert.Arequal (outperson.name, name); assert.Arequal (OutPerson.ID, ID);} 3.Flushing the Cache (Clealed Cache) will inevitably take a clear cache, you will show how to empty the cache below.
[TestMethod]
public
Void
Flushcache ()
{String name = "SHY520"; CacheManager TestFlushCache = CacheFactory.GetCacheManager (); TestFlushCache.Add ( "1", name); // empty the cache TestFlushCache.Flush (); int i = TestFlushCache.Count; Assert.AreEqual (0 , i);
4.Removing Items from The Cache This section describes how to remove a certain item in the cache, as follows:
[TestMethod]
public
Void
REMOVEITEMOFCACHE ()
{String Name = "Shy520"; cacheManager testflushcache = cachefactory.getCacheManager (); testflushcache.add ("1", name); // From the cache, the key = 1 item testflushcache.Remove ("1"); int i = Testflushcache.count; assert.Areequal (0, i);
5. RETRIEVING ITEMS from The Cache This section describes how to take a value in the cache, you need to pay attention to the mandatory conversion of the return value type.
[TestMethod]
public
Void
GetDataFromcache ()
{String Name = "Shy520"; cacheManager testflushcache = cachefactory.getCacheManager (); testflushcache.add ("1", name); // According to the key value to get some of the cache, but pay attention to the type of mandatory Convert String RNAME = (String) Testflushcache.getdata ("1"); Assert.Arequal (Name, RNAME);} About the entry part of Caching Application Block, say so much, hope and friends who are learning Enterprise Library 2.0 exchange And I hope that people will not ask for suggestions.