From eb893b5388f82c42c2e7a17f099d76f7bf5071e0 Mon Sep 17 00:00:00 2001 From: Thierry De Leeuw Date: Wed, 11 Sep 2024 18:55:38 +0200 Subject: [PATCH] Issue #51 Simple Cache is not Thread Safe (and remove unused imports) --- src/main/java/io/ipinfo/api/cache/Cache.java | 3 --- src/main/java/io/ipinfo/api/cache/SimpleCache.java | 4 ++-- src/main/java/io/ipinfo/api/request/MapRequest.java | 5 ++++- src/test/java/io/ipinfo/IPinfoTest.java | 3 --- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/ipinfo/api/cache/Cache.java b/src/main/java/io/ipinfo/api/cache/Cache.java index a967191..1d58472 100644 --- a/src/main/java/io/ipinfo/api/cache/Cache.java +++ b/src/main/java/io/ipinfo/api/cache/Cache.java @@ -1,8 +1,5 @@ package io.ipinfo.api.cache; -import io.ipinfo.api.model.ASNResponse; -import io.ipinfo.api.model.IPResponse; - public interface Cache { /** * Gets an arbitrary object stored in cache. diff --git a/src/main/java/io/ipinfo/api/cache/SimpleCache.java b/src/main/java/io/ipinfo/api/cache/SimpleCache.java index 52d14cb..be23e51 100644 --- a/src/main/java/io/ipinfo/api/cache/SimpleCache.java +++ b/src/main/java/io/ipinfo/api/cache/SimpleCache.java @@ -3,12 +3,12 @@ import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoField; -import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class SimpleCache implements Cache { private final Duration duration; - private final Map cache = new HashMap<>(); + private final Map cache = new ConcurrentHashMap<>(); public SimpleCache(Duration duration) { this.duration = duration; diff --git a/src/main/java/io/ipinfo/api/request/MapRequest.java b/src/main/java/io/ipinfo/api/request/MapRequest.java index 3e6baf6..1b56fdf 100644 --- a/src/main/java/io/ipinfo/api/request/MapRequest.java +++ b/src/main/java/io/ipinfo/api/request/MapRequest.java @@ -3,7 +3,10 @@ import io.ipinfo.api.errors.ErrorResponseException; import io.ipinfo.api.errors.RateLimitedException; import io.ipinfo.api.model.MapResponse; -import okhttp3.*; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; import java.util.List; diff --git a/src/test/java/io/ipinfo/IPinfoTest.java b/src/test/java/io/ipinfo/IPinfoTest.java index 0bd0fc7..ac8c51e 100644 --- a/src/test/java/io/ipinfo/IPinfoTest.java +++ b/src/test/java/io/ipinfo/IPinfoTest.java @@ -1,14 +1,11 @@ package io.ipinfo; import io.ipinfo.api.IPinfo; -import io.ipinfo.api.errors.ErrorResponseException; import io.ipinfo.api.errors.RateLimitedException; import io.ipinfo.api.model.ASNResponse; import io.ipinfo.api.model.IPResponse; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.List;