Configuring the second-level cache with code
NHibernate also provides an option for cache configuration with the NHibernate.Cfg.Loquacious
namespace. In this recipe, we'll show you how to configure the second-level cache with code.
Getting ready
Complete the Getting Ready instructions at the beginning of. Chapter, Queries.
How to do it...
Add a reference to
NHibernate.Caches.SysCache
using NuGet Package Manager Console.Add a new folder named
CachingWithCode
to the project.Add a new class named
Recipe
to the folder:using NH4CookbookHelpers.Queries; using NH4CookbookHelpers.Queries.Model; using NHibernate; using NHibernate.Caches.SysCache; using NHibernate.Cfg; namespace QueryRecipes.CachingWithCode { public class Recipe : QueryRecipe { } }
In
Recipe
, add the following method:protected override void Configure(Configuration nhConfig) { nhConfig .Cache(x => { x.Provider<SysCacheProvider>(); x.UseQueryCache = true; }) .EntityCache<Product>(c =>...