Skip to content

Commit db6715b

Browse files
committed
Implemented DifficultyService IpAddressMatcher cache
1 parent fc378be commit db6715b

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

captchaservice-backend/src/main/java/de/muenchen/captchaservice/service/difficulty/DifficultyService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import org.springframework.stereotype.Service;
1313

1414
import java.time.Instant;
15+
import java.util.Map;
1516
import java.util.Optional;
17+
import java.util.concurrent.ConcurrentHashMap;
1618

1719
@Service
1820
@Slf4j
@@ -22,6 +24,8 @@ public class DifficultyService {
2224

2325
private final CaptchaRequestRepository captchaRequestRepository;
2426

27+
private final Map<String, IpAddressMatcher> matcherCache = new ConcurrentHashMap<>();
28+
2529
@SuppressFBWarnings(value = { "EI_EXPOSE_REP2" }, justification = "Dependency Injection")
2630
public DifficultyService(final CaptchaProperties captchaProperties, final CaptchaRequestRepository captchaRequestRepository) {
2731
this.captchaProperties = captchaProperties;
@@ -65,7 +69,8 @@ public long getDifficultyForSourceAddress(final String siteKey, final SourceAddr
6569
public boolean isSourceAddressWhitelisted(final String siteKey, final SourceAddress sourceAddress) {
6670
final CaptchaSite captchaSite = captchaProperties.sites().get(siteKey);
6771
for (String subnet : captchaSite.whitelistedSourceAddresses()) {
68-
if (new IpAddressMatcher(subnet).matches(sourceAddress.getSourceAddress())) return true;
72+
IpAddressMatcher matcher = matcherCache.computeIfAbsent(subnet, IpAddressMatcher::new);
73+
if (matcher.matches(sourceAddress.getSourceAddress())) return true;
6974
}
7075
return false;
7176
}

0 commit comments

Comments
 (0)