The ORM framework, like hibernate, requires more complex mechanisms for mapping, object state management, and so on, compared to JDBC operations, and therefore has some loss in performance and efficiency.
Caching is one of the keys to improving hibernate in the assurance that mapping is avoided to produce inefficient SQL operations.
Adding the cache can avoid the connection creation and destruction of database calls, data package unpacking, SQL execution, network transmission, good caching mechanism and reasonable cache mode can bring a great performance improvement, Ehcache provides this good caching mechanism.
Reusing Sessionfactory is the first and most basic performance optimization method for hibernate optimization before considering the optimization of system join cache, referring to the sessionfactory reuse of Hibernate performance optimization.
Hibernate's caching mechanism
The level of caching is generally divided into three categories, each of which has a larger range of caches:
Transaction-level caching: Hibernate is called a first-level cache, and the cache object is shared in a session;
Application-level caching: Hibernate is called Level two cache, in a sessionfactory to share the cache object, sessionfactory in the entire application scope of reuse;
Distributed cache: Deployed as separate instances, such as Redis, Memcache, and so on.
Hibernate is cached in the following manner:
When hibernate accesses data objects based on their IDs, the first cache is checked from the session level.
Not found, if a level two cache is configured, then from the level two cache;
If it is not found, then query the database, the results by ID into the cache to delete, update, add data, while updating the cache.
Hibernate does not enable level two caching by default, Ehcache is a level two cache plug-in in Hibernate, and Hibernate-enabled systems can use the Ehcache cache directly.
Why use Ehcache directly
Look back at that sentence: a good caching mechanism and a reasonable caching mode can bring a great performance boost.
What is Hibernate's cache mode?
The object is cached according to the ID, which is the get, load operation of the session.
The drawbacks of this cache mode are two points:
1, the application scenario is too single, the system of a large number of list-type query cache does not work;
2, in some systems through the threadlocal thread in the reuse session, each thread may require a lot of processing unused business logic, cache hit rate is very low, if the session is not reused, the general scene cache hit rate is lower.
Now that Ehcache has provided a good caching mechanism, it is best to optimize the cache mode in conjunction with the business of your own system.
How to use Ehcache
Ehcache is a two-level cache plug-in in Hibernate, which uses hibernate to directly use the Ehcache cache without the need to add additional jar packages.
New Ehcache configuration file, the specific configuration meaning can be found in the manual:
<EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Ehcache.xsd"> <DiskstorePath= "Java.io.tmpdir"/> <Defaultcachemaxelementsinmemory= "10000"Maxelementsondisk= "0"Eternal= "true"Overflowtodisk= "true"diskpersistent= "false"Timetoidleseconds= "0"Timetoliveseconds= "0"Diskspoolbuffersizemb= " the"Diskexpirythreadintervalseconds= "+"Memorystoreevictionpolicy= "LFU" /> <Cachename= "Restcache"maxelementsinmemory= "+"Maxelementsondisk= "0"Eternal= "false"Overflowtodisk= "false"diskpersistent= "false"Timetoidleseconds= "119"Timetoliveseconds= "119"Diskspoolbuffersizemb= " the"Diskexpirythreadintervalseconds= "+"Memorystoreevictionpolicy= "FIFO" /> </Ehcache>
One advantage of Ehcache is the thread-safe, multi-threaded usage scenario that simplifies the use of developers.
So I wrote a singleton pattern to avoid writing getcache each time in the method, and this class also covers the basic use of Ehcache:
Public classEhcachefactory {Private FinalCacheManager Manager; Private Finalcache Cache; PrivateEhcachefactory () {Manager= Cachemanager.create (GetClass (). GetResource ("/ehcache.xml")); Cache= Manager.getcache ("Restcache"); } PublicElement GetCache (String strkey) {returnehcachefactory.getinstance (). GetCache (). get (strkey); } Public voidSetcache (String strkey, String strval) {ehcachefactory.getinstance (). GetCache (). Put (NewElement (strkey, strval)); } PublicCache GetCache () {returnCache; } Private Static classSingletonholder {Private Final StaticEhcachefactory INSTANCE =Newehcachefactory (); } Public Staticehcachefactory getinstance () {returnsingletonholder.instance; }}
My system is Java rest, where the Ehcache cache is added to the rest interface that needs buffering, and the JSON data returned by the rest interface as cached values through the URL parameter as the cache key, which is ideal for rest.
A simple performance test using AB:
In a simple query interface, performance is increased by one-fold;
In a slightly more complex interface, 4, 5 queries are executed, and the performance is increased 20 times times after the cache is added.
The AB test results are not posted here, so you can test them yourself.
Record, for the better of myself!
Hibernate performance Optimization Ehcache cache