Skip to content

Commit

Permalink
Remove r4j from DefaultL2CacheStorage (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed May 31, 2022
1 parent 5418622 commit 8c5e6b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 180 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<sqli.version>2.0.0</sqli.version>
<x7.version>3.0.0</x7.version>
<fastjson.version>1.2.79</fastjson.version>
<resilience4j.version>1.4.0</resilience4j.version>
<io.xream.util.version>1.0.0</io.xream.util.version>
</properties>

Expand Down
10 changes: 0 additions & 10 deletions x7-repo/x7-redis-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,5 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>${resilience4j.version}</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>${resilience4j.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,197 +17,56 @@
package io.xream.x7.repository.redis.cache;


import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.vavr.control.Try;
import io.xream.sqli.spi.L2CacheStorage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public final class DefaultL2CacheStorage implements L2CacheStorage {

@Resource
private StringRedisTemplate stringRedisTemplate;

private CircuitBreakerRegistry circuitBreakerRegistry;

@Value("${circuitbreaker.l2cache.name:l2cache}")
private String circuitBreakerL2cacheName;

private CircuitBreakerConfig circuitBreakerConfig = null;

public void setCircuitBreakerRegistry(CircuitBreakerRegistry registry) {
this.circuitBreakerRegistry = registry;
}


public <T> T handle(BackendService<T> backendService) {

if (this.circuitBreakerConfig == null) {
this.circuitBreakerConfig = circuitBreakerRegistry.getConfiguration(circuitBreakerL2cacheName)
.orElse(circuitBreakerRegistry.getDefaultConfig());
}
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(circuitBreakerL2cacheName,this.circuitBreakerConfig);
Supplier<T> decoratedSupplier = CircuitBreaker
.decorateSupplier(circuitBreaker, backendService::handle);
return Try.ofSupplier(decoratedSupplier).get();
}



public boolean set(String key, String value) {

if (circuitBreakerRegistry == null) {
if (key == null || key.equals(""))
return false;
stringRedisTemplate.opsForValue().set(key, value);
return true;
}

return this.handle(new BackendService<Boolean>() {
@Override
public Boolean handle() {
if (key == null || key.equals(""))
return false;
stringRedisTemplate.opsForValue().set(key, value);
return true;
}

@Override
public Boolean fallback(Throwable e) {

return true;
}
});

if (key == null || key.equals(""))
return false;
stringRedisTemplate.opsForValue().set(key, value);
return true;
}


public boolean set(String key, String value, int validSeconds, TimeUnit timeUnit) {

if (circuitBreakerRegistry == null) {
if (key == null || key.equals(""))
return false;
stringRedisTemplate.opsForValue().set(key, value, validSeconds, timeUnit);
return true;
}

return this.handle(new BackendService<Boolean>() {
@Override
public Boolean handle() {
if (key == null || key.equals(""))
return false;
stringRedisTemplate.opsForValue().set(key, value, validSeconds, timeUnit);
return true;
}

@Override
public Boolean fallback(Throwable e) {
return true;
}
});
if (key == null || key.equals(""))
return false;
stringRedisTemplate.opsForValue().set(key, value, validSeconds, timeUnit);
return true;
}


public String get(String key) {

if (circuitBreakerRegistry == null) {
String str = stringRedisTemplate.opsForValue().get(key);
if (str == null)
return null;
return str.trim();
}

return this.handle(new BackendService<String>() {
@Override
public String handle() {
String str = stringRedisTemplate.opsForValue().get(key);
if (str == null)
return null;
return str.trim();
}

@Override
public String fallback(Throwable e) {
return null;
}
});

String str = stringRedisTemplate.opsForValue().get(key);
if (str == null)
return null;
return str.trim();
}


public List<String> multiGet(List<String> keyList) {

if (circuitBreakerRegistry == null) {
if (keyList == null || keyList.isEmpty())
return null;

return stringRedisTemplate.opsForValue().multiGet(keyList);
}

return this.handle(new BackendService<List<String>>() {
@Override
public List<String> handle() {
if (keyList == null || keyList.isEmpty())
return null;

return stringRedisTemplate.opsForValue().multiGet(keyList);
}

@Override
public List<String> fallback(Throwable e) {
return null;
}
});

if (keyList == null || keyList.isEmpty())
return null;
return stringRedisTemplate.opsForValue().multiGet(keyList);
}


public boolean delete(String key) {
if (circuitBreakerRegistry == null) {
return stringRedisTemplate.delete(key);
}
return this.handle(new BackendService<Boolean>() {
@Override
public Boolean handle() {
return stringRedisTemplate.delete(key);
}

@Override
public Boolean fallback(Throwable e) {
return null;
}
});
return stringRedisTemplate.delete(key);
}

public Set<String> keys(String pattern) {

if (circuitBreakerRegistry == null) {
return stringRedisTemplate.keys(pattern);
}

return this.handle(new BackendService<Set<String>>() {
@Override
public Set<String> handle() {
return stringRedisTemplate.keys(pattern);
}

@Override
public Set<String> fallback(Throwable e) {
return null;
}
});
return stringRedisTemplate.keys(pattern);
}

public interface BackendService<T> {
T handle();
Object fallback(Throwable e) throws Throwable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.xream.x7;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.xream.sqli.api.NativeRepository;
import io.xream.sqli.api.TemporaryRepository;
import io.xream.sqli.api.customizer.DialectCustomizer;
Expand All @@ -41,7 +40,6 @@
import io.xream.x7.lock.LockProvider;
import io.xream.x7.lock.customizer.LockProviderCustomizer;
import io.xream.x7.repository.jdbctemplate.JdbcTemplateHelper;
import io.xream.x7.repository.redis.cache.DefaultL2CacheStorage;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -199,15 +197,6 @@ private void customizeCacheStorage(ApplicationStartedEvent applicationStartedEve
if (levelTwoCacheResolver == null)
return;
levelTwoCacheResolver.setCacheStorage(cacheStorage);
if (cacheStorage instanceof DefaultL2CacheStorage) {
try {
CircuitBreakerRegistry registry = applicationStartedEvent.getApplicationContext()
.getBean(CircuitBreakerRegistry.class);
((DefaultL2CacheStorage) cacheStorage).setCircuitBreakerRegistry(registry);
}catch (Exception e){

}
}

}

Expand Down

0 comments on commit 8c5e6b9

Please sign in to comment.