Hibernate & lt; Level 2 Cache & gt; and hibernate Level 2 Cache

Source: Internet
Author: User

Hibernate <second-level cache> and hibernate second-level cache

Secondary cache:

Definition:

1. The second-level cache is called the process-level cache or sessionFactory-level cache. The second-level cache can be shared by all sessions.

2. the life cycle of the second-level cache is the same as that of sessionFactory (sessionFactory needs to be set in configuration). sessionFactory can manage the second-level cache.

3. the data in the second-level cache is put into the first-level cache. That is to say, the second-level cache is the same as that in the first-level cache. It is also a storage object and does not execute a cache policy on common attributes, the difference is that the second-level cache can share different lifecycles.

 

The second-level cache must be configured to take effect:

1. Introduce a third-party package ehcache

2. Configure in hibernate. cfg:

We need to configure three parts: one is provided by the driver, the other is to enable the second-level cache, and the third is to configure the entity class to be cached.

1 <! -- Level 2 cache driver provided --> 2 <property name = "hibernate. cache. provider_class"> org. hibernate. cache. EhCacheProvider </property> 3 <! -- Level 2 cache --> 4 <property name = "hibernate. cache. use_second_level_cache"> true </property> 5 6 <! -- Cache configuration --> 7 <class-cache usage = "read-only" class = "entity. Grade"/> 8 <! -- The object associated with grade --> 9 <class-cache usage = "read-only" class = "entity. student "/> 10 <collection-cache usage =" read-only "collection =" entity. grade. stu "/>

 

Note:

1. Because hibernate. cache. use_second_level_cache is enabled by default, we can also omit it.

2. Common cache policies: read-only/read-write

3. cache principle: Use cache when reading is much larger than writing

 

When Level 2 cache is enabled in configuration:

When the service does not need second-level cache:

HibernateUtils. getSessionFactory. evict (object. class );

Or

HibernateUtils. getSessionFactory. evict (object. class, 1); // specify a specific object

When we do not want the query results to be saved to the second-level cache:

Session. setCacheMode (CacheMode. IGNRE); // cancel the interaction between the level-1 cache and the level-2 cache, so that the current sesison cannot store data in the level-2 cache.

Supplement:

I mentioned in my notes yesterday that big data cache processing. When we enable the second-level cache, session. flush () session. clear () can only clear session-level caches. How to Handle sessionFactory-level/second-level caches:

Here I learned a method in the video:

Cancel the interaction between Level 1 cache and level 2 cache before saving or this query.

    

      

 

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.