Skip to content

Commit 735847f

Browse files
committed
simplify cache and use versioned cache keys.
1 parent 6ed2723 commit 735847f

File tree

5 files changed

+31
-112
lines changed

5 files changed

+31
-112
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Breaking changes:
55
- `IPInfo` is renamed to `IPinfo`.
66
- `IPInfoBuilder` is moved into `IPinfo.Builder`.
77
- `ASNResponse.numIps` is now an `Integer` instead of a `String`.
8+
- The cache implementation now only uses `get` and `set`, which accept
9+
arbitrary strings, which may not necessarily be IP or ASN strings like
10+
"1.2.3.4" and "AS123".
811

912
Additions:
1013

@@ -15,3 +18,5 @@ Additions:
1518
of IPs.
1619
- Many new pieces of data have been added that were previously missing. The new
1720
dataset reflects all the new data available via raw API calls.
21+
- The keys given to cache functions will now be versioned. `IPinfo.cacheKey`
22+
must be used to derive the correct key if doing manual lookups.

src/main/java/io/ipinfo/api/IPinfo.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class IPinfo {
5454

5555
public static void main(String... args) {
5656
System.out.println("This library is not meant to be run as a standalone jar.");
57-
5857
System.exit(0);
5958
}
6059

@@ -66,15 +65,15 @@ public static void main(String... args) {
6665
* @throws RateLimitedException an exception when your api key has been rate limited.
6766
*/
6867
public IPResponse lookupIP(String ip) throws RateLimitedException {
69-
IPResponse response = cache.getIp(ip);
68+
IPResponse response = (IPResponse)cache.get(cacheKey(ip));
7069
if (response != null) {
7170
return response;
7271
}
7372

7473
response = new IPRequest(client, token, ip).handle();
7574
response.setContext(context);
7675

77-
cache.setIp(ip, response);
76+
cache.set(cacheKey(ip), response);
7877
return response;
7978
}
8079

@@ -86,15 +85,15 @@ public IPResponse lookupIP(String ip) throws RateLimitedException {
8685
* @throws RateLimitedException an exception when your api key has been rate limited.
8786
*/
8887
public ASNResponse lookupASN(String asn) throws RateLimitedException {
89-
ASNResponse response = cache.getAsn(asn);
88+
ASNResponse response = (ASNResponse)cache.get(cacheKey(asn));
9089
if (response != null) {
9190
return response;
9291
}
9392

9493
response = new ASNRequest(client, token, asn).handle();
9594
response.setContext(context);
9695

97-
cache.setAsn(asn, response);
96+
cache.set(cacheKey(asn), response);
9897
return response;
9998
}
10099

@@ -208,7 +207,7 @@ private ConcurrentHashMap<String, Object> getBatchGeneric(
208207
if (this.cache != null) {
209208
lookupUrls = new ArrayList<>(urls.size()/2);
210209
for (String url : urls) {
211-
Object val = cache.get(url);
210+
Object val = cache.get(cacheKey(url));
212211
if (val != null) {
213212
result.put(url, val);
214213
} else {
@@ -340,14 +339,24 @@ public void accept(String k, Object v) {
340339
for (String url : lookupUrls) {
341340
Object v = result.get(url);
342341
if (v != null) {
343-
cache.set(url, v);
342+
cache.set(cacheKey(url), v);
344343
}
345344
}
346345
}
347346

348347
return result;
349348
}
350349

350+
/**
351+
* Converts a normal key into a versioned cache key.
352+
*
353+
* @param k the key to convert into a versioned cache key.
354+
* @return the versioned cache key.
355+
*/
356+
public static String cacheKey(String k) {
357+
return k+":1";
358+
}
359+
351360
public static class Builder {
352361
private File countryFile =
353362
new File(this.getClass().getClassLoader().getResource("en_US.json").getFile());

src/main/java/io/ipinfo/api/cache/Cache.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@
44
import io.ipinfo.api.model.IPResponse;
55

66
public interface Cache {
7-
/**
8-
* Gets the IPResponse stored in cache.
9-
*
10-
* @param ip The ip string
11-
* @return IPResponse Object - may be null.
12-
*/
13-
IPResponse getIp(String ip);
14-
15-
/**
16-
* Gets the ASNResponse stored in cache.
17-
*
18-
* @param asn The asn string
19-
* @return ASNResponse Object - may be null.
20-
*/
21-
ASNResponse getAsn(String asn);
22-
237
/**
248
* Gets an arbitrary object stored in cache.
259
*
@@ -28,24 +12,6 @@ public interface Cache {
2812
*/
2913
Object get(String key);
3014

31-
/**
32-
* Sets the IP in the cache.
33-
*
34-
* @param ip The IP string.
35-
* @param response The IPResponse object - may be null.
36-
* @return if caching was successful
37-
*/
38-
boolean setIp(String ip, IPResponse response);
39-
40-
/**
41-
* Sets the ASN in the cache.
42-
*
43-
* @param asn the ASN string.
44-
* @param response the ASNResponse object - may be null.
45-
* @return if caching was successful.
46-
*/
47-
boolean setAsn(String asn, ASNResponse response);
48-
4915
/**
5016
* Sets a key/value pair in the cache.
5117
*

src/main/java/io/ipinfo/api/cache/NoCache.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
11
package io.ipinfo.api.cache;
22

3-
4-
import io.ipinfo.api.model.ASNResponse;
5-
import io.ipinfo.api.model.IPResponse;
6-
73
public class NoCache implements Cache {
8-
@Override
9-
public IPResponse getIp(String ip) {
10-
return null;
11-
}
12-
13-
@Override
14-
public ASNResponse getAsn(String asn) {
15-
return null;
16-
}
17-
184
@Override
195
public Object get(String key) {
206
return null;
217
}
228

23-
@Override
24-
public boolean setIp(String ip, IPResponse response) {
25-
return false;
26-
}
27-
28-
@Override
29-
public boolean setAsn(String asn, ASNResponse response) {
30-
return false;
31-
}
32-
339
@Override
3410
public boolean set(String key, Object val) {
3511
return false;

src/main/java/io/ipinfo/api/cache/SimpleCache.java

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package io.ipinfo.api.cache;
22

3-
import io.ipinfo.api.model.ASNResponse;
4-
import io.ipinfo.api.model.IPResponse;
5-
63
import java.time.Duration;
74
import java.time.Instant;
85
import java.time.temporal.ChronoField;
@@ -11,88 +8,54 @@
118

129
public class SimpleCache implements Cache {
1310
private final Duration duration;
14-
private final Map<String, Payload<ASNResponse>> asnCache = new HashMap<>();
15-
private final Map<String, Payload<IPResponse>> ipCache = new HashMap<>();
16-
private final Map<String, Payload<Object>> cache = new HashMap<>();
11+
private final Map<String, Payload> cache = new HashMap<>();
1712

1813
public SimpleCache(Duration duration) {
1914
this.duration = duration;
2015
}
2116

22-
@Override
23-
public IPResponse getIp(String ip) {
24-
Payload<IPResponse> payload = ipCache.get(ip);
25-
if (payload == null || payload.hasExpired()) {
26-
return null;
27-
}
28-
29-
return payload.data;
30-
}
31-
32-
@Override
33-
public ASNResponse getAsn(String asn) {
34-
Payload<ASNResponse> payload = asnCache.get(asn);
35-
if (payload == null || payload.hasExpired()) {
36-
return null;
37-
}
38-
39-
return payload.data;
40-
}
41-
4217
@Override
4318
public Object get(String key) {
44-
Payload<Object> payload = cache.get(key);
19+
Payload payload = cache.get(key);
4520
if (payload == null || payload.hasExpired()) {
4621
return null;
4722
}
4823

4924
return payload.data;
5025
}
5126

52-
@Override
53-
public boolean setIp(String ip, IPResponse response) {
54-
ipCache.put(ip, new Payload<>(response, duration));
55-
return true;
56-
}
57-
58-
@Override
59-
public boolean setAsn(String asn, ASNResponse response) {
60-
asnCache.put(asn, new Payload<>(response, duration));
61-
return true;
62-
}
63-
6427
@Override
6528
public boolean set(String key, Object val) {
66-
cache.put(key, new Payload<>(val, duration));
29+
cache.put(key, new Payload(val, duration));
6730
return true;
6831
}
6932

7033
@Override
7134
public boolean clear() {
72-
ipCache.clear();
73-
asnCache.clear();
7435
cache.clear();
7536
return true;
7637
}
7738

78-
private static class Payload<T> {
79-
final T data;
39+
private static class Payload {
40+
final Object data;
8041
final Instant creation;
8142
final Duration expiration;
8243

83-
Payload(T data, Duration duration) {
44+
Payload(Object data, Duration duration) {
8445
this.data = data;
8546
creation = Instant.now();
8647
this.expiration = duration;
8748
}
8849

8950
public boolean hasExpired() {
90-
long time = expiration.addTo(creation).getLong(ChronoField.INSTANT_SECONDS);
51+
long time = expiration
52+
.addTo(creation)
53+
.getLong(ChronoField.INSTANT_SECONDS);
9154
long now = System.currentTimeMillis();
9255
return now <= time;
9356
}
9457

95-
public T getData() {
58+
public Object getData() {
9659
return data;
9760
}
9861
}

0 commit comments

Comments
 (0)