Skip to content

Commit

Permalink
added cache!
Browse files Browse the repository at this point in the history
  • Loading branch information
kane50613 committed May 15, 2021
1 parent 1a9c75b commit 64c7d13
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>tw.kane</groupId>
<artifactId>osu4j</artifactId>
<version>v1.1.5</version>
<version>v1.2</version>
<build>
<plugins>
<plugin>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tw.kane</groupId>
<artifactId>osu4j</artifactId>
<version>v1.1.5</version>
<version>v1.2</version>

<distributionManagement>
<repository>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/tw/kane/osu4j/Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tw.kane.osu4j;

import java.util.HashMap;

public class Cache {
public static HashMap<String, User> userCache = new HashMap<>();
public static HashMap<String, Beatmap> beatmapCache = new HashMap<>();
}
17 changes: 12 additions & 5 deletions src/main/java/tw/kane/osu4j/OsuClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class OsuClient {
private final String token;
private final OkHttpClient httpClient = new OkHttpClient();

static OsuClient client;

public OsuClient(String token) {
this.token = token;
client = this;
}

/**
Expand All @@ -28,7 +30,10 @@ public OsuClient(String token) {
* @throws InvalidTokenException if token provided to OsuClient wrong
* @throws NotFoundException if user not found
*/
public User getUser(String id) throws IOException, InvalidTokenException, NotFoundException {
public User getUser(String id, boolean allowCached) throws IOException, InvalidTokenException, NotFoundException {
if(allowCached && Cache.userCache.containsKey(id))
return Cache.userCache.get(id);

Request request = new Request.Builder()
.url(String.format("https://osu.ppy.sh/api/get_user?k=%s&u=%s",
token,
Expand All @@ -43,10 +48,12 @@ public User getUser(String id) throws IOException, InvalidTokenException, NotFou
throw new InvalidTokenException(result.getString("error"));
}
else {
JSONArray user = new JSONArray(responseString);
if(user.length() == 0)
JSONArray userArray = new JSONArray(responseString);
if(userArray.length() == 0)
throw new NotFoundException();
return new User((JSONObject) user.get(0));
User user = new User(userArray.getJSONObject(0));
Cache.userCache.put(id, user);
return user;
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tw/kane/osu4j/Score.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public enum Rank {
D("D"),
F("F");

public final String name;
private final String name;

Rank(String name) {
this.name = name;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/tw/kane/osu4j/User.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package tw.kane.osu4j;

import org.json.JSONObject;
import tw.kane.osu4j.Exception.InvalidTokenException;
import tw.kane.osu4j.Exception.NotFoundException;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -37,6 +40,18 @@ public User(JSONObject object) {
pp = new PerformancePoint();
}

public Score[] getBest() throws NotFoundException, InvalidTokenException, IOException {
if(OsuClient.client == null)
throw new InvalidTokenException("client not init first");
return OsuClient.client.getUserBest(id);
}

public Score[] getRecent() throws NotFoundException, InvalidTokenException, IOException {
if(OsuClient.client == null)
throw new InvalidTokenException("client not init first");
return OsuClient.client.getUserRecent(id);
}

private class Count {
long _50, _100, _300, SSH, SS, SH, S, A, plays, secondPlayed;

Expand Down

0 comments on commit 64c7d13

Please sign in to comment.