26 Eylül 2022 Pazartesi

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

Ayarlar
use_second_level_cache=true 
 2. kademe cache kullanılacağı belirtilir. 2. kademe cache açılıp kapatılabilen bir şey. Açıklaması şöyle
Set this to true to enable the second-level cache.
hibernate.cache.region.factory_class
 hangi cache gerçekleştirimi kullanılacağı belirtilir. Cache gerçekleştirimi olarak Hazelcast, Redis, EhCache vs kullanılabilir. Eğer bu alanı tanımlamazsak sanırım Spring kendisi otomatik olarak bulmaya çalışıyor Açıklaması şöyle
This property specifies the caching provider to be used for the second-level and query cache. For example, to use EHCache, set it to org.hibernate.cache.ehcache.EhCacheRegionFactory.
hibernate.cache.provider_configuration_file_resource_path
Açıklaması şöyle
This property specifies the path to the configuration file for the caching provider. For example, for EHCache, it can be set to ehcache.xml.

Entity İçin CacheConcurrency
Açıklaması şöyle
Hibernate supports three cache modes for Second-level Caching: read-only, read-write, and transactional. The read-only cache mode is used for data that is not expected to change frequently. The read-write cache mode is used for data that is frequently updated. The transactional cache mode is used for data that is updated within a transaction.

The cache concurrency strategy determines how multiple threads access the cache. Hibernate supports multiple cache concurrency strategies such as READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, and TRANSACTIONAL.
CacheConcurrencyStrategy.READ_ONLY
Açıklaması şöyle
- This strategy is suitable for entities that are read frequently but rarely modified. It assumes that the data is mostly static and does not change frequently.
- No automatic synchronization with the database when entities are updated or deleted.
CacheConcurrencyStrategy.READ_WRITE
Açıklaması şöyle
This strategy is used for entities that are frequently both read and modified. It ensures that cached data is kept up-to-date with changes in the database.

- Automatic synchronization with the database when entities are updated or deleted.
- This strategy guarantees strong consistency which it achieves by using ‘soft’ locks: When a cached entity is updated, a soft lock is stored in the cache for that entity as well, which is released after the transaction is committed. All concurrent transactions that access soft-locked entries will fetch the corresponding data directly from database.
-  Suitable for entities that are updated occasionally but read frequently.

In JTA Environment: If your application uses JTA for managing transactions (common in enterprise-level applications), you need to specify the property hibernate.transaction.manager_lookup_class. This property should specify a strategy for obtaining the JTA TransactionManager, which is crucial for managing distributed transactions in JTA environments.
CacheConcurrencyStrategy.NONSTRICT_READ_WRITE
Açıklaması şöyle
- This strategy allows for better performance than READ_WRITE while sacrificing some cache consistency.
- Suitable for entities that are updated less frequently than they are read and where slight data staleness is acceptable.
- Automatic synchronization with the database when entities are updated or deleted but with less strict consistency guarantees compared to READ_WRITE.
- Provides improved write performance compared to READ_WRITE because it doesn't acquire locks during read operations.
Örnek
Şöyle yaparız
@Data @Entity @Table(name = "users") @Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String email; }
Örnek - One-Many & Many-Many Relations
Açıklaması şöyle
When Hibernate caches a collection, it doesn’t cache the entire collection of entities but rather caches the IDs of the entities contained in the collection.
Şöyle yaparız
@Entity @Cacheable public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @OneToMany(mappedBy = "category") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) private List<Product> products; // Getters and setters } @Entity @Cacheable public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToOne private Category category; // Getters and setters }
Açıklaması şöyle
- Hibernate creates a cache region named com.example.model.Category.products. This region is dedicated to storing cached collections of products associated with categories.
- Let’s say we have a category with ID 1 and it has products with IDs 101, 102, and 103. Hibernate stores this data in the cache using a key-value pair.
- The key-value pair might look like "Category:1:products — [101, 102, 103]"

EhCache
- EhCache için "org.hibernate.cache.ehcache.EhCacheRegionFactory" yazılır. Şu satırı dahil ederiz
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-ehcache</artifactId>
  <version>5.6.11.Final</version>
</dependency>
Örnek
Şöyle yaparız.
# caching
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=
  org.hibernate.cache.ehcache.EhCacheRegionFactory
Örnek
Şöyle yaparız
# Enable @Cacheable annotation spring.jpa.properties.javax.persistence.sharedCache.mode = ENABLE_SELECTIVE # Enable second-level cache. spring.jpa.properties.hibernate.cache.use_second_level_cache = true # Specifies the class to handle the second-level cache. spring.jpa.properties.hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory # Check whether the cache is applied spring.jpa.properties.hibernate.generate_statistics = true
Örnek
Şöyle yaparız.
jpa:
    hibernate:
      ddl-auto: update
      show_sql: true
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        cache:
          use_second_level_cache: true
          region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
          use_query_cache: true
          use_collection_cache: true
Hazelcast
Şu satırı dahil ederiz
<dependency>
  <groupId>com.hazelcast</groupId>
  <artifactId>hazelcast-hibernate53</artifactId>
  <version>2.2.1</version>
</dependency>
Bu bağımlılık ile HazelcastCacheRegionFactory sınıfı geliyor. Hibernate ile bağlantıyı sağlayan sınıf bu. Kalıtım şöyle
org.hibernate.cache.spi.RegionFactory
  org.hibernate.cache.RegionFactory
    AbstractHazelcastCacheRegionFactory
      HazelcastCacheRegionFactory
  
Örnek
Şöyle yaparız. Burada Hibernate Second Level Cache aynı zamanda SpringCache içinde kullanılıyor. Bu yüzden instance_name belirtiliyor ve shutdown_on_session_factory_close alanı false yapılıyor
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=
  com.hazelcast.hibernate.HazelcastCacheRegionFactory

spring.jpa.properties.hibernate.cache.hazelcast.instance_name=users-app
spring.jpa.properties.hibernate.cache.hazelcast.shutdown_on_session_factory_close=false
Açıklaması şöyle
Here, along with a general second-level cache, we also enable Query cache. As we are using an embedded Hazelcast that will be used in a distributed environment, the factory class is set into com.hazelcast.hibernate.HazelcastCacheRegionFactory. A detailed description of properties and settings for Hibernate caching and Hazelcast implementation for the second-level cache can be found in documentation correspondingly Hibernate Caching and Hibernate Second Level Cache.
Hazelcast ayarlarında şöyle yaparız
hazelcast:
  instance-name: users-app
Burada entity şöyle tanımlanır @Cache ve CacheConcurrencyStrategy sınıfları Hibernate kütüphanesinden geliyor.
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.Cacheable;
import javax.persistence.Entity;

@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Entity
public class User {
  // skipped
}
Örnek
Şöyle yaparız
# Second-Level Cache Configuration
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=
  com.hazelcast.hibernate.HazelcastCacheRegionFactory
spring.jpa.properties.hibernate.cache.hazelcast.use_native_client=true
spring.jpa.properties.hibernate.cache.use_minimal_puts=true
spring.jpa.properties.hibernate.generate_statistics=true
logging.level.org.hibernate.type=trace
spring.cache.type=hazelcast

Redis
Örnek
Şu satırı dahil ederiz. Bu örnekte hibernate.cache.region.factory_class belirtilmiyor. 
<dependency>
  <groupId>org.redisson</groupId>
  <artifactId>redisson-hibernate-53</artifactId>
  <version>3.19.0</version>
</dependency>



Hiç yorum yok:

Yorum Gönder