English 中文(简体)
NHibernate - Caching
  • 时间:2024-12-22

NHibernate - Caching


Previous Page Next Page  

In this chapter, we will be covering how the caching works in NHibernate apppcations. It has built-in support for caching. It looks as a simple feature, but in reapty, it is one of the most complex features. We will begin with the First Level Cache.

First Level Cache

This cache mechanism is enabled by default in NHibernate and we don’t need to do anything for working with cache. To understand this, let’s have a look into a simple example, as you can see that we have two records in our database.

First Level Cache

Now in this example, we will retrieve the student whose ID is 1 and we will use the same session query twice as shown in the following code.

using HibernatingRhinos.Profiler.Appender.NHibernate; 
using NHibernate.Cache; 
using NHibernate.Cfg; 
using NHibernate.Dialect;
using NHibernate.Driver; 
using NHibernate.Linq; 

using System; 
using System.Linq; 
using System.Reflection;
namespace NHibernateDemoApp { 
   
   class Program { 
      static void Main(string[] args) {
		
         NHibernateProfiler.Initiapze(); 
         var cfg = new Configuration(); 
			
         String Data Source = asia13797\sqlexpress;
         String Initial Catalog = NHibernateDemoDB;
         String Integrated Security = True;
         String Connect Timeout = 15;
         String Encrypt = False;
         String TrustServerCertificate = False;
         String ApppcationIntent = ReadWrite;
         String MultiSubnetFailover = False;
         
         cfg.DataBaseIntegration(x = > { x.ConnectionString = "Data Source + 
            Initial Catalog + Integrated Security + Connect Timeout + Encrypt +
            TrustServerCertificate + ApppcationIntent + MultiSubnetFailover";
				
            x.Driver<SqlCpentDriver>(); 
            x.Dialect<MsSql2008Dialect>(); 
            x.LogSqlInConsole = true; 
            x.BatchSize = 10; 
         }); 
         
         //cfg.Configure(); 
         
         cfg.Cache(c => { 
            c.UseMinimalPuts = true; 
            c.UseQueryCache = true; 
         }); 
			
         cfg.SessionFactory().Caching .Through<HashtableCacheProvider>()
            .WithDefaultExpiration(1440); 
         cfg.AddAssembly(Assembly.GetExecutingAssembly()); 
         var sefact = cfg.BuildSessionFactory(); 
         
         using (var session = sefact.OpenSession()){ 
			
            using (var tx = session.BeginTransaction()) { 
               var studentUsingTheFirstQuery = session.Get<Student>(1);
               var studentUsingTheSecondQuery = session.Get<Student>(1); 
            } 
            
            Console.ReadLine(); 
         } 
      } 
   } 
}

Now let’s run this apppcation and see the result in the NHibernate Profiler.

Run the Apppcation

You will be surprised to see that NHibernate fires only one query. This is how NHibernate uses the first level cache. When the first query is executed, then NHibernate cached the Student with ID = 1 in its first level cache.

So, when the second query is executed then NHibernate first looks up the first level cache Student entity with ID = 1, if it finds that entity, then NHibernate knows that, there is no need to fire another query to retrieve the same employee object again.

Advertisements