I've been looking at Jhipster to learn how to setup caching correctly and there are a few things I don't understand.
This is the CacheConfig I have been using. It uses Ehcache with Hibernate 2nd level cache with use_query_cache=false.
When you annotate an Entity with @Cache:
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends AbstractAuditingEntity<Long> implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@NotNull
@Column(length = 50, unique = true, nullable = false)
private String login;
And also in CacheConfig:
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer() {
return cm -> {
createCache(cm,io.github.jhipster.sample.repository.UserRepository.USERS_BY_LOGIN_CACHE);
createCache(cm, io.github.jhipster.sample.domain.User.class.getName());
};
}
You essentially cache User entity with both Spring Cache Abstraction and Hibernate 2nd level right? So now every time you access a user you will cache it twice.
I can confirm is this true with a listener and hibernate.properties.generate_statictics=true.
On top of that Jhipster also cache on @Repository level with:
@Cacheable(cacheNames = USERS_BY_LOGIN_CACHE)
Optional<User> findOneWithAuthoritiesByLogin(String login);
Overall I'm really confused on how and when to use Spring Cache Abstraction, Hibernate 2nd level cache. It seems like even with use_query_cache=false when you query findAll Hibernate will cache every entity that it returns.