2-level cache for Nhibernate Performance

Source: Internet
Author: User

1. Learning Objectives
Learn more about the working mechanism and usage of level 2 cache in nhive.
2. Development Environment and necessary preparation
Development Environment: Windows 2003, Visual Studio. NET 2005, SQL Server 2005 Developer Edition
Prerequisites: the first five nhibana Learning Series: Start of nhibana learning-1. Start of nhibana learning-to-one, and start of nhibana learning-to-quit, 3-Level Association of nhibana Learning (ternary associations), 1 improvement in performance of nhibana Learning
3. Pre-school Analysis
When buying a computer, we often look at the CPU configuration. One metric to measure CPU performance is Level 2 Cache. The larger the Level 2 cache, the higher the CPU performance. This is why, as we all know, the CPU processing speed is much faster than the operation in the memory (memory). This will inevitably cause a certain bottleneck effect when the system is running, adding a processing speed between memory and CPU to the media between memory and processor-high-speed cache can play a balanced role, each time the CPU tries to read data from the cache first, if not, read from memory again. Generally, the CPU hit rate for high-speed cache is above 90%, which greatly improves the performance. Adding a high-speed cache between memory and disk can also improve the new performance of the system and reduce the number of I/O operations on the disk.
4. How to enable the second-level cache in nhib.pdf
The isession object in nhision already has a level-1 cache, but at the isession level, we can see from the previous section that the isession object should be released as soon as possible, the level-1 cache that depends on it will be destroyed when it is destroyed, so the level-1 cache hit rate is relatively low. The isessionfactory object is recommended for infrequent creation, which is very suitable for Cache Usage. The second-level cache here is exactly at the isessionfactory level.
To enable Level 2 caching in nhib.pdf, add the following in hibernate. cfg. xml: Code :

< Property Name = "Hibernate. cache. provider_class" > Nhib.pdf. Caches. syscache. syscacheprovider, nhib.pdf. Caches. syscache </ Property >
< Property Name = "Expiration" > 120 </ Property >

Nhib.pdf. Caches. syscache. syscacheprovider, nhib.pdf. Caches. syscache can also be replaced with nhib.pdf. caches.Prevalence. Prevalencecacheprovider, nhib.pdf. Caches. Prevalence, representing the cache implementation class. In the bin directory, there are two DLL
Nhib.pdf. Caches. syscache. dll, nhib.pdf. Caches. Prevalence. dll, which one to copy to the applicationProgramUnder the bin directory
Expiration indicates the cache expiration time, in seconds
After setting, you also need to configure the second-level cache policy in the object's ing file. For example, in user. HBM. XML, configure the following:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Hibernate-Mapping Xmlns = "Urn: nhibernate-mapping-2.2" >
< Class Name = "Nhibernatesample1.user, nhibernatesample1" Table = "Users" Lazy = "False" >
< Cache Usage = "Read-write" />
< ID Name = "ID" Column = "ID" Unsaved-Value = "0" >
< Generator Class = "Native"   />
</ ID >
< Property Name = "Name" Column = "Name" Type = "String" Length = "64" Not-Null = "True" Unique = "True" > </ Property >
< Property Name = "PWD" Column = "PWD" Type = "String" Length = "64" Not-Null = "True" > </ Property >
< Allow-to-one Name = "Role" Class = "Nhibernatesample1.role, nhibernatesample1" Column = "Roleid" > </ Allow-to-one >
</ Class >
</ Hibernate-Mapping >

Nhibernatehelper. CS

Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using Nhib.pdf;
Using Nhib.pdf. cfg;


Namespace Webapp
{
Public   Sealed   Class Nhibernatehelper
{
Private   Const   String Currentsessionkey =   " Nhib.pdf. current_session " ;
Private   Static   Readonly Isessionfactory sessionfactory;

Static Nhibernatehelper ()
{< br> string export path = @" E: \ My project \ nhib1_study \ simple4 \ nhibernatestudy1 \ nhibernatesample1 \ hibernate. cfg. XML " ;
sessionfactory = New nhib.pdf. cfg. configuration (). configure (export path ). buildsessionfactory ();
}

Public   Static Isession getcurrentsession ()
{
Httpcontext Context = Httpcontext. Current;
Isession currentsession = Context. items [currentsessionkey] As Isession;

If (Currentsession =   Null )
{
Currentsession=Sessionfactory. opensession ();
Context. items [currentsessionkey]=Currentsession;
}

Return Currentsession;
}

Public   Static   Void Closesession ()
{
Httpcontext Context = Httpcontext. Current;
Isession currentsession = Context. items [currentsessionkey] As Isession;

If (Currentsession =   Null )
{
//No current session
Return;
}

Currentsession. Close ();
Context. Items. Remove (currentsessionkey );
}

Public   Static   Void Closesessionfactory ()
{
If (Sessionfactory ! =   Null )
{
Sessionfactory. Close ();
}
}
}

}

Page code: System. Diagnostics. Stopwatch SW =   New System. Diagnostics. Stopwatch ();
Sw. Start ();
Isession session = Nhibernatehelper. getcurrentsession ();
Itransaction tra = Session. begintransaction ();
Session. Load ( Typeof (Nhibernatesample1.user ), 1 );
Tra. Commit ();
Sw. Stop ();
Response. Write (SW. elapsedticks + " <Br> " );
Sw. Reset ();
Sw. Start ();
Session = Nhibernatehelper. getcurrentsession ();
Tra = Session. begintransaction ();
Session. Load ( Typeof (Nhibernatesample1.user ), 1 );
Tra. Commit ();
Sw. Stop ();
Response. Write (SW. elapsedticks +   " <Br> " );
Sw. Reset ();
Sw. Start ();
Session = Nhibernatehelper. getcurrentsession ();
Session. Close ();
Sw. Stop ();
Response. Write (SW. elapsedticks +   " <Br> " );

Run for the first time and use the SQL event probe. The result is

From the SQL statement in, the first time the user data is added from the database
Second

It can be seen that the second time the user object is loaded, the data is not obtained from the data, but the role information that is not set to cache is obtained from the user.
Of course, when using the cache, the cache will not know that the entity stored by another process has changed. You should set up some policies to promptly update the cache snapshot. When the isessionfactory is destroyed, the second-level cache is also destroyed, which is also the application time. It's a bit difficult to write this article today.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.