Click here to Skip to main content
15,884,836 members
Articles / Web Development / ASP.NET

Generic Cache Manager

Rate me:
Please Sign up or sign in to vote.
2.25/5 (5 votes)
2 Feb 2007CPOL2 min read 38.4K   204   11   2
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 :)

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:

XML
</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.

C#
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:

C#
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)


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

Comments and Discussions

 
Generalnot update my cache data immediately Pin
osmansonmez5-Oct-10 1:03
osmansonmez5-Oct-10 1:03 
Hi. i use cache manager and i use IIS 6.0 and asp.net 2.0. i have a problem about cache manager. i use cache manager and save my data in the memory. but i always update the cache data. update data in x class after that if i retreive data from another class i didnt see the changes. i see changes some time later. what can i do. if i use asp.net web server i didnt see same issue.
GeneralSlight confusion Pin
JHubSharp2-Feb-07 23:48
JHubSharp2-Feb-07 23: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.