Click here to Skip to main content
13,295,178 members (70,018 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 2 Feb 2007

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.


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 :)

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" ?>
        <section name="cachingConfiguration" 

                 Microsoft.Practices.EnterpriseLibrary.Caching" />
    <cachingConfiguration defaultCacheManager="Default Cache Manager">
        <add name="inMemory" 

                Microsoft.Practices.EnterpriseLibrary.Caching" />
        <add name="Default Cache Manager" expirationPollFrequencyInSeconds="60" 


           numberToRemoveWhenScavenging="10" backingStoreName="inMemory" />

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.


  • 1.0.0 - Initial release.


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


About the Author

Architect Archon Gnosis
Australia Australia
Just a developer with a little bit of knowledge, constantly learning.

You may also be interested in...


Comments and Discussions

Generalnot update my cache data immediately Pin
osmansonmez5-Oct-10 2:03
memberosmansonmez5-Oct-10 2:03 
GeneralSlight confusion Pin
Shawn H3-Feb-07 0:48
memberShawn H3-Feb-07 0:48 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 3 Feb 2007
Article Copyright 2007 by DanielBrownAU
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid