Skip to content

Commit efaca44

Browse files
committed
♻️ refactor: refactor codebase #2
1 parent a41f7b4 commit efaca44

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.redis4j.config;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.cache.Cache;
6+
import org.springframework.cache.interceptor.CacheErrorHandler;
7+
import org.springframework.stereotype.Component;
8+
9+
@SuppressWarnings({"NullableProblems"})
10+
@Component
11+
public class DefaultRedis4jCacheErrorConfig implements CacheErrorHandler {
12+
protected static final Logger logger = LoggerFactory.getLogger(DefaultRedis4jCacheErrorConfig.class);
13+
14+
@Override
15+
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
16+
logger.error("Redis4j, cache get error for key: {} in cache: {}", key, cache.getName(), exception);
17+
}
18+
19+
@Override
20+
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
21+
logger.error("Redis4j, cache put error for key: {} with value: {} in cache: {}", key, value, cache.getName(), exception);
22+
}
23+
24+
@Override
25+
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
26+
logger.error("Redis4j, cache evict error for key: {} in cache: {}", key, cache.getName(), exception);
27+
}
28+
29+
@Override
30+
public void handleCacheClearError(RuntimeException exception, Cache cache) {
31+
logger.error("Redis4j, cache clear error in cache: {}", cache.getName(), exception);
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.redis4j.config;
2+
3+
import io.lettuce.core.event.connection.ConnectionActivatedEvent;
4+
import io.lettuce.core.event.connection.ConnectionDeactivatedEvent;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.context.event.EventListener;
8+
9+
//@Component
10+
public class DefaultRedis4jEventListenerConfig {
11+
protected static final Logger logger = LoggerFactory.getLogger(DefaultRedis4jEventListenerConfig.class);
12+
13+
@EventListener
14+
public void onConnectionActivated(ConnectionActivatedEvent event) {
15+
logger.info("Connection activated event: {}", event.toString());
16+
// drafting
17+
}
18+
19+
@EventListener
20+
public void onConnectionDeactivated(ConnectionDeactivatedEvent event) {
21+
logger.error("Connection deactivated event: {}", event.toString());
22+
// drafting
23+
}
24+
}

plugin/src/main/groovy/org/redis4j/config/Redis4jConfig.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package org.redis4j.config;
22

3+
import io.lettuce.core.RedisClient;
4+
import io.lettuce.core.event.EventBus;
5+
import io.lettuce.core.resource.ClientResources;
6+
import io.lettuce.core.resource.DefaultClientResources;
37
import org.redis4j.service.Redis4jConfigService;
48
import org.springframework.beans.factory.annotation.Autowired;
59
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
610
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
711
import org.springframework.cache.CacheManager;
812
import org.springframework.cache.annotation.EnableCaching;
13+
import org.springframework.cache.interceptor.CacheErrorHandler;
914
import org.springframework.context.annotation.Bean;
1015
import org.springframework.context.annotation.Configuration;
1116
import org.springframework.context.annotation.Primary;
@@ -23,11 +28,7 @@
2328
@Configuration
2429
@EnableCaching
2530
@EnableRedisRepositories
26-
@ConditionalOnProperty(
27-
value = "spring.redis4j.enabled",
28-
havingValue = "true",
29-
matchIfMissing = false
30-
)
31+
@ConditionalOnProperty(value = "spring.redis4j.enabled", havingValue = "true", matchIfMissing = false)
3132
public class Redis4jConfig {
3233
protected final Redis4jConfigService redis4jConfigService;
3334

@@ -88,4 +89,26 @@ public StringRedisTemplate stringRedisTemplate() {
8889
public CacheManager cacheManager() {
8990
return redis4jConfigService.createCacheManager(this.factory());
9091
}
92+
93+
@Bean
94+
public ClientResources clientResources() {
95+
return DefaultClientResources.create();
96+
}
97+
98+
@Bean
99+
public RedisClient redisClient() {
100+
return RedisClient.create(clientResources());
101+
}
102+
103+
@Bean
104+
public EventBus eventBus() {
105+
return clientResources().eventBus();
106+
}
107+
108+
@Bean
109+
@Primary
110+
@ConditionalOnMissingBean(CacheErrorHandler.class)
111+
public CacheErrorHandler errorHandler() {
112+
return new DefaultRedis4jCacheErrorConfig();
113+
}
91114
}

0 commit comments

Comments
 (0)