Practical Tips for spring. Net 1-the use of Nhibernate Level 2 cache based on prevalence

Source: Internet
Author: User

What is level-2 cache?

The session of Nhibernate provides a level-1 cache. Each session loads the same ID twice and does not send two SQL statements to the database. However, once the session is closed, the first-level cache becomes invalid.

Compared with Session, sessionfactory also provides a caching mechanism.

Sessionfactory caches can be divided into built-in and external caches based on different functions and purposes.

Sessionfactory's built-in cache stores the ing metadata and predefined SQL statements. The ing metadata is a copy of the data in the ing file, the pre-defined SQL statement is derived from the ing metadata during the Nhibernate initialization phase.

Sessionfactory's built-in cache is read-only and ApplicationsProgramYou cannot modify the ing metadata and predefined SQL statements in the cache,

Therefore, sessionfactory does not need to synchronize built-in cache and ing files. The external cache of sessionfactory is a configurable plug-in. By default, sessionfactory does not enable this plug-in. The external cache data is a copy of the database data, and the external cache media can be memory or hard disk. The external cache of sessionfactory is also called the hibernate second-level cache.

The implementation principle of the second-level cache of Nhibernate is the same as that of the first-level cache, and the object is cached by map with ID as the key. Because the second-level cache of Nhibernate is used within the sessionfactory range and is a global cache, it is wider than the first-level cache and can be shared by all session objects.

Work content of level 2 Cache

The second-level cache of Nhibernate is the same as that of the first-level cache. It also caches object IDs. Therefore, the scope of the second-level cache is to query the objects obtained by ID.

The second-level cache can be summarized into the following parts:

When performing various condition queries, if the obtained result set is a collection of object objects, all data objects are put into the second-level cache by ID.

When nhib accesses the Data Object Based on the ID, it first searches for the data object from the session level-1 cache. If the session level-2 cache is not found and configured, it searches for the data object from the level-2 cache, if not, the database will be queried and the results will be put into the cache by ID.

When data is deleted, updated, or added, the cache is updated.

 

Cache Policy
Read-only, read-write, and nonstrict-read-write)

 

Here is an example of the second-level cache usage scenario: transactions are often used when operating the database. When you use the begin Tran transaction and do not execute commit or rollback immediately, you can query the data in the table. In this case, some data cannot be obtained and will be queried only when the transaction is completed. Long transactions may even experience a "timeout. (See)

In other words, when the database server encounters high concurrency, It is terrible to have no cache mechanism. The level-1 cache is only for sessions. To solve such problems, a global cache-level-2 cache is required. After the second-level cache is configured, the addition, deletion, and modification operations for queries and accompanying transactions are not affected.

 

The second-level cache of Nhibernate includes memcache, prevalence, sharedcache, syscache, syscache2, and velocity. This section describes how to use prevalence.

 

1. Add bamboo. Prevalence. dll, bamboo. Prevalence. util. dll, and nhib.pdf. Caches. Prevalence. dll.

Http://sourceforge.net/projects/nhcontrib/files/

 

2. Add nodes to the Nhibernate File<Cache Usage = "read-write "/>.

3. ConfigurationSpring. Data. nhib.pdf.LocalsessionfactoryobjectThe hibernateproperties attribute of the object:

(1) Add<Entry key = "cache. use_second_level_cache" value = "true"/>,Enable level-2 caching.

(2) join<Entry key = "Expiration" value = "300"/>,Sets the effective time of the second-level cache. The default value is 300 seconds.

(3) add<Entry key = "prevalencebase" value = "D: \ cache"/>,Set the storage location of physical cache files,

Note that relative paths are not supported currently. Otherwise, the development environment will be added to the IDE directory and system32/inetsrv/during deployment.

 

 

Hibernate-Mapping

< Hibernate-Mapping Xmlns = "Urn: nhibernate-mapping-2.2" Assembly = "Domain" Namespace = "Domain" >
< Class Name = "Domain. Department, domain" Table = "T_department" Lazy = "True"   >

< Cache Usage = "Read-write"   />

< ID Name = "Dimension mentid" Column = "Dimension mentid" Type = "Int32"   >
< Generator Class = "Native"   />
</ ID >

< Property Name = "Name" Type = "String" >
< Column Name = "Name" Length = "50" Not-Null = "True" > </ Column >
</ Property >

< Bag Name = "Personlist" Inverse = "True" Lazy = "True" Generic = "True" Cascade = "All-delete-Orphan" Table = "T_person" >
< Cache Usage = "Read-write"   />
< Key Column = "Dimension mentid" Foreign-Key = "Fk_person_department" />
< One-to-least Class = "Domain. Person, domain"   />
</ Bag >

</Class>
</Hibernate-Mapping>

 

 

 

Nhib1_configuration

<! -- Nhib1_configuration -->
< Object ID = "Nhibernatesessionfactory" Type = "Spring. Data. nhib.pdf. localsessionfactoryobject, spring. Data. nhibernate21" >
< Property Name = "Dbprovider" Ref = "Dbprovider" />
< Property Name = "Mappingassemblies" >
< List >
< Value > Domain </ Value >
</ List >
</ Property >
< Property Name = "Hibernateproperties" >
< Dictionary >
< Entry Key = "Hibernate. Connection. provider" Value = "Nhib.pdf. Connection. driverconnectionprovider" />
< Entry Key = "Dialect" Value = "Nhibect. dialect. sqlitedialect" />
< Entry Key = "Connection. driver_class" Value = "Nhib.pdf. Driver. sqlite20driver" />
<! -- <Entry key = "dialect" value = "Nhibernate. dialect. mssql2005dialect"/>
<Entry key = "hibernate. Connection. driver_class" value = "nhib.pdf. Driver. sqlclientdriver"/> -->
< Entry Key = "Use_outer_join" Value = "True" />
< Entry Key = "Show_ SQL" Value = "True" />
< Entry Key = "Hbm2ddl. Auto" Value = "Update" />
< Entry Key = "Query. substitutions" Value = "True 1, false 0, yes 'y', no 'n '" />
< Entry Key = "Proxyfactory. factory_class" Value = "Nhibernate. bytecode. linfu. proxyfactoryfactory, Nhibernate. bytecode. linfu" />

< Entry Key = "Cache. use_second_level_cache" Value = "True" />
< Entry Key = "Cache. default_expiration" Value = "300" />
< Entry Key = "Prevalencebase" Value = "Cache"   />
< Entry Key = "Expiration" Value = "300"   />
< Entry Key = "Cache. provider_class" Value = "Nhib.pdf. Caches. Prevalence. prevalencecacheprovider, nhib.pdf. Caches. Prevalence" />
</ Dictionary >
</ Property >

<! --Provides integation with spring's declarative transaction management features-->
<PropertyName= "Exposetransactionawaresessionfactory"Value= "True" />

</Object>

 

 

 

OK. The second-level cache can be implemented in the previous three steps.

 

Code download

 

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.