30 Ocak 2023 Pazartesi

SpringCache CacheErrorHandler - Redis Server Inaccessibility Yani Redis Erişimindeki Hatalar İçindir


Giriş
Şu satırı dahil ederiz 
import org.springframework.cache.interceptor.CacheErrorHandler;
Açıklaması şöyle
We define a custom error handler to handle any exceptions occurred during any Redis command execution. This helps to connect to the actual source of data instead of throwing an exception if the Redis command execution failed.
CachingConfigurer arayüzünü gerçekleştiren sınıfımızın errorHandler() metodunu override etmek gerekir

Örnek
Şöyle yaparız
@Override
public CacheErrorHandler errorHandler() {
  return new CacheErrorHandler() {
   @Override
   public void handleCacheGetError(RuntimeException exception, Cache cache, 
     Object key) {
   }

   @Override
   public void handleCachePutError(RuntimeException exception, Cache cache, 
    Object key, Object value) {
   }

    @Override
    public void handleCacheEvictError(RuntimeException exception, Cache cache,
      Object key) {
    }

    @Override
    public void handleCacheClearError(RuntimeException exception, Cache cache) {
    }
  };
}
Örnek
Şöyle yaparız
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CachingConfiguration implements CachingConfigurer {

  @Override
  public CacheErrorHandler errorHandler() {
    return new CustomCacheErrorHandler();
  }
}

import org.springframework.cache.interceptor.CacheErrorHandler;

public class CustomCacheErrorHandler implements CacheErrorHandler {

  @Override
  public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
  }

  @Override
  public void handleCachePutError(RuntimeException exception, Cache cache, Object key,
    Object value) {
  }

  @Override
  public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
  }

  @Override
  public void handleCacheClearError(RuntimeException exception, Cache cache) {
  }
}



Hiç yorum yok:

Yorum Gönder