Geeks With Blogs
Rohit Gupta Engaging talk on Microsoft Technologies ....My Resume

For this we need to setup a AppFabric Cluster on-premise…. or setup a Caching Worker role (or colocated Caching) on Azure.

The steps for the same are mentioned in this blog post:

Configure MVC app to use onpremise AppFabric Cache or use Azure Caching.

So once we have AppFabric Cache setup. to use this AppFabric Cache as the second level Cache in NHibernate is very simple:

We need to add a nuget package as shown below (NHibernate second-level cache provider):

image

One this has been added, this automatically adds the following configuration to web.config:

   1: <configSections>
   2:   <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
   3:   <section name="AppFabricProviderSettings" type="NHibernate.Caches.AppFabric.AppFabricProviderSettings, NHibernate.Caches.AppFabric" />
   4: </configSections>
   5:  
   6: <AppFabricProviderSettings CacheType="Named" NamedCacheTypeRegionName="nhibernate" NamedCachesMustExist="false" LockTimeout="30000" LocksRegionName="Locks" />

For more information get the details from this blog (or search the following term in google: “NHibernate second level cache with AppFabric “ ):

https://github.com/SimonTaylor/NHibernate.Caches.AppFabric

http://www.sharpcoder.co.uk/post/2012/01/04/NHibernateCachesAppFabric.aspx

http://www.sharpcoder.co.uk/post/2012/01/11/NHibernateCachesAppFabric-Documentation.aspx

Now in the NHibernate configuration we need to enable second level cache and use NHibernate.Caches.AppFabric as the second level cache.

Here is the code that does the same:

   1: var cacheToUse = ConfigurationManager.AppSettings["cacheToUse"] ?? "InMemoryCache";
   2: var cfg = Fluently.Configure()
   3:     .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)
   4:                   .Provider("NHibernate.Connection.DriverConnectionProvider, NHibernate")
   5:                   .Dialect<CustomMsSql2008Dialect>()
   6:                   .Driver<SqlAzureClientDriver>()
   7:                   .ShowSql())
   8:     .Cache(x =>
   9:                {
  10:                    if (cacheToUse == "InMemoryCache")
  11:                        x.UseSecondLevelCache().ProviderClass<HashtableCacheProvider>().UseMinimalPuts();
  12:                    else
  13:                        x.UseSecondLevelCache().ProviderClass<NHibernate.Caches.AppFabric.AppFabricProvider>().UseMinimalPuts();
  14:                })
  15:     .CurrentSessionContext("NHibernate.Context.ThreadStaticSessionContext, NHibernate")
  16:     .Mappings(m =>
  17:     {
  18:         m.FluentMappings.Conventions.Setup(x => x.Add(AutoImport.Never()));
  19:         m.FluentMappings.Conventions.AddFromAssemblyOf<ClassConvention>();
  20:         foreach (var assembly in assemblies)
  21:         {
  22:             m.FluentMappings.AddFromAssembly(assembly);
  23:         }
  24:     })
  25:     .ExposeConfiguration(c =>
  26:                              {
  27:                                  c.SetProperty(Environment.TransactionStrategy, typeof(ReliableAdoNetWithDistributedTransactionFactory).AssemblyQualifiedName);
  28:                                  NHibernateCfg = c;
  29:                                  c.LinqToHqlGeneratorsRegistry<ExtendedLinqtoHqlGeneratorsRegistry>();
  30:                              })
  31:     .BuildConfiguration();
Posted on Saturday, March 30, 2013 6:01 AM | Back to top


Comments on this post: Using AppFabric Cache as second level Cache for NHibernate

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Rohit Gupta | Powered by: GeeksWithBlogs.net