Click here to Skip to main content
Click here to Skip to main content

Generic Cache Manager

, 2 Feb 2007
Rate this:
Please Sign up or sign in to vote.
A Cache Manager built using Generics and the Microsoft Enterprise Library Cache Application Block.

Introduction

Firstly, I'd like to point out that this is my first article, so any feedback is most welcome as I'm a long time reader and now plan to share some of my achievements with the community and become a long time poster to CodeProject Smile | :)

The Generic Cache Manger was built on top of the Microsoft Enterprise Library Caching Application Block, and provides a means to have a strongly typed cache manager which uses .NET 2.0 Generics. I decided to develop the generic cache manager from a much larger application I am working on. I needed a way to quickly cache objects but also wanted the flexibility which came with Generics. As I was already using the Microsoft Enterprise Library, I decided to use this as a basis and as such, open to the power and flexibility of the Microsoft Enterprise Library.

Using the code

In order to use the code, you must first update your web/app.config file with the Microsoft Enterprise Library Caching Application Block sections.

A quick sample is below of the app/web.config settings:

</configuration />
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="cachingConfiguration" 
           type="Microsoft.Practices.EnterpriseLibrary.Caching.
                 Configuration.CacheManagerSettings,
                 Microsoft.Practices.EnterpriseLibrary.Caching" />
    </configSections>
    <cachingConfiguration defaultCacheManager="Default Cache Manager">
    <backingStores>
        <add name="inMemory" 
          type="Microsoft.Practices.EnterpriseLibrary.Caching.
                BackingStoreImplementations.NullBackingStore, 
                Microsoft.Practices.EnterpriseLibrary.Caching" />
    </backingStores>
    <cacheManagers>
        <add name="Default Cache Manager" expirationPollFrequencyInSeconds="60" 
           maximumElementsInCacheBeforeScavenging="1000" 
           numberToRemoveWhenScavenging="10" backingStoreName="inMemory" />
    </cacheManagers>
    </cachingConfiguration>
</configuration>

The class for taking advantage of the Generic Cache Manager is named GenericCacheManager in the namespace DandTSoftware.GenericCacheManager (making it DandTSoftware.GenericCacheManager.GenericCacheManager).

Firstly, you must create an instance of the Generic Cache Manager using the type of the objects you wish to use.

The code below shows you how to create a Generic Cache Manager instance which only deals with strings. However, multiple types of different typed Generic Cache Managers point to the same central cache store.

GenericCacheManager<string> StringCacheManager = 
                            new GenericCacheManager<string>();

Adding objects into the cache.

To add an object into the cache store, simple call the AddToCache method. This method, along with all other methods which access the cache store, has both a string and integer override. Both serve the same purpose; however, dealing with objects with integer IDs often, I decided to just make a method to support integers as keys.

The GenericCacheManager also supports the creation of unique keys in the Cache Store. This is done by the private method MakeKey, which simply performs the following:

private string MakeKey(string ID)
{
    return string.Format("CACHEKEY_{0}_{1}", typeof(T).ToString(), ID);
}
private string MakeKey(int ID)
{
    // Abstracted to MakeKey(string)
    return this.MakeKey(ID.ToString());
}

Retrieving objects into the cache

To retrieve an item from the Cache Manager, the use of the method GetData() should be used.

Removing objects into the cache

To remove an item from the cache, use the method RemoveFromCache().

Now you have an instance of the Generic Cache Manager which, using Generics, expects and only deals with strings. Of course, any type is possible.

Points of interest

When you wish to construct the Generic Cache Manager, there is an overload for the constructor which accepts a boolean. I implemented this functionality due to the Microsoft Enterprise Library trying to use such resources which the ASP.NET user account does not have access to. This constructor instructs the Generic Cache Manager, and impersonates the identity of the user which the Application Pool is running.

History

  • 1.0.0 - Initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DanielBrownAU

Australia Australia
No Biography provided

Comments and Discussions

 
Generalnot update my cache data immediately Pinmemberosmansonmez5-Oct-10 1:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 3 Feb 2007
Article Copyright 2007 by DanielBrownAU
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid