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.
NHibernate.Caches.SysCache
using NuGet Package Manager Console.CachingWithCode
to the project.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 { } }
Recipe
, add the following method:protected override void Configure(Configuration nhConfig) { nhConfig .Cache(x => { x.Provider<SysCacheProvider>(); x.UseQueryCache = true; }) .EntityCache<Product>(c => { c.Strategy = EntityCacheUsage.ReadWrite; c.RegionName = "hourly"; }) .EntityCache<ActorRole>(c => { c.Strategy = EntityCacheUsage.ReadWrite; c.RegionName = "hourly"; }) .EntityCache<Movie>(c => c.Collection( movie => movie.Actors, coll => { coll.Strategy = EntityCacheUsage.ReadWrite; coll.RegionName = "hourly"; })); }
Recipe
, add the two following methods:protected override void Run(ISessionFactory sessionFactory) { ShowMoviesBy(sessionFactory, "Steven Spielberg"); ShowMoviesBy(sessionFactory, "Steven Spielberg"); } private void ShowMoviesBy(ISessionFactory sessionFactory, string director) { using (var session = sessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) { var movies = session.QueryOver<Movie>() .Where(x => x.Director == director) .Cacheable() .List(); Show("Movies found:", movies); tx.Commit(); } } }
CachingWithCode
recipe.cache.provider_class
) but also specify that the query cache should be enabled (cache.use_query_cache
):.Cache(x => { x.Provider<SysCacheProvider>(); x.UseQueryCache = true; })
cache.use_second_level_cache
).Product
hierarchy and ActorRole
entities with the following code:.EntityCache<Product>(c => { c.Strategy = EntityCacheUsage.ReadWrite; c.RegionName = "hourly"; }) .EntityCache<ActorRole>(c => { c.Strategy = EntityCacheUsage.ReadWrite; c.RegionName = "hourly"; })
Actors
collection with the following code:nhConfig .EntityCache<Movie>(c => c.Collection( movie => movie.Actors, coll => { coll.Strategy = EntityCacheUsage.ReadWrite; coll.RegionName = "hourly"; }));
Note how we call Collection()
, passing an expression for our Actors
collection, as well as the settings for our collection cache.
Just as with XML mappings, it's perfectly possible to specify the class-level cache configuration in coded mappings.
public class ProductMapping : ClassMapping<Product> { public ProductMapping() { … Cache(c => { c.Usage(CacheUsage.ReadWrite); c.Region("hourly"); }); } } public class MovieMapping : SubclassMapping<Movie> { public MovieMapping() { ... Set(x => x.Actors, x => { x.Cache(c => { c.Usage(CacheUsage.ReadWrite); c.Region("hourly"); }); ... } , x => x.OneToMany()); } }
public class ProductMap : ClassMap<Product> { public ProductMap() { ... Cache.ReadWrite().Region("hourly"); } } public class MovieMap : SubclassMap<Movie> { public MovieMap() { ... HasMany(x => x.Actors) .AsSet() .Cache.ReadWrite().Region("hourly"); } }
3.149.250.11