13 Aralık 2021 Pazartesi

SpringCache Ehcache 3 Kullanımı

Giriş
Açıklaması şöyle
The EhCache is an open source Java based cache used to boost performance. It stores the cache in memory and disk (SSD).

EhCache used a file called ehcache.xml. The EhCacheCacheManager is automatically configured if the application found the file on the classpath.
Maven
Şu satırı dahil ederiz
<dependency>
<groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.9.6</version> </dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> <version>1.1.0</version> <scope>runtime</scope> </dependency>
Açıklaması şöyle
You need both JCache and EhCache if you are using EhCache v3. In the previous v2, JCache was not needed as it was not built upon the JSR standard.
Bundan sonra cache ayarlarını ya xml ya da kodla vermek gerekir.

XML İle Belirtmek
application.properties dosyasında şöyle yaparız
spring.cache.jcache.config=classpath:ehcache.xml
ehcache.xml
Örnek
Şöyle yaparız.
<config
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns='http://www.ehcache.org/v3'
  xsi:schemaLocation="http://www.ehcache.org/v3
                      http://www.ehcache.org/schema/ehcache-core.xsd">

  <cache alias="cache_10s">
    <expiry>
      <ttl unit="seconds">10</ttl>
    </expiry>
    <heap unit="entries">10000</heap>
  </cache>

  <cache alias="cache_30s">
    <expiry>
      <ttl unit="seconds">30</ttl>
    </expiry>
    <heap unit="entries">10000</heap>
  </cache>
</config>
Örnek
Şöyle yaparız. Burada offheap ile JVM dışında kulanılabilecek bellek belirtiliyor.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.ehcache.org/v3"
   xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
   xsi:schemaLocation="
     http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
     http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

  <cache alias="allUserCache">
    <key-type>java.lang.String</key-type>
    <value-type>java.util.List</value-type>
    <expiry>
      <ttl unit="seconds">30</ttl>
    </expiry>
    <resources>
      <heap unit="entries">10</heap>
      <offheap unit="MB">10</offheap>
    </resources>
  </cache>
</config>
Kodla Belirtmek
1. org.ehcache.config.builders.CacheManagerBuilder kullanılır
Ehcache 3 CacheManagerBuilder Sınıfı yazında Ehcache3 ayarları görülebilir.
JCacheCacheManager Sınıfı - EhCache 3 yazında Ehcache3 ile Spring'in nasıl birleştirildiği görülebilir.

2. Eh107Configuration kullanılır
Örnek
Şöyle yaparız. Burada  org.ehcache.config.builders.CacheManagerBuilder kullanılarak bir org.ehcache.config.CacheConfiguration yaratılıyor. 

Daha sonra org.ehcache.jsr107.Eh107Configuration kullanılarak CacheConfiguration nesnesi 
JSR-107 javax.cache.configuration.Configuration haline çevriliyor.
Daha sonra JSR-107 configuration nesnesi yine JSR-107 javax.cache.spi.CachingProvider nesnesine takılıyor
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.jsr107.Eh107Configuration;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import java.time.Duration;

@Configuration
public class AppConfig {

  @Bean
  public CacheManager EhcacheManager() {

    CacheConfiguration<String, Person> cachecConfig = CacheConfigurationBuilder
      .newCacheConfigurationBuilder(String.class,
                        Person.class,
                        ResourcePoolsBuilder.newResourcePoolsBuilder()
                                .offheap(10, MemoryUnit.MB)
                                .build())
      .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(10)))
      .build();

    CachingProvider cachingProvider = Caching.getCachingProvider();
    CacheManager cacheManager = cachingProvider.getCacheManager();

    javax.cache.configuration.Configuration<String, Person> configuration = Eh107Configuration.fromEhcacheCacheConfiguration(cachecConfig);
    cacheManager.createCache("cacheStore", configuration);
    return cacheManager;
  }
}

Hiç yorum yok:

Yorum Gönder