In hibernate, the cache will work in the following scenarios:
1. When loading data via id[primary key]
2. Lazy Loading
First-level caching:
Also known as the internal cache, the data object associated with the current session is saved, which is created with the creation of the session instance and dies out. This is called the session level cache.
The first-level cache is normally hibernate and automatically maintained, and if manual intervention is required, it can be done in the following ways.
1.session.evict
Clears a particular object from the internal cache.
2.sessin.clear
Empty the internal cache
Second-level cache:
Also known as sessionfactory level Cache.
Use level two caching for what kind of data.
Caching all data is the simplest and most common approach.
However, in some cases, it can affect performance, such as the telephone fee query system, if the cache is implemented, memory will be almost impossible to reuse the data flooding, resulting in performance degradation.
If your data meets the following criteria, you can include it in cache management:
1. The data will not be modified by third party applications.
2.data size within acceptable range
3. Low Data Update frequency
4. The same data may be frequently referenced by the system
5. Non-critical data
Hibernate itself does not provide a product implementation of level two caching (a simple Hashtable-based cache is provided for debugging) and can be implemented using a third-party cache.
By default, Ehcache is implemented as a level two cache.
EHCache Org.ehcache.hibernate.Provider