Pārlūkot izejas kodu

添加品牌redis,优化radio(v3.0)

JensionDzero 2 gadi atpakaļ
vecāks
revīzija
9434e2800b

+ 3 - 0
benyun-core/src/main/java/com/benyun/core/constant/RedisContant.java

@@ -40,4 +40,7 @@ public class RedisContant {
 
     public static String TOPOLOGICAL_COMMON_MAP = "topological_common_map";
     public static int TOPOLOGICAL_COMMON_MAP_TIME = 60*8;
+
+    public static String CHANNEL_ANALYSE_BRANDLIST = "channel_analyse_brandList";
+    public static int CHANNEL_ANALYSE_BRANDLIST_TIME = 60*8;
 }

+ 12 - 0
benyun-core/src/main/java/com/benyun/core/controller/ChannelAnalyseController.java

@@ -47,7 +47,19 @@ public class ChannelAnalyseController {
 
     @RequestMapping("/brandList")
     public R brandList(ChannelAnalyseAceeptVo channelAnalyseAceeptVo){
+        String md5 = channelAnalyseAceeptVo.getHash();
+
+        //2.查看redis中是否存在有缓存
+        PageInfo<Brand> wdCount = (PageInfo<Brand>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_BRANDLIST).get(md5);
+        if (wdCount != null) {
+            return R.ok(wdCount);
+        }
+
         PageInfo<Brand> brandPageInfo = channelAnalyseService.brandList(channelAnalyseAceeptVo);
+
+        //4.保存到redis中
+        redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_BRANDLIST).put(md5,brandPageInfo);
+        redisTemplate.expire(RedisContant.CHANNEL_ANALYSE_BRANDLIST,RedisContant.CHANNEL_ANALYSE_BRANDLIST_TIME, TimeUnit.MINUTES); //30分钟
         return R.ok(brandPageInfo);
     }
 

+ 5 - 2
benyun-core/src/main/java/com/benyun/core/service/impl/ChannelAnalyseServiceImpl.java

@@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.boot.origin.Origin;
 import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
 import java.util.*;
 
 @Service
@@ -261,7 +262,9 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
             else
                 tagAnalyse.setName("");
             tagAnalyse.setCount(hashMap.get(s));
-            tagAnalyse.setRadio((double) tagAnalyse.getCount() / total);
+            BigDecimal bigDecimal = new BigDecimal((double) tagAnalyse.getCount() / total);
+            double v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
+            tagAnalyse.setRadio(v);
             list.add(tagAnalyse);
         }
 
@@ -281,7 +284,7 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
             start=end;
             end+=6;
         }
-        result.put("pages",i);
+        result.put("pages",i-1);
         result.put("total",list.size());
         return result;
     }

+ 6 - 2
benyun-core/src/main/java/com/benyun/core/service/impl/ChannelMapServiceImpl.java

@@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -148,8 +149,11 @@ public class ChannelMapServiceImpl implements ChannelMapService {
 
         //5.计算radio
         for (WdCountBody wdCountBody : result) {
-            if (total != 0)
-                wdCountBody.setRadio((float) wdCountBody.getCount() / total);
+            if (total != 0){
+                BigDecimal bigDecimal = new BigDecimal((float) wdCountBody.getCount() / total);
+                float v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
+                wdCountBody.setRadio(v);
+            }
             else
                 wdCountBody.setRadio(0.0f);
         }