01-08-01 "Nhibernate (version 3.3.1.4000) in and out of the lake" first-level cache in Nhibernate

Source: Internet
Author: User

The scope of the cache?

1. Scope of transactions

Transaction-scoped caches can only be accessed by the current transaction, each transaction has its own cache, and the data in the cache is usually in the form of an interrelated object. The lifecycle of a cache relies on the life cycle of a transaction, and the lifetime of the cache ends only when the transaction ends. The transaction-scoped cache uses memory as the storage medium, The first-level cache belongs to the transaction scope.

2. Scope of application

The application's cache can be accessed by all transactional shares within the scope of the application. The lifecycle of a cache relies on the life cycle of the app, and the lifecycle of the cache ends only when the app ends. Application-scoped caches can use either memory or hard disk as storage media, and level two caches are application-wide.

3. Cluster scope

In a clustered environment, the cache is shared by processes of one machine or multiple machines, and the data in the cache is replicated to each process node in the clustered environment, where the data in the cache is guaranteed to be consistent through remote communication, and the data in the cache is usually in the form of loose data for the object.

-------------------------------------------------------------------

The session cache is a first-level cache. is a transaction-scoped cache

The sessionfactory cache is divided into built-in caches and external caches. The built-in cache holds the data contained in some of the collection properties of the Sessionfactory object (mapping elements and predefined SQL statements, etc.), and for Applications, It is read-only. The external cache holds a copy of the database data, which acts like a first-level cache. Level two cache in addition to memory as storage media, you can also choose the hard disk and other external storage devices

Precautions:

1. Session level cache, which is the same as session state. It has the same life cycle as the session. The session is destroyed, and it is destroyed at the same time; management cache, first-level cache cannot be canceled, two methods managed, clear (), evict ()

2. Two sessions cannot share a first-level cache, as it will be accompanied by the creation and destruction of the session's life cycle;

3. The session cache is an entity-level cache, which is used only when querying the object level, and does not use the first-level cache if you use HQL and SQL are query attribute levels! Remember!!!!

4. Understand the difference between get () and load () in the first level cache

Use the Isession.get () method to immediately save the object instance to the cache.

Using the Isession.load () method only loads the ID of the entity first.

When you need to use a property other than the entity's ID, access the database to save the instance in the cache.

-------------------------------------------------------------------------------------

NHibernate Level Cache Management

NHibernate for us to provide a first-level cache, so we want to explicitly manage the ISession cache, what to do? The ISession interface gives us some ways to explicitly manage a first-level cache.

Isession.evict (object): Removes the specified instance from the session cache.

Isession.clear (): Clears all caches in the session.

Isession.contains (object): Checks whether the cache contains the specified instance.

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.