[NHibernate] Cache (nhibernate.caches)

Source: Internet
Author: User

Series Articles

[Nhibernate] Architecture

[NHibernate] Isessionfactory Configuration

[NHibernate] Persistence class (persistent Classes)

[NHibernate] O/R Mapping Basics

[NHibernate] Collection Class (collections) mappings

[NHibernate] Association mappings

[NHibernate] Parent/child

What is Nhibernate.caches?

Nhibernate.caches is NHibernate's add-on software, which is contributed by Kevin Williams (aka K-dub). The cache is a place where entities are saved (at first load), and once entered into the cache, they can be obtained without (again) querying the back of the store (database). This means that they can be loaded (or reloaded) faster.

The NHibernate session has an internal (first-level) cache that stores its entities. These caches are not shared, so their caches are destroyed when the session is destroyed. NHibernate provides a level two cache system, which works at the sessionfactory level. So it is shared by the same session as the Sessionfactory generation.

With each request, a session mode allows many sessions to concurrently access the same entity without having to access the database every time, thus improving performance.

Contributors make it possible to use different cache providers in NHibernate:

    • NHibernate.Caches.Prevalence makes it possible to use the underlying Bamboo.prevalence implementation as a cache provider. Open the file Bamboo.Prevalence.license.txt can see its license information, you can also access its site.
    • NHibernate.Caches.SysCache makes it possible for the underlying System.Web.Caching.Cache implementation to be a cache provider. This means that you can rely on the ASP. NET caching feature to understand how it works. To get more information, you can read Cachingapplicationdata (MSDN)
How do I use it?

Here are the steps to enable level two caching in NHibernate:

    • Select the cache provider you want to use and copy its assembly to your assembly path (NHibernate.Caches.Prevalence.dll or NHibernate.Caches.SysCache.dll)
    • to indicate which cache provider to use, in the NHibernate configuration file (you can add the following in the YourAssembly.exe.config or Web. config or. cfg.xml file):
 1  <  add  key  = " Hibernate.cache.provider_class "  value  = "XXX"  />   (1)  Span style= "Color:rgb (0, 128, 128); >2  <  add  =" Relativeexpiration "   

(1) "XXX" can be "nhibernate.caches.prevalence.prevalencecacheprovider,nhibernate.caches.prevalence" or " Nhibernate.caches.syscache.syscacheprovider,nhibernate.caches.syscache ".
(2) The value of Relativeexpiration is the number of seconds you want to cache each entity (this is two minutes).

Add <cache usage= "Read-write|nonstrict-read-write|read-only"/> (behind <class>) to the entity mappings you need to cache, and it is also a collection (bag,list , Map,set, ...) Provide support.

Note: The cache does not know about changes to the entities stored by another process (although the cache data expiration time is configured). When caches are built at the sessionfactory level, they are destroyed along with the Sessionfactory instance, so you must keep the sessionfactory present when you need to cache.

Summarize

This article is mainly about how to use caching in NHibernate. There's a place to be aware of:

The cache does not know about changes to the entities stored by another process (although the cache data expiration time is configured). When caches are built at the sessionfactory level, they are destroyed along with the Sessionfactory instance, so you must keep the sessionfactory present when you need to cache.

Examples are provided in subsequent articles, where only the content of the document is studied.

This article is from "NHibernate Chinese document"

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.