Skip to content

Commit

Permalink
feat : handle exception in cache (#473)
Browse files Browse the repository at this point in the history
* feat : handle exception in cache

* implement code review comments
  • Loading branch information
rajadilipkolli authored Dec 18, 2024
1 parent e086376 commit 6de2735
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"workbox-core": "7.3.0",
"workbox-precaching": "7.3.0"
},
"hash": "9c1312c261083d399cbb80453e62a52e287d8e2a587ecedd137dd0ee750baab5"
"hash": "3f87f2707dd507d9d4d7e4788795bda3f005f8e1d539f51da9bb24a911a3b133"
},
"overrides": {
"@vaadin/bundles": "$@vaadin/bundles",
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/com/app/folioman/config/redis/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.app.folioman.config.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.lang.Nullable;

@Configuration(proxyBeanMethods = false)
@EnableCaching
class RedisConfig {
class RedisConfig implements CachingConfigurer {

private static final Logger log = LoggerFactory.getLogger(RedisConfig.class);

@Bean
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
Expand All @@ -27,4 +35,36 @@ CustomRedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFacto
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
return new CustomRedisCacheManager(redisCacheWriter, redisConnectionFactory, monitor);
}

@Override
public CacheErrorHandler errorHandler() {

return new CacheErrorHandler() {

@Override
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
// your custom error handling logic
log.error("Error getting key {} from cache {}", key, cache.getName(), exception);
}

@Override
public void handleCachePutError(
RuntimeException exception, Cache cache, Object key, @Nullable Object value) {
// your custom error handling logic
log.error("Error putting key {} in cache {}", key, cache.getName(), exception);
}

@Override
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
// your custom error handling logic
log.error("Error evicting key {} from cache {}", key, cache.getName(), exception);
}

@Override
public void handleCacheClearError(RuntimeException exception, Cache cache) {
// your custom error handling logic
log.error("Error clearing cache {}", cache.getName(), exception);
}
};
}
}

0 comments on commit 6de2735

Please sign in to comment.