From 912171165155ff78ad111c9c4043844c34567a42 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Apr 2022 11:13:58 +0300 Subject: Configure caches * caching is disabled because of OOMs in production --- src/main/java/com/juick/API.java | 2 ++ src/main/java/com/juick/config/WebConfig.java | 5 ----- src/main/java/com/juick/service/EmailService.java | 3 +++ .../java/com/juick/service/MessagesService.java | 7 +++++++ .../com/juick/service/PrivacyQueriesService.java | 4 ++++ .../com/juick/service/SubscriptionService.java | 4 ++++ src/main/java/com/juick/service/TagService.java | 2 ++ src/main/java/com/juick/service/UserService.java | 9 ++++++++ src/main/resources/application.properties | 4 +++- .../java/com/juick/server/tests/ServerTests.java | 24 +++++++++++----------- 10 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/juick/API.java b/src/main/java/com/juick/API.java index 79ec209d..755e4fc4 100644 --- a/src/main/java/com/juick/API.java +++ b/src/main/java/com/juick/API.java @@ -21,10 +21,12 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @EnableAutoConfiguration(exclude = { MailSenderAutoConfiguration.class }) +@EnableCaching @ComponentScan(basePackages = {"com.juick.config", "com.juick.service", "com.juick.www"}) public class API { public static void main(String[] args) { diff --git a/src/main/java/com/juick/config/WebConfig.java b/src/main/java/com/juick/config/WebConfig.java index bfebd77f..0c816712 100644 --- a/src/main/java/com/juick/config/WebConfig.java +++ b/src/main/java/com/juick/config/WebConfig.java @@ -40,7 +40,6 @@ import org.commonmark.parser.Parser; import org.commonmark.renderer.html.HtmlRenderer; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; @@ -78,10 +77,6 @@ public class WebConfig implements WebMvcConfigurer { public StorageService storageService() { return new FileSystemStorageService(baseDir, tmpDir); } - @Bean - public CaffeineCacheManager cacheManager() { - return new CaffeineCacheManager("help"); - } @Bean public HelpService helpService() { diff --git a/src/main/java/com/juick/service/EmailService.java b/src/main/java/com/juick/service/EmailService.java index 158fae80..bdd7d405 100644 --- a/src/main/java/com/juick/service/EmailService.java +++ b/src/main/java/com/juick/service/EmailService.java @@ -19,12 +19,15 @@ package com.juick.service; import com.juick.model.User; +import org.springframework.cache.annotation.CacheEvict; + import java.util.List; /** * Created by vitalyster on 09.12.2016. */ public interface EmailService { + @CacheEvict(value = "users_by_name", allEntries = true) boolean verifyAddressByCode(Integer userId, String code); boolean addVerificationCode(Integer userId, String account, String code); boolean addEmail(Integer userId, String email); diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index d102031d..3fdd2b9e 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -23,6 +23,8 @@ import com.juick.model.User; import com.juick.model.ResponseReply; import com.juick.model.Tag; import org.apache.commons.lang3.tuple.Pair; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import java.net.URI; import java.util.*; @@ -31,8 +33,10 @@ import java.util.*; * Created by aalexeev on 11/13/16. */ public interface MessagesService { + @CacheEvict(value = { "discussions", "messages", "replies" }, allEntries = true) int createMessage(int uid, String txt, String attachment, Set tags); + @CacheEvict(value = { "discussions", "messages", "replies" }, allEntries = true) int createReply(int mid, int rid, User user, String txt, String attachment); enum RecommendStatus { Error, @@ -83,6 +87,7 @@ public interface MessagesService { List getPrivate(int uid, int before); + @Cacheable(value = "discussions", key="#uid + #to") List getDiscussions(int uid, Long to); /** @@ -111,10 +116,12 @@ public interface MessagesService { List getUserSearch(User visitor, int UID, String search, int privacy, int page); + @Cacheable(value = "messages", key="#visitor.uid + #mids.hashCode()") List getMessages(User visitor, List mids); Map> updateReactionsFor(final List mid); + @Cacheable(value = "replies", key="#user.uid + #mid") List getReplies(User user, int mid); boolean setMessagePrivacy(int mid); diff --git a/src/main/java/com/juick/service/PrivacyQueriesService.java b/src/main/java/com/juick/service/PrivacyQueriesService.java index e4b1da25..68c157f6 100644 --- a/src/main/java/com/juick/service/PrivacyQueriesService.java +++ b/src/main/java/com/juick/service/PrivacyQueriesService.java @@ -20,6 +20,8 @@ package com.juick.service; import com.juick.model.Tag; import com.juick.model.User; +import org.springframework.cache.annotation.CacheEvict; + /** * Created by aalexeev on 11/13/16. */ @@ -28,7 +30,9 @@ public interface PrivacyQueriesService { Removed, Added } + @CacheEvict(value = { "discussions", "messages", "replies" }, allEntries = true) PrivacyResult blacklistUser(User user, User target); + @CacheEvict(value = { "discussions", "messages", "replies" }, allEntries = true) PrivacyResult blacklistTag(User user, Tag tag); } diff --git a/src/main/java/com/juick/service/SubscriptionService.java b/src/main/java/com/juick/service/SubscriptionService.java index c12c9625..96735d1a 100644 --- a/src/main/java/com/juick/service/SubscriptionService.java +++ b/src/main/java/com/juick/service/SubscriptionService.java @@ -20,6 +20,9 @@ package com.juick.service; import com.juick.model.Message; import com.juick.model.Tag; import com.juick.model.User; + +import org.springframework.cache.annotation.CacheEvict; + import com.juick.model.NotifyOpts; import java.util.List; @@ -37,6 +40,7 @@ public interface SubscriptionService { List getUsersSubscribedToUserRecommendations(int uid, Message msg); + @CacheEvict(value = { "discussions" }, allEntries = true) boolean subscribeMessage(Message message, User user); boolean unSubscribeMessage(int mid, int vuid); diff --git a/src/main/java/com/juick/service/TagService.java b/src/main/java/com/juick/service/TagService.java index 96e5975b..d04aacbc 100644 --- a/src/main/java/com/juick/service/TagService.java +++ b/src/main/java/com/juick/service/TagService.java @@ -21,6 +21,7 @@ import com.juick.model.Tag; import com.juick.model.User; import com.juick.model.TagStats; import org.apache.commons.lang3.tuple.Pair; +import org.springframework.cache.annotation.Cacheable; import java.util.Collection; import java.util.List; @@ -45,6 +46,7 @@ public interface TagService { List getUserBLTags(int uid); + @Cacheable(value = "popular_tags") List getPopularTags(); List getTagStats(); diff --git a/src/main/java/com/juick/service/UserService.java b/src/main/java/com/juick/service/UserService.java index 06637aee..a3ab8315 100644 --- a/src/main/java/com/juick/service/UserService.java +++ b/src/main/java/com/juick/service/UserService.java @@ -25,6 +25,8 @@ import com.juick.model.ExternalToken; import com.juick.util.UsernameTakenException; import org.apache.commons.lang3.tuple.Pair; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import javax.annotation.Nonnull; import java.util.Collection; @@ -44,10 +46,12 @@ public interface UserService { String getSignUpHashByTelegramID(Long telegramId, String username); + @CacheEvict(value = "users_by_name", allEntries = true) Optional createUser(String username, String password) throws UsernameTakenException; Optional getUserByUID(int uid); + @Cacheable(value = "users_by_name") @Nonnull User getUserByName(String username); @Nonnull User getUserByEmail(String email); @@ -69,6 +73,7 @@ public interface UserService { Optional checkPassword(String username, String password); + @CacheEvict(value = "users_by_name", allEntries = true) boolean updatePassword(User user, String newPassword); int getUserOptionInt(int uid, String option, int defaultValue); @@ -174,16 +179,20 @@ public interface UserService { String getTelegramNameByHash(String hash); + @CacheEvict(value = "users_by_name", allEntries = true) boolean setFacebookUser(String hash, int uid); String getVKNameByHash(String hash); + @CacheEvict(value = "users_by_name", allEntries = true) boolean setVKUser(String hash, int uid); + @CacheEvict(value = "users_by_name", allEntries = true) boolean setTelegramUser(String hash, int uid); String getJIDByHash(String hash); + @CacheEvict(value = "users_by_name", allEntries = true) boolean setJIDUser(String hash, int uid); boolean canDeleteTelegramUser(User user); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index df575a3c..a1fc50a3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -6,5 +6,7 @@ spring.jackson.serialization.write-empty-json-arrays=true 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, discussions, messages, replies, popular_tags +spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s spring.profiles.active=@activatedProperties@ - diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index 367b8e47..1e956855 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -2331,18 +2331,18 @@ public class ServerTests { } @Test - public void verifiedUsersTest() { - assertThat(userService.getUserByName("ugnich").isVerified(), is(false)); - jdbcTemplate.update("INSERT INTO telegram(user_id, tg_id) VALUES(?, ?)", ugnich.getUid(), - "100001866137681"); - assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false)); - jdbcTemplate.update("INSERT INTO facebook(user_id, fb_id) VALUES(?, ?)", ugnich.getUid(), - "100001866137681"); - assertThat(userService.getUserByName("ugnich").isVerified(), is(true)); - assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(true)); - jdbcTemplate.update("DELETE FROM facebook"); - assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false)); - } + public void verifiedUsersTest() { + assertThat(userService.getUserByName("ugnich").isVerified(), is(false)); + jdbcTemplate.update("INSERT INTO telegram(user_id, tg_id) VALUES(?, ?)", ugnich.getUid(), "100001866137681"); + assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false)); + userService.addFacebookState("12345", "http://localhost"); + userService.createFacebookUser(12345, "12345", "5678", "ugnich"); + userService.setFacebookUser("12345", ugnich.getUid()); + assertThat(userService.getUserByName("ugnich").isVerified(), is(true)); + assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(true)); + jdbcTemplate.update("DELETE FROM facebook"); + assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false)); + } @Test @Disabled("FIXME: profile changed as expected, but cache is not refreshed or something") -- cgit v1.2.3