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:
</configuration />
="1.0"="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)
{
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