aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-04-07 11:13:58 +0300
committerGravatar Vitaly Takmazov2022-12-08 08:09:37 +0300
commit912171165155ff78ad111c9c4043844c34567a42 (patch)
tree876278eb4338d73d294ef55f10471d08f2b3e38b
parenta6dff3324a55540877deaed23083ea502706a5ae (diff)
Configure caches
* caching is disabled because of OOMs in production
-rw-r--r--src/main/java/com/juick/API.java2
-rw-r--r--src/main/java/com/juick/config/WebConfig.java5
-rw-r--r--src/main/java/com/juick/service/EmailService.java3
-rw-r--r--src/main/java/com/juick/service/MessagesService.java7
-rw-r--r--src/main/java/com/juick/service/PrivacyQueriesService.java4
-rw-r--r--src/main/java/com/juick/service/SubscriptionService.java4
-rw-r--r--src/main/java/com/juick/service/TagService.java2
-rw-r--r--src/main/java/com/juick/service/UserService.java9
-rw-r--r--src/main/resources/application.properties4
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java24
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<Tag> 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<Integer> getPrivate(int uid, int before);
+ @Cacheable(value = "discussions", key="#uid + #to")
List<Integer> getDiscussions(int uid, Long to);
/**
@@ -111,10 +116,12 @@ public interface MessagesService {
List<Integer> getUserSearch(User visitor, int UID, String search, int privacy, int page);
+ @Cacheable(value = "messages", key="#visitor.uid + #mids.hashCode()")
List<Message> getMessages(User visitor, List<Integer> mids);
Map<Integer,Set<Reaction>> updateReactionsFor(final List<Integer> mid);
+ @Cacheable(value = "replies", key="#user.uid + #mid")
List<Message> 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<User> 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<String> getUserBLTags(int uid);
+ @Cacheable(value = "popular_tags")
List<String> getPopularTags();
List<TagStats> 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<User> createUser(String username, String password) throws UsernameTakenException;
Optional<User> 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<User> 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")