NHibernate also provides an option for cache configuration with the NHibernate.Cfg. Loquacious namespace
. In this recipe, I'll show you how to configure the second level cache with code.
NHibernate.Caches.SysCache.dll
to your solution's Lib folder.<section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler, NHibernate.Caches.SysCache" />
<syscache> <cache region="hourly" expiration="60" priority="3" /> </syscache>
Program.cs
, add the following using statements:using System; using Eg.Core; using NHibernate.Caches.SysCache; using NHibernate.Cfg; using NHibernate.Cfg.Loquacious; using Environment = NHibernate.Cfg.Environment;
Program.cs
, add the following method:static void ConfigureCaching(Configuration nhConfig) { nhConfig .SetProperty(Environment.UseSecondLevelCache, "true") .SetProperty(Environment.UseQueryCache, "true") .Cache(c => c.Provider<SysCacheProvider>()) .EntityCache<Product>(c => { c.Strategy = EntityCacheUsage.Readonly; c.RegionName = "hourly"; }) .EntityCache<ActorRole>(c => { c.Strategy = EntityCacheUsage.Readonly; c.RegionName = "hourly"; }) .EntityCache<Movie>(c => c.Collection( movie => movie.Actors, coll => { coll.Strategy = EntityCacheUsage.Readonly; coll.RegionName = "hourly"; })); }
Main
:var nhConfig = new Configuration().Configure(); ConfigureCaching(nhConfig); var sessionFactory = nhConfig.BuildSessionFactory(); Console.WriteLine("NHibernate cache configured!"); Console.ReadKey();
In this recipe, we use the NHibernate.Cfg.Loquacious
namespace to configure the second level cache. Our configuration is identical to the one used in the previous recipe, Configuring the cache. The relevant items from the XML configuration are as follows:
<property name="cache.use_second_level_cache"> true </property> <property name="cache.use_query_cache"> true </property> <property name="cache.provider_class"> NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache </property> <class-cache class="Eg.Core.Product, Eg.Core" region="hourly" usage="read-only"/> <class-cache class="Eg.Core.ActorRole, Eg.Core" region="hourly" usage="read-only"/> <collection-cache collection="Eg.Core.Movie.Actors" region="hourly" usage="read-only"/>
We begin by setting cache.use_second_level_cache
and cache.use_query_cache
to true with the following code:
nhConfig .SetProperty(Environment.UseSecondLevelCache, "true") .SetProperty(Environment.UseQueryCache, "true")
We set cache.provider_class
with the following code:
nhConfig .Cache(c => c.Provider<SysCacheProvider>())
We configure the class cache for our Product hierarchy and ActorRole entities with the following code:
nhConfig .EntityCache<Product>(c => { c.Strategy = EntityCacheUsage.Readonly; c.RegionName = "hourly"; }) .EntityCache<ActorRole>(c => { c.Strategy = EntityCacheUsage.Readonly; c.RegionName = "hourly"; })
Finally, we configure the collection cache for our Actors collection with the following code:
nhConfig .EntityCache<Movie>(c => c.Collection( movie => movie.Actors, coll => { coll.Strategy = EntityCacheUsage.Readonly; coll.RegionName = "hourly"; }));
Notice how we call Collection(), passing an expression for our Actors collection, as well as the settings for our collection cache.