qilikuacha пре 4 година
родитељ
комит
cd03a3ed3e

+ 22 - 0
src/main/java/com/jtxt/demo/config/CacheBean.java

@@ -0,0 +1,22 @@
+package com.jtxt.demo.config;
+
+import com.jtxt.demo.model.LearningMaterial;
+import com.jtxt.demo.model.Question;
+import com.jtxt.demo.util.CacheStore;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.concurrent.TimeUnit;
+
+@Configuration
+public class CacheBean {
+    @Bean
+    public CacheStore<LearningMaterial> popularLearningMaterialCache() {
+        return new CacheStore<>(1, TimeUnit.HOURS);
+    }
+
+    @Bean
+    public CacheStore<Question> questionCache() {
+        return new CacheStore<>(1, TimeUnit.HOURS);
+    }
+}

+ 1 - 1
src/main/java/com/jtxt/demo/config/MongoConfig.java

@@ -22,7 +22,7 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
 
     @Override
     public MongoClient mongoClient() {
-        ConnectionString connectionString = new ConnectionString("mongodb://jtxt-admin:jtxt-admin@120.77.221.199:27017/?authSource=jtxt-dev");
+        ConnectionString connectionString = new ConnectionString("mongodb://jtxt-admin:jtxt-admin@localhost:27017/?authSource=jtxt-dev");
         MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                 .applyConnectionString(connectionString)
                 .build();

+ 11 - 0
src/main/java/com/jtxt/demo/model/Favorite.java

@@ -1,6 +1,7 @@
 package com.jtxt.demo.model;
 
 import org.springframework.data.annotation.Id;
+import org.springframework.data.annotation.Transient;
 import org.springframework.data.mongodb.core.mapping.Document;
 
 import java.util.ArrayList;
@@ -11,6 +12,8 @@ public class Favorite {
     @Id
     private String userId;
     private List<String> materials;
+    @Transient
+    private List<String> titles;
 
     public Favorite(String userId) {
         this.userId = userId;
@@ -32,4 +35,12 @@ public class Favorite {
     public void setMaterials(List<String> materials) {
         this.materials = materials;
     }
+
+    public List<String> getTitles() {
+        return titles;
+    }
+
+    public void setTitles(List<String> titles) {
+        this.titles = titles;
+    }
 }

+ 9 - 1
src/main/java/com/jtxt/demo/service/LearningMaterialServiceImpl.java

@@ -2,6 +2,7 @@ package com.jtxt.demo.service;
 
 import com.jtxt.demo.model.*;
 import com.jtxt.demo.repository.*;
+import com.jtxt.demo.util.CacheStore;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -22,6 +23,8 @@ public class LearningMaterialServiceImpl implements LearningMaterialService {
     private ReadHistoryRepository readHistoryRepository;
     @Autowired
     private CommentRepository commentRepository;
+    @Autowired
+    private CacheStore<LearningMaterial> materialCache;
 
     @Override
     public LearningMaterial getMaterialByName(String name) {
@@ -30,7 +33,12 @@ public class LearningMaterialServiceImpl implements LearningMaterialService {
 
     @Override
     public LearningMaterial getMaterialById(String id) {
-        return repository.findById(id).orElse(null);
+        LearningMaterial material = materialCache.get(id);
+        if (material == null) {
+            material = repository.findById(id).orElse(null);
+            if (material != null) materialCache.add(id, material);
+        }
+        return material;
     }
 
     @Override

+ 9 - 1
src/main/java/com/jtxt/demo/service/QuestionServiceImpl.java

@@ -3,6 +3,7 @@ package com.jtxt.demo.service;
 import com.jtxt.demo.model.Question;
 import com.jtxt.demo.model.QuestionCategory;
 import com.jtxt.demo.repository.QuestionRepository;
+import com.jtxt.demo.util.CacheStore;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -11,6 +12,8 @@ import java.util.List;
 public class QuestionServiceImpl implements QuestionService {
     @Autowired
     QuestionRepository questionRepository;
+    @Autowired
+    private CacheStore<Question> questionCache;
     @Override
     public Question createQuestion(Question question) {
         return questionRepository.insert(question);
@@ -51,7 +54,12 @@ public class QuestionServiceImpl implements QuestionService {
 
     @Override
     public Question findQuestionById(String id) {
-        return questionRepository.findById(id).orElse(null);
+        Question question = questionCache.get(id);
+        if (question == null) {
+            question = questionRepository.findById(id).orElse(null);
+            if (question != null) questionCache.add(id, question);
+        }
+        return question;
     }
 
     @Override

+ 6 - 0
src/main/java/com/jtxt/demo/service/UserServiceImpl.java

@@ -10,6 +10,7 @@ import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.stereotype.Service;
 
 import java.time.LocalDateTime;
+import java.util.ArrayList;
 import java.util.List;
 
 import static com.jtxt.demo.util.Constants.CHINA_ZONE;
@@ -22,6 +23,8 @@ public class UserServiceImpl implements UserService {
     @Autowired
     private FavoriteRepository favoriteRepository;
     @Autowired
+    private LearningMaterialService learningMaterialService;
+    @Autowired
     private MongoTemplate mongoTemplate;
     @Autowired
     private GroupToUserRepository groupToUserRepository;
@@ -153,6 +156,9 @@ public class UserServiceImpl implements UserService {
     @Override
     public Favorite showFavorites(User user) {
         Favorite favorite = favoriteRepository.findById(user.getUserName()).orElse(new Favorite(user.getUserName()));
+        List<String> materialTitles = new ArrayList<>();
+        favorite.getMaterials().forEach(mid -> materialTitles.add(learningMaterialService.getMaterialById(mid).getName()));
+        favorite.setTitles(materialTitles);
         return favorite;
     }
 

+ 31 - 0
src/main/java/com/jtxt/demo/util/CacheStore.java

@@ -0,0 +1,31 @@
+package com.jtxt.demo.util;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+
+import java.util.concurrent.TimeUnit;
+
+public class CacheStore<T> {
+    private Cache<String, T> cache;
+
+    //Constructor to build Cache Store with expiration time
+    public CacheStore(int expiryDuration, TimeUnit timeUnit) {
+        cache = CacheBuilder.newBuilder()
+                .expireAfterWrite(expiryDuration, timeUnit)
+                .concurrencyLevel(Runtime.getRuntime().availableProcessors())
+                .maximumSize(20)
+                .build();
+    }
+
+    //Method to fetch previously stored record using record key
+    public T get(String key) {
+        return cache.getIfPresent(key);
+    }
+
+    //Method to put a new record in Cache Store with record key
+    public void add(String key, T value) {
+        if(key != null && value != null) {
+            cache.put(key, value);
+        }
+    }
+}

+ 0 - 1
src/main/resources/application.properties

@@ -1,4 +1,3 @@
-server.port=7000
 spring.data.mongodb.host=localhost
 spring.data.mongodb.database=jtxt-dev
 spring.data.mongodb.port=27017