title: NHibernate which cache to use for WinForms application
body: <p>I have a C# WinForms application with a database backend (oracle) and use NHibernate for O/R mapping. I would like to reduce communication to the database as much as possible since the network in here is quite slow, so I read about second level caching. I found <a href="http://nhibernate.hibernatingrhinos.com/28/first-and-second-level-caching-in-nhibernate" rel="noreferrer">this quite good introduction</a>, which lists the following available cache implementations.</p>
  
  <p>I'm wondering <strong>which implementation I should use for my application</strong>.</p>
  
  <p>The caching should be simple, it should not significantly slow down the first occurrence of a query, and it should not take much memory to load the implementing assemblies. (With NHibernate and Castle, the application already takes up to 80 MB of RAM!)</p>
  
  <blockquote>
    <ul>
    <li>Velocity: uses Microsoft Velocity which is a highly scalable in-memory application cache for all kinds of data.</li>
    <li>Prevalence: uses Bamboo.Prevalence as the cache provider. Bamboo.Prevalence is a .NET implementation of the object prevalence concept brought to life by Klaus Wuestefeld in Prevayler. Bamboo.Prevalence provides transparent object persistence to deterministic systems targeting the CLR. It offers persistent caching for smart client applications.</li>
    <li>SysCache: Uses System.Web.Caching.Cache as the cache provider. This means that you can rely on ASP.NET caching feature to understand how it works.</li>
    <li>SysCache2: Similar to NHibernate.Caches.SysCache, uses ASP.NET cache. This provider also supports SQL dependency-based expiration, meaning that it is possible to configure certain cache regions to automatically expire when the relevant data in the database changes.</li>
    <li>MemCache: uses memcached; memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Basically a distributed hash table.</li>
    <li>SharedCache: high-performance, distributed and replicated memory object caching system. See here and here for more info</li>
    </ul>
  </blockquote>
  
  <p>My considerations so far were:</p>
  
  <ul>
  <li>Velocity seems quite heavyweight and overkill (the files totally take 467 KB of disk space, haven't measured the RAM it takes so far because I didn't manage to make it run, see below)</li>
  <li>Prevalence, at least in my first attempt, slowed down my query from ~0.5 secs to ~5 secs, and caching didn't work (see below)</li>
  <li>SysCache seems to be for ASP.NET, not for winforms.</li>
  <li>MemCache and SharedCache seem to be for distributed scenarios.</li>
  </ul>
  
  <p><strong>Which one would you suggest me to use?</strong> There would also be a built-in implementation, which of course is very lightweight, but the referenced article tells me that I "<em>(...) should never use this cache provider for production code but only for testing</em>."</p>
  
  <p><strong>Besides the question which fits best into my situation I also faced problems with applying them:</strong></p>
  
  <ul>
  <li><p>Velocity complained that "<em>dcacheClient" tag not specified in the application configuration file. Specify valid tag in configuration file,</em>" although I created an app.config file for the assembly and pasted the example from <a href="http://blogs.circlesource.com/2009/01/20/velocity-usages-and-its-advantages/" rel="noreferrer">this article</a>.</p></li>
  <li><p>Prevalence, as mentioned above, heavily slowed down my first query, and the next time the exact same query was executed, another select was sent to the database. Maybe I should "externalize" this topic into another post. I will do that if someone tells me it is absolutely unusual that a query is slowed down so much and he needs further details to help me.</p></li>
  </ul>