1

I'm using Spring Boot version 2.0 for an application. Here we have used one third-party jar file which contains lots of entities. For improving the performance of an application, we really needs to do second level cache on those jar contained entities. Since we are using Hibernate in our application, we preferred EhCache provider to implement second level cache.

Most of the websites provide examples with annotation @Cacheable that is not usable in our application since we are using jar file entities that contains class files only that is not editable. so, it would really help a lot if someone provides the code for cache those jar file entities using xml and how to configure those entities in ehCacheManager.

Versions

  • Hibernate v5.x +
  • ehCache v2.x +
  • Spring Boot v2.x +
Avinash
  • 4,115
  • 2
  • 22
  • 41
radhik
  • 51
  • 1
  • 7

3 Answers3

0

You can use XML Configuration for ehcache, here is a small example

Create a file called ehcache.xml inside src/main/resources

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">
    <diskStore path="java.io.tmpdir/ehcache" />
    <defaultCache maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="3" timeToLiveSeconds="120" overflowToDisk="true" />
    <cache
        name="your.entity.in.jar.Entity"
        maxElementsInMemory="100" eternal="false" timeToIdleSeconds="3"
        timeToLiveSeconds="120" overflowToDisk="false" />
</ehcache>

Visit the ehcache site for more details

https://www.ehcache.org/documentation/2.8/configuration/configuration.html

Avinash
  • 4,115
  • 2
  • 22
  • 41
  • I done the same what you suggested... but, the result is not cached. Each time, db hits happening. So, any more help pls. – radhik Jun 23 '21 at 13:47
0

In case you cannot annotate entities with @Cacheable you can set the SharedCacheMode to ALL

This answer explains how to setup the 2nd level cache and in your case you should change the mode from ENABLE_SELECTIVE to ALL. This other answer should help too.

Guillaume
  • 14,306
  • 3
  • 43
  • 40
0

Finally, I have made it work by enabling the below way.

Persistence.xml:

<persistence>
//few lines
<shared-cache-mode>ALL</shared-cache-mode>
</persistence>

eh-cache.xml:

<defaultCache name="default" maxElementsInMemory="500"
 eternal="false" timeToIdleSeconds="6000" timeToLiveSeconds="12000"
 overflowToDisk="false" diskPersistent="false"
 memoryStoreEvictionPolicy="LRU" />

In Configuration file enabled, ehcache region factory with second level cache as true.

Cache is working fine only on single table queries without joins, without inner queries. Does anyone help me to make it work for join queries and inner queries as well.?

Notes

  1. annotations not possible since we deals with class files

@Cacheable or @Cache on top of collection property on the entity class is not possible since we are dealing with class files (using entity class from jars). eg:

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "TABLE_NAME" ,schema ="SCHEMA_NAME")
public class SampleEntity {
    @Cacheable
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE) ***//not possible here since we deals with class files***
    @OneToMany
    private Collection<Bar> bars; 
} 
  1. Already jpa annotations only used in the entity class files and only cache is missing which we need to configure without disturbing the class files.
radhik
  • 51
  • 1
  • 7