Skip to content

Commit 17d385c

Browse files
authored
#28 Fix diable caffeine cache if useCache=false completely (#33)
1 parent 7bad055 commit 17d385c

File tree

2 files changed

+69
-34
lines changed

2 files changed

+69
-34
lines changed

src/main/java/org/bytedream/untis4j/RequestManager.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,23 @@ public class RequestManager {
2626
private final Infos infos;
2727
private final String url;
2828
private boolean loggedIn = true;
29-
private final LoadingCache<String, Response> requests = Caffeine.newBuilder().maximumSize(1_000).expireAfterWrite(Duration.ofMinutes(10)).refreshAfterWrite(Duration.ofMinutes(1)).build(this::POST);
29+
private final boolean useCache;
30+
private LoadingCache<String, Response> requests;
3031

3132
/**
3233
* Initialize the {@link RequestManager} class
3334
*
3435
* @param infos user information
3536
* @since 1.0
3637
*/
37-
public RequestManager(Infos infos) {
38+
public RequestManager(Infos infos, boolean useCache) {
3839
this.infos = infos;
39-
40-
url = infos.getServer() + baseURL + "?school=" + infos.getSchoolName();
40+
this.url = infos.getServer() + baseURL + "?school=" + infos.getSchoolName();
41+
this.useCache = useCache;
42+
43+
if (this.useCache) {
44+
requests = Caffeine.newBuilder().maximumSize(1_000).expireAfterWrite(Duration.ofMinutes(10)).refreshAfterWrite(Duration.ofMinutes(1)).build(this::POST);
45+
}
4146
}
4247

4348
/**
@@ -144,7 +149,12 @@ public Response POST(String method, Map<String, ?> params) throws IOException {
144149
public Response CachedPOST(String method, Map<String, ?> params) throws IOException {
145150

146151
if (loggedIn) {
147-
Response response = requests.get(UntisUtils.processParams(method, params));
152+
Response response;
153+
if (useCache) {
154+
response = requests.get(UntisUtils.processParams(method, params));
155+
} else {
156+
response = POST(UntisUtils.processParams(method, params));
157+
}
148158
if (method.equals(UntisUtils.Method.LOGOUT.getMethod()) && loggedIn && response != null) {
149159
loggedIn = false;
150160
}
@@ -211,5 +221,14 @@ private Response POST(String request) throws IOException
211221
public String getURL() {
212222
return url;
213223
}
214-
224+
225+
/**
226+
* Gets if every request response should be saved in cache
227+
*
228+
* @return gets if every request response should be saved in cache
229+
* @since 1.1
230+
*/
231+
public boolean isCacheUsed() {
232+
return useCache;
233+
}
215234
}

src/main/java/org/bytedream/untis4j/Session.java

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
public class Session {
3333

3434
private Infos infos;
35-
private boolean useCache = true;
3635
private RequestManager requestManager;
3736

3837
/**
@@ -68,6 +67,26 @@ public static Session login(String username, String password, String server, Str
6867
return login(username, password, server, schoolName, "");
6968
}
7069

70+
/**
71+
* Logs in to the server.
72+
*
73+
* <p>Send an login request to the server and returns {@link Session} if the login was successful.
74+
* Throws {@link IOException} if an IO Exception occurs or {@link LoginException} (which inherits from IOException) if login fails</p>
75+
*
76+
* @param username the username used for the api
77+
* @param password the password used for the api
78+
* @param server the server used for the api
79+
* @param schoolName the school name used for the api
80+
* @param useCache sets if every request response should be saved in cache
81+
* @return a session
82+
* @throws IOException if an IO Exception occurs
83+
* @see Session#login(String, String, String, String, String)
84+
* @since 1.0
85+
*/
86+
public static Session login(String username, String password, String server, String schoolName, boolean useCache) throws IOException {
87+
return login(username, password, server, schoolName, "", useCache);
88+
}
89+
7190
/**
7291
* Logs in to the server.
7392
*
@@ -84,12 +103,32 @@ public static Session login(String username, String password, String server, Str
84103
* @since 1.0
85104
*/
86105
public static Session login(String username, String password, String server, String schoolName, String userAgent) throws IOException {
106+
return login(username, password, server, schoolName, userAgent, true);
107+
}
108+
109+
/**
110+
* Logs in to the server.
111+
*
112+
* <p>Send an login request to the server and returns {@link Session} if the login was successful.
113+
* Throws {@link IOException} if an IO Exception occurs or {@link LoginException} (which inherits from IOException) if login fails</p>
114+
*
115+
* @param server the server from your school as URL
116+
* @param schoolName school name of the school you want to connect to
117+
* @param username the username used for the API
118+
* @param password the password used for the API
119+
* @param userAgent the user agent you want to send with
120+
* @param useCache sets if every request response should be saved in cache
121+
* @return a {@link Session} session
122+
* @throws IOException if an IO Exception occurs
123+
* @since 1.0
124+
*/
125+
public static Session login(String username, String password, String server, String schoolName, String userAgent, boolean useCache) throws IOException {
87126
if (!server.startsWith("http://") && !server.startsWith("https://")) {
88127
server = "https://" + server;
89128
}
90129
Infos infos = RequestManager.generateUserInfosAndLogin(username, password, server, schoolName, userAgent);
91130

92-
RequestManager requestManager = new RequestManager(infos);
131+
RequestManager requestManager = new RequestManager(infos, useCache);
93132

94133
return new Session(infos, requestManager);
95134
}
@@ -116,13 +155,14 @@ public void logout() throws IOException {
116155
* @since 1.0
117156
*/
118157
public void reconnect() throws IOException {
158+
boolean useCache = requestManager.isCacheUsed();
119159
try {
120160
logout();
121161
} catch (IOException ignore) {
122162
}
123163

124164
infos = RequestManager.generateUserInfosAndLogin(infos.getUsername(), infos.getPassword(), infos.getServer(), infos.getSchoolName(), infos.getUserAgent());
125-
requestManager = new RequestManager(infos);
165+
requestManager = new RequestManager(infos, useCache);
126166
}
127167

128168
/**
@@ -147,11 +187,7 @@ private <T extends BaseResponse> T requestSender(UntisUtils.Method method, Respo
147187
* @since 1.1
148188
*/
149189
private <T extends BaseResponse> T requestSender(UntisUtils.Method method, Map<String, ?> params, ResponseConsumer<? extends T> action) throws IOException {
150-
if (useCache) {
151-
return action.getResponse(requestManager.CachedPOST(method.getMethod(), params));
152-
} else {
153-
return action.getResponse(requestManager.POST(method.getMethod(), params));
154-
}
190+
return action.getResponse(requestManager.CachedPOST(method.getMethod(), params));
155191
}
156192

157193
/**
@@ -1148,24 +1184,4 @@ public Infos getInfos() {
11481184
return infos;
11491185
}
11501186

1151-
/**
1152-
* Gets if every request response should be saved in cache
1153-
*
1154-
* @return gets if every request response should be saved in cache
1155-
* @since 1.1
1156-
*/
1157-
public boolean isCacheUsed() {
1158-
return useCache;
1159-
}
1160-
1161-
/**
1162-
* Sets if every request response should be saved in cache what returns in better performance
1163-
*
1164-
* @param useCache sets if every request response should be saved in cache
1165-
* @since 1.1
1166-
*/
1167-
public void useCache(boolean useCache) {
1168-
this.useCache = useCache;
1169-
}
1170-
11711187
}

0 commit comments

Comments
 (0)