Browse DevX
Sign up for e-mail newsletters from DevX


Cache In On the Enterprise Library Caching Block for .NET 2.0 : Page 2

Nearly every application needs to cache data. While you're probably familiar wth the caching functionality built into ASP.NET, the Enterprise Library Caching Block provides in-memory, file-based, or database caching storage for all your other .NET applications.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using the Caching Application Block
After installing the Enterprise Library, you still need to perform some setup steps to use the Caching Block successfully in a project. Follow the steps listed below:

  1. Add a reference to the Microsoft.Practices.EnterpriseLibrary.Common.dll and Microsoft.Practices.EnterpriseLibrary.Caching.dll assemblies to your solution using the "Add Reference" option and navigating to the <Drive Name>:\Program Files\Microsoft Enterprise Library January 2006\bin folder on the drive where you installed the Enterprise Library. If you decide to use the database as a caching store, you also need to add a reference to the Data Access Block (the Microsoft.Practices.EnterpriseLibrary.Caching.Data.dll).
  2. Add the necessary configuration entries to the app.config or web.config file. To this end, you add the <configSections> element under the root <configuration> element.
  3. <configSections> <section name="cachingConfiguration" type="Microsoft.Practices. EnterpriseLibrary.Caching.Configuration. CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary. Caching, Version=, Culture=neutral, PublicKeyToken=null"/> </configSections>

  4. Next, add the <cachingConfiguration> element directly under the root <configuration> element as shown below:
  5. <cachingConfiguration defaultCacheManager="Default Cache Manager"> <backingStores> <add name="inMemory" type="Microsoft.Practices. EnterpriseLibrary.Caching. BackingStoreImplementations. NullBackingStore, Microsoft.Practices. EnterpriseLibrary.Caching" />

    Figure 1. Enterprise Library Configuration Tool: The figure shows the in-memory cache configuration settings.
    </backingStores> <cacheManagers> <add name="Default Cache Manager" expirationPollFrequencyInSeconds = "60" maximumElementsInCacheBeforeScavenging ="1000" numberToRemoveWhenScavenging="10" backingStoreName="inMemory" /> </cacheManagers> </cachingConfiguration>
    The preceding settings show how to configure the cache to use an in-memory store by default. While you can add the settings manually, it's far easier to add them using the Enterprise Library Configuration Tool that ships with Enterprise Library. When viewed through the Enterprise Library Configuration tool the preceding settings look like Figure 1.

  6. Add an "Imports" (or using, in C#) line for the core namespace for the Caching Block, "Microsoft.Practices.EnterpriseLibrary.Caching" to your code.
Now you're ready to begin writing code that takes advantage of the Caching Block.

Thanks for your registration, follow us on our social networks to keep up-to-date