Skip to content

Commit 2a925b9

Browse files
committed
caffeine cache manager
1 parent ac29e4b commit 2a925b9

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<maven-plugin-version>1.0.0</maven-plugin-version>
1919
<junit-version>4.13.1</junit-version>
2020
<default.package>vc.openapi</default.package>
21+
<spring.version>3.5.4</spring.version>
2122
</properties>
2223

2324
<repositories>
@@ -44,14 +45,18 @@
4445
<dependency>
4546
<groupId>org.springframework.boot</groupId>
4647
<artifactId>spring-boot-starter-web</artifactId>
47-
<version>3.5.4</version>
48+
<version>${spring.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-cache</artifactId>
53+
<version>${spring.version}</version>
4854
</dependency>
4955
<dependency>
5056
<groupId>me.paulschwarz</groupId>
5157
<artifactId>spring-dotenv</artifactId>
5258
<version>4.0.0</version>
5359
</dependency>
54-
<!-- JSON processing: jackson -->
5560
<dependency>
5661
<groupId>com.fasterxml.jackson.core</groupId>
5762
<artifactId>jackson-core</artifactId>

src/main/java/vc/config/CacheConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.springframework.cache.CacheManager;
44
import org.springframework.cache.annotation.EnableCaching;
5-
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
5+
import org.springframework.cache.caffeine.CaffeineCacheManager;
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.Configuration;
88

@@ -12,6 +12,6 @@ public class CacheConfiguration {
1212

1313
@Bean
1414
CacheManager cacheManager() {
15-
return new ConcurrentMapCacheManager();
15+
return new CaffeineCacheManager();
1616
}
1717
}

src/main/java/vc/live/watch/WatchManager.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ private void processChatsKeywordQueue() {
188188
}
189189
sendUserWatchNotification(
190190
"ChatsKeyword",
191+
userWatch.keyword(),
191192
userWatch,
192193
() -> chatsKeywordWatchEmbed(data, userWatch.keyword()),
193194
userChatWatchRepository::delete
@@ -202,6 +203,7 @@ private void processChatsKeywordQueue() {
202203
}
203204
sendGuildNotification(
204205
"ChatsKeyword",
206+
guildWatch.keyword(),
205207
guildWatch,
206208
() -> chatsKeywordWatchEmbed(data, guildWatch.keyword()),
207209
guildChatWatchRepository::delete
@@ -232,12 +234,13 @@ private <T> void processQueue(
232234
try {
233235
sendUserWatchNotification(
234236
id,
237+
userWatch.targetName(),
235238
userWatch,
236239
() -> watchEmbedProvider.apply(data),
237240
userPlayerWatchRepository::delete
238241
);
239242
} catch (Exception e) {
240-
LOGGER.error("Error sending user watch notification {}", userWatch, e);
243+
LOGGER.error("Error sending user watch target {} notification {}", userWatch.targetName(), userWatch, e);
241244
}
242245
}
243246
for (var userWatch : userWatches) {
@@ -263,6 +266,7 @@ private <T> void processQueue(
263266
if (!guildActivePredicate.apply(data, guildWatch)) continue;
264267
sendGuildNotification(
265268
id,
269+
guildWatch.targetName(),
266270
guildWatch,
267271
() -> watchEmbedProvider.apply(data),
268272
guildPlayerWatchRepository::delete
@@ -297,13 +301,14 @@ private <T> void processQueue(
297301

298302
private <T extends UserWatch> void sendUserWatchNotification(
299303
String id,
304+
String targetName,
300305
T userWatch,
301306
Supplier<EmbedCreateSpec> embedSupplier,
302307
Consumer<T> removeWatchConsumer
303308
) {
304309
try {
305310
var channel = discordClient.getUserById(Snowflake.of(userWatch.ownerUserId()))
306-
.doOnSuccess(user -> LOGGER.info("[{}] Sending user notification to {}", id, user.getUsername()))
311+
.doOnSuccess(user -> LOGGER.info("[{}] Sending {} user notification to {}", id, targetName, user.getUsername()))
307312
.flatMap(User::getPrivateChannel)
308313
.block(Duration.ofSeconds(10));
309314

@@ -312,36 +317,37 @@ private <T extends UserWatch> void sendUserWatchNotification(
312317
.build();
313318

314319
channel.createMessage(msg)
315-
.doOnError(error -> LOGGER.error("Error sending user notification to: {}", userWatch.ownerUserName()))
320+
.doOnError(error -> LOGGER.error("Error sending {} user notification to: {}", targetName, userWatch.ownerUserName()))
316321
.timeout(Duration.ofSeconds(3))
317322
.retryWhen(Retry.fixedDelay(1, Duration.ofSeconds(1))
318323
.filter(error -> error instanceof TimeoutException)
319324
.onRetryExhaustedThrow((spec, signal) -> Exceptions.retryExhausted(
320-
"Retries exhausted sending notification to user: " + userWatch.ownerUserName() + ", channelId: " + channel.getId().asString(),
325+
"Retries exhausted sending " + targetName + " notification to user: " + userWatch.ownerUserName() + ", channelId: " + channel.getId().asString(),
321326
signal.failure())))
322327
.onErrorResume(error -> {
323328
if (Exceptions.isRetryExhausted(error)) {
324329
if (error instanceof ClientException e) {
325330
int code = e.getStatus().code();
326331
if (code == 429) {
327-
LOGGER.error("Rate limited while sending notification to user: {}", userWatch.ownerUserName());
332+
LOGGER.error("Rate limited while sending {} notification to user: {}", targetName, userWatch.ownerUserName());
328333
} else if (code == 403 || code == 404) {
329-
LOGGER.error("Missing permissions while sending notification to user: {}. Removing watch.", userWatch.ownerUserName());
334+
LOGGER.error("Missing permissions while sending {} notification to user: {}. Removing watch.", targetName, userWatch.ownerUserName());
330335
removeWatchConsumer.accept(userWatch);
331336
}
332337
}
333338
}
334-
LOGGER.error("Error sending user notification to: {}", userWatch.ownerUserName(), error);
339+
LOGGER.error("Error sending {} user notification to: {}", targetName, userWatch.ownerUserName(), error);
335340
return Mono.empty();
336341
})
337342
.block(Duration.ofSeconds(10));
338343
} catch (Exception e) {
339-
LOGGER.error("Error sending user notification to {}", userWatch.ownerUserId(), e);
344+
LOGGER.error("Error sending {} user notification to {}", targetName, userWatch.ownerUserId(), e);
340345
}
341346
}
342347

343348
private <T extends GuildWatch> void sendGuildNotification(
344349
String id,
350+
String targetName,
345351
T guildWatch,
346352
Supplier<EmbedCreateSpec> embedSupplier,
347353
Consumer<T> removeGuildWatchFunction
@@ -362,7 +368,7 @@ private <T extends GuildWatch> void sendGuildNotification(
362368
return;
363369
}
364370

365-
LOGGER.info("[{}] Sending guild watch notification to {} ({})", id, guild.getName(), channel.getName());
371+
LOGGER.info("[{}] Sending guild watch {} notification to {} ({})", id, targetName, guild.getName(), channel.getName());
366372
var msgBuilder = MessageCreateSpec.builder()
367373
.addEmbed(embedSupplier.get());
368374

@@ -375,31 +381,31 @@ private <T extends GuildWatch> void sendGuildNotification(
375381
}
376382

377383
channel.getRestChannel().createMessage(msgBuilder.build().asRequest())
378-
.doOnError(error -> LOGGER.error("Error sending guild notification to guild: {}, channelId: {}", guildWatch.guildId(), channel.getId()))
384+
.doOnError(error -> LOGGER.error("Error sending guild notification {} to guild: {}, channelId: {}", targetName, guildWatch.guildId(), channel.getId()))
379385
.timeout(Duration.ofSeconds(3))
380386
.retryWhen(Retry.fixedDelay(1, Duration.ofSeconds(1))
381387
.filter(error -> error instanceof TimeoutException)
382388
.onRetryExhaustedThrow((spec, signal) -> Exceptions.retryExhausted(
383-
"Retries exhausted sending notification to guild: " + guildWatch.guildId() + ", channelId: " + channel.getId().asString(),
389+
"Retries exhausted sending " + targetName + " notification to guild: " + guildWatch.guildId() + ", channelId: " + channel.getId().asString(),
384390
signal.failure())))
385391
.onErrorResume(error -> {
386392
if (Exceptions.isRetryExhausted(error)) {
387393
if (error instanceof ClientException e) {
388394
int code = e.getStatus().code();
389395
if (code == 429) {
390-
LOGGER.error("Rate limited while sending notification to guild: {}, channelId: {}.", guildWatch.guildId(), channel.getId());
396+
LOGGER.error("Rate limited while sending {} notification to guild: {}, channelId: {}.", targetName, guildWatch.guildId(), channel.getId());
391397
} else if (code == 403 || code == 404) {
392-
LOGGER.error("Missing permissions while sending notification to guild: {}, channelId: {}. Removing watch.", guildWatch.guildId(), channel.getId());
398+
LOGGER.error("Missing permissions while sending {} notification to guild: {}, channelId: {}. Removing watch.", targetName, guildWatch.guildId(), channel.getId());
393399
removeGuildWatchFunction.accept(guildWatch);
394400
}
395401
}
396402
}
397-
LOGGER.error("Error sending guild notification to guild: {}, channelId: {}", guildWatch.guildId(), channel.getId(), error);
403+
LOGGER.error("Error sending guild notification {} to guild: {}, channelId: {}", targetName, guildWatch.guildId(), channel.getId(), error);
398404
return Mono.empty();
399405
})
400406
.block(Duration.ofSeconds(10));
401407
} catch (Exception e) {
402-
LOGGER.error("Error sending guild notification to guild: {}, channelId: {}", guildWatch.guildId(), guildWatch.channelId(), e);
408+
LOGGER.error("Error sending guild notification {} to guild: {}, channelId: {}", targetName, guildWatch.guildId(), guildWatch.channelId(), e);
403409
}
404410
}
405411

0 commit comments

Comments
 (0)