What do you know about hibernate cache?

Source: Internet
Author: User

First, we need to understand what cache is and what cache refers to as a temporary file swap zone. It indicates that he is not permanent. The cache is a device between the CPU and the primary storage. The read/write speed is faster than the master memory and slower than the CPU. Cache has the concept of hit rate. This is also easy to understand. The CPU is the place where data and computing data are processed. Data Reading does not come from memory or from cache. However, the cache is between the primary memory and the memory. So if the CPU can directly fetch data from the cache. The execution efficiency will be improved. If the data is not stored in the cache. Then you can know that the hit rate = the number of cache accesses/(the number of cache accesses + the number of master memory accesses ).

So hibernate introduces cache. You will know why. To improve efficiency. You must know that each access to data consumes a lot of resources, and the database is external, so the time will be slow. Suppose you want to query some data from the database (this result is very large. The second query is required. If there is no cache, this means you have to access the database twice. However, the query results only have one or two updates, or they are not updated at all. So if there is a cache at this time, you can put the first result in the cache. You can retrieve data from the cache for the second query. If you do not access the database again, this greatly improves the efficiency. Therefore, there are four prerequisites for Using Cache: first, the data accuracy is not very high, that is, you allow data errors. Second: the number of modifications is less than the number of reads and writes (the cache hit rate is high). Imagine if your first and second queries have different results,

First, you cannot find it in the cache, and then you can find it in the database. In this case, it is better not to cache the database directly. Third, data should be subject to exclusive control. If you are reading and writing the data, it is basically useless to modify the data. Fourth: the data capacity is smaller than the memory capacity, which is a serious injury, otherwise the memory will overflow,

Therefore, the cache of Hibernate is regarded as a map in disguise. In fact, it is also a map. First, how to implement the cache, the user queries the data for the first time, while returning the results, the results are also placed in the map. You must know that map is a set of key-value pairs. The key here is your hql statement, and value is your encapsulated useful information. If the hql statement is the same for the second query, Hibernate will find it in map first. If not, find it in the database. My understanding of the set is that the set is a container that can perform addition, deletion, modification, and query. Therefore, the process of removing map is the process of clearing the cache. Of course, the cache of Hibernate is far from as simple as a map, such as the effective time of cache and so on.

In hibernate, cache is classified into Level 1 cache and level 2 cache. The primary cache is session-level sharing. The primary operations include Save, update, saveorupdate, load, get, list, iterate, lock, evick, clear, contains, and flush. However, the primary cache function is small, so hibernate has two levels of cache. Generally, Hibernate is found in level 1 cache. If it cannot be found, it will be found in Level 2 cache. Level 2 cache, sessionfactory-level sharing. First, Hibernate must enable Level 2 Cache configuration in the configuration file.

 

Hibernate. cfg. xml

 

<Hibernate-configuration>

<Session-factory>

<! -- Set the provider class of the level-2 Cache plug-in ehcache -->

<Propertyname = "hibernate. cache. provider_class">

Org. hibernate. cache. ehcacheprovider

</Property>

<! -- Start "query cache" -->

<Propertyname = "hibernate. cache. use_query_cache">

True

</Property>

</Session-factory>

</Hibernate-configuration>

 

 

Ehcache. xml

 

<Ehcache>

<! -- Maxelementsinmemory is the maximum number of cached objects, and eternal is set to never expire. timetoidleseconds objects are idle for a maximum of seconds, and timetoliveseconds objects are cached for a maximum of seconds.
-->

<Diskstorepath = "Java. Io. tmpdir"/>

<Defaultcache maxelementsinmemory = "10000" Eternal = "false" timetoidleseconds = "300" timetoliveseconds = "600" overflowtodisk = "true"/>

</Ehcache>

 

 

 

* ***. HBM. xml

 

<? Xmlversion = "1.0" encoding = 'utf-8'?>

<! Doctype hibernate-mappingpublic

"-// Hibernate/hibernatemapping DTD 3.0 // en"

Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

 

<Hibernate-mapping>

<Class>

<! -- Set the level-2 Cache concurrent access policy of the persistence class read-only read-write nonstrict-read-writetransactional -->

<Cacheusage = "read-write"/>

</Class>

 

</Hibernate-mapping>

 

 

 

There are also two caching methods for the server to access web data. One is distributed cache and the other is central cache. Distributed Data retrieval is fast, but data modification costs are high. The other is the opposite, so it depends on the situation.

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.