aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-12-09 14:45:24 +0300
committerGravatar Vitaly Takmazov2022-12-09 14:45:52 +0300
commita5ccfcd3f3e8d96250c92638cf2f66736827ffbc (patch)
treeddab5a2464976533d27ae9eb92dfcb5b71c9b06b
parent13b81d40fde2939500924403dd767bdc9887ff14 (diff)
Better caching in AnythingFilter
-rw-r--r--src/main/java/com/juick/service/UserService.java7
-rw-r--r--src/main/java/com/juick/service/UserServiceImpl.java6
-rw-r--r--src/main/java/com/juick/www/filters/AnythingFilter.java11
-rw-r--r--src/main/resources/application.properties2
4 files changed, 17 insertions, 9 deletions
diff --git a/src/main/java/com/juick/service/UserService.java b/src/main/java/com/juick/service/UserService.java
index a3ab8315..db02c34e 100644
--- a/src/main/java/com/juick/service/UserService.java
+++ b/src/main/java/com/juick/service/UserService.java
@@ -46,12 +46,15 @@ public interface UserService {
String getSignUpHashByTelegramID(Long telegramId, String username);
- @CacheEvict(value = "users_by_name", allEntries = true)
+ @CacheEvict(value = { "usernames", "users_by_name" }, allEntries = true)
Optional<User> createUser(String username, String password) throws UsernameTakenException;
Optional<User> getUserByUID(int uid);
- @Cacheable(value = "users_by_name")
+ @Cacheable(value = "usernames")
+ List<String> getUsernames();
+
+ @Cacheable(value = "users_by_name", key = "{ #username }")
@Nonnull User getUserByName(String username);
@Nonnull User getUserByEmail(String email);
diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java
index 71f2dc91..0e6f9606 100644
--- a/src/main/java/com/juick/service/UserServiceImpl.java
+++ b/src/main/java/com/juick/service/UserServiceImpl.java
@@ -173,6 +173,12 @@ public class UserServiceImpl extends BaseJdbcService implements UserService {
return AnonymousUser.INSTANCE;
}
+ @Transactional(readOnly = true)
+ @Override
+ public List<String> getUsernames() {
+ return getJdbcTemplate().queryForList("SELECT nick FROM users", String.class);
+ }
+
@Override
@Transactional(readOnly = true)
@Nonnull
diff --git a/src/main/java/com/juick/www/filters/AnythingFilter.java b/src/main/java/com/juick/www/filters/AnythingFilter.java
index a725a66f..2ec82801 100644
--- a/src/main/java/com/juick/www/filters/AnythingFilter.java
+++ b/src/main/java/com/juick/www/filters/AnythingFilter.java
@@ -71,16 +71,15 @@ public class AnythingFilter extends OncePerRequestFilter {
}
}
}
- User user = userService.getUserByName(anything);
- if (user.getUid() > 0) {
- servletResponse.sendRedirect("/" + user.getName() + "/");
+ ;
+ if (userService.getUsernames().stream().anyMatch(name -> name.equals(anything))) {
+ servletResponse.sendRedirect("/" + anything + "/");
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
} else {
- User user = userService.getUserByName(anything);
- if (!user.isAnonymous()) {
- servletResponse.sendRedirect("/" + user.getName() + "/?before=" + before);
+ if (userService.getUsernames().stream().anyMatch(name -> name.equals(anything))) {
+ servletResponse.sendRedirect("/" + anything + "/?before=" + before);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index b23842ee..3ef9bdfc 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -7,5 +7,5 @@ spring.h2.console.enabled=true
spring.datasource.generate-unique-name=false
spring.sql.init.platform=h2
spring.cache.type=none
-spring.cache.cache-names=help, users_by_name, discover, discussions, messages, replies, popular_tags
+spring.cache.cache-names=help, usernames, users_by_name, discover, discussions, messages, replies, popular_tags
spring.profiles.active=@activatedProperties@