26 Mayıs 2023 Cuma

SpringBoot spring.jpa Hibernate'e Özel Ayarlar - Query Cache

Giriş
Açıklaması şöyle
Hibernate can also cache result set of a query. Hibernate Query Cache doesn’t cache the state of the actual entities in the cache; it caches only identifier values and results of value type. So it should always be used in conjunction with the second-level cache.
Açıklaması şöyle. Kullanılması tavsiye edilmiyor
Caching of query results introduces some overhead in terms of your application's normal transactional processing. For example, if you cache the results of a query against a Person, Hibernate will need to keep track of when those results should be invalidated because changes have been committed against any Person entity.

That, coupled with the fact that most applications simply gain no benefit from caching query results, leads Hibernate to disable caching of query results by default.
Açıklaması şöyle
For all tables that are queried as part of cacheable queries, Hibernate keeps last update timestamps in a separate region named org.hibernate.cache.spi.UpdateTimestampsCache. Being aware of this region is very important if we use query caching because Hibernate uses it to verify that cached query results aren't stale. The entries in this cache must not be evicted/expired as long as there are cached query results for the corresponding tables in the query results regions. It's best to turn off automatic eviction and expiration for this cache region, as it doesn't consume lots of memory anyway.
Query Cache Nerede Saklanıyor
Eskiden org.hibernate.cache.internal.StandardQueryCache diye bir sınıf içinde saklanıyormuş. Bir soru burada. Ancak sanırım Hibernate 5.3 ile bu sınıf kaldırıldı

Ayarlar
hibernate.cache.use_query_cache
collection_cache, query_cache gibi farklı cache'ler mevcut. Açıklaması şöyle
Set this to true to enable the query cache.
Kod
Örnek
Şöyle yaparız
org.hibernate.Session session = (Session) entityManager.getDelegate(); Statistics statistics = session.getSessionFactory().getStatistics(); // check these variables // queryCacheHitCount // queryCacheMissCount // queryCachePutCount
Örnek - Spring Repository
Şöyle yaparız findAll() metodunun döndürdüğü her şey Hibernate Query Cache içinde saklanır
import jakarta.persistence.QueryHint; import org.springframework.data.jpa.repository.QueryHints; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import static org.hibernate.jpa.HibernateHints.HINT_CACHEABLE; @Repository public interface BookRepository extends CrudRepository<Book, Long> { @Override @QueryHints(@QueryHint(name = HINT_CACHEABLE, value = "true")) Iterable<Book> findAll(); }
Örnek
Şöyle yaparız
@Repository public interface AuthorRepository extends CrudRepository<Author, Integer> { @QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") }) Author findByName(String name); }
Örnek - Spring Repository
Şöyle yaparız. Burada Query Cache için isim belirtiliyor
import org.springframework.data.jpa.repository.QueryHints; import javax.persistence.QueryHint; import static org.hibernate.jpa.QueryHints.HINT_CACHEABLE; import static org.hibernate.jpa.QueryHints.HINT_CACHE_REGION; // some code is skipped @QueryHints({ @QueryHint(name = HINT_CACHEABLE, value = "true"), @QueryHint(name = HINT_CACHE_REGION, value = "query-cache-users") }) @Override Iterable<User> findAll();
Örnek - EntityManager
Şöyle yaparız
TypedQuery<BookPublisherValue> q = em .createQuery( "SELECT new org.thoughts.on.java.model.BookPublisherValue(b.title, p.name) + "FROM Book b JOIN b.publisher p WHERE b.id = :id", BookPublisherValue.class); q.setHint(QueryHints.CACHEABLE, true); q.setParameter("id", 1L); BookPublisherValue value = q.getSingleResult();
EhCache
Örnek
Şöyle yaparız
spring.jpa.properties.hibernate.cache.use_query_cache=true spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory





Hiç yorum yok:

Yorum Gönder