Persistent cache management with Managed Esent

I have a program where components could depend from cache, implemented by ICache interface.


I have really different types of cache, for data that needs to be temporary stored in memory I use a cache component based on enterprise library, but there are components that logically needs a cache that will survive after the process will exit.

The first and obvious solution was to create a very simple component that stores cache data on application database, this is especially useful because program running from different computer could share the same cache with no problem. (program was born when Velocity still not exists, and we decided to keep everything really simple)

The main drawback to this approach is the traffic to the database, and we have some specific part of the application based on windows client that communicates over WCF, and deployed outside our organization.

The first solution was to deploy a sql server express with the application, and use the very same cache component based on database, but this solution has some drawback, because we want to keep the installer simple, and not all users likes to have a SQLExpress on their machine, so we need a different solution based on file system.

A simple and quick solution was using a PersistentDictionary based on Esent. To keep performances good, I stored all the data needed to query the cache (Key, Timestamp,..) in a separate PersistentDictionary.

private class EsentDb
private PersistentDictionary<String, CacheToken> _keyDatabase;
private PersistentDictionary<String, String> _valueDatabase;

Code is really simple, because I rely on object serialization to store complex object into a string, with few lines of code and some test now I have a persistent cache that requires no other component, thanks to ESENT.