Enterprise Library Caching without configuration#

I've been using Enterprise Library 2.0 quite a lot lately, and although the configuration part is truly excellent and easy to use, sometimes you just want to use some block without going through configuration. In my case, I wanted to use a cache somewhere in a piece of framework code without requiring users of that code to add anything "magic" to their application configuration to make it work. So with a little help of Brian Button, I cooked up the following code to create a standard CacheManager with an in-memory backing store:

DictionaryConfigurationSource internalConfigurationSource = new DictionaryConfigurationSource();
CacheManagerSettings settings = new CacheManagerSettings();
internalConfigurationSource.Add(CacheManagerSettings.SectionName, settings);
CacheStorageData storageConfig = new CacheStorageData("Null Storage", typeof(NullBackingStore));
settings.BackingStores.Add(storageConfig);
CacheManagerData cacheManagerConfig = new CacheManagerData("CustomCache", 60, 1000, 10, storageConfig.Name);
settings.CacheManagers.Add(cacheManagerConfig);
settings.DefaultCacheManager = cacheManagerConfig.Name;
CacheManagerFactory cacheFactory = new CacheManagerFactory(internalConfigurationSource);
CacheManager customCache = cacheFactory.CreateDefault();

So this piece of code in fact creates the configuration objects directly and passes them on to the CacheManagerFactory to use so it doesn't go about reading any configuration files. Pretty simple once you get the hang of it, and pretty powerful for the cases you want to use Enterprise Library "behind the scenes".

Check out Brian's blog for an example on Exception Handling without using an external configuration file. By the way: thanks for all your work on EntLib and all the best in your career move, Brian!

Blog | Programming | .NET | EntLib | Samples
Comments are closed.
All content © 2010, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 350
This Year: 4
This Month: 2
This Week: 2
Comments: 526
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In