Configuring the cache with code

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.

Getting ready

  1. Complete the Configuring NHibernate with App.config recipe from Chapter 1.
  2. Download the NHibernate Caches binary files from SourceForge at http://sourceforge.net/projects/nhcontrib/files/.
  3. Extract NHibernate.Caches.SysCache.dll to your solution's Lib folder.

How to do it...

  1. In the configSections element of App.config, declare a section for our cache provider's configuration:
    <section name="syscache"
        type="NHibernate.Caches.SysCache.SysCacheSectionHandler, 
              NHibernate.Caches.SysCache" />
  2. After the hibernate-configuration section, add the syscache section we just declared:
    <syscache>
      <cache region="hourly" expiration="60" priority="3" />
    </syscache>
  3. In 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;
  4. In 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";
            }));
    }
  5. Use the following code in Main:
    var nhConfig = new Configuration().Configure();
    ConfigureCaching(nhConfig);
    var sessionFactory = nhConfig.BuildSessionFactory();
    Console.WriteLine("NHibernate cache configured!");
    Console.ReadKey();
  6. Build and run your application. You will see NHibernate cache configured!

How it works...

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.

See also

  • Configuring the cache
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset