wangzhe 2 лет назад
Родитель
Сommit
91ddbd44db

+ 9 - 4
mybusiness/src/main/java/com/sooka/system/gas/zenner_gas_purchase_center_infor/mapper/TGuijiZennerGasPurchaseCenterMapper.java

@@ -57,7 +57,6 @@ public interface TGuijiZennerGasPurchaseCenterMapper
      * 创建数据缓存(通用)
      *
      * @param cache 缓存名称,缓存值
-     * @return 数据数量
      */
     public void createCache(Map cache);
 
@@ -65,17 +64,23 @@ public interface TGuijiZennerGasPurchaseCenterMapper
      * 刷新数据缓存(通用)
      *
      * @param cache 缓存ID,缓存值
-     * @return 数据数量
      */
     public void refreshCache(Map cache);
 
     /**
+     * 更新数据事件(通用)
+     *
+     * @param cache 缓存ID,缓存值
+     */
+    public void updateCache(Map cache);
+
+    /**
      * 查询数据缓存(通用)
      *
      * @param cacheName 缓存名称
-     * @return 数据数量
+     * @return 数据数量,刷新时间
      */
-    public Long getCache(String cacheName);
+    public Map getCache(String cacheName);
 
     /**
      * 新增真兰收费系统-中心计费购气信息

+ 32 - 9
mybusiness/src/main/java/com/sooka/system/gas/zenner_gas_purchase_center_infor/service/impl/TGuijiZennerGasPurchaseCenterInforServiceImpl.java

@@ -9,6 +9,8 @@ import com.sooka.system.gas.zenner_gas_purchase_center_infor.service.ITGuijiZenn
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -58,29 +60,50 @@ public class TGuijiZennerGasPurchaseCenterInforServiceImpl implements ITGuijiZen
     {
 //        Long total = Convert.toLong(CacheUtils.get("guiji-data-count", tableName + "," + paramStr));//cacheName, cacheKey
         String cacheName = tableName + "," + paramStr;
-        Long total = mapper.getCache(cacheName);
-        if(total == null){
+//        Long total = mapper.getCache(cacheName);
+        Map cache = mapper.getCache(cacheName);
+        Long total;
+        if(cache == null){
             String msg = "21:17:55.281 [http-nio-8080-exec-27] INFO  o.a.s.c.e.EhCacheManager - [getCache,158] - Cache with name 'guiji-data-count' does not yet exist.  Creating now.";
             Map map = new HashMap();
             map.put("tableName", tableName);
             total = mapper.getDataCount(map);
+            map.put("updateTime", new Date());
             map.put("cacheName", cacheName);
             map.put("total", total);
             mapper.createCache(map);
             System.out.println("分页功能 缓存创建成功:" + tableName);
         }else {
+            total = Long.valueOf(cache.get("total").toString());
+//            Map map = new HashMap();
+//            map.put("tableName", tableName);
+//            total = mapper.getDataCount(map);
 //            total = Long.valueOf(cache.get("total").toString());
+//            final Long count = total;
             System.out.println("分页功能 根据缓存名称获取缓存:" + tableName);
             new Thread() {
                 public void run() {
                     try{
-                        Map map = new HashMap();
-                        map.put("tableName", tableName);
-                        Long total = mapper.getDataCount(map);
-                        map.put("cacheName", cacheName);
-                        map.put("total", total);
-                        mapper.refreshCache(map);
-                        System.out.println("分页功能 缓存刷新成功:" + tableName);
+                        String updateTime = cache.get("updateTime").toString();
+                        System.out.println(updateTime.split("\\.")[0] + "=updateTime");//2022-12-01 15:26:16.0
+                        boolean refresh = new Date().getTime() - new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(updateTime.split("\\.")[0]).getTime() > 10 * 60 * 1000;//最多10分钟刷新一次最新数据
+                        if(refresh){
+                            System.out.println("分页功能 缓存刷新中。。。:" + tableName);
+    //                        String updateTime = cache.get("updateTime").toString();
+    //                        System.out.println(updateTime + "=updateTime");
+                            Map map = new HashMap();
+                            map.put("updateTime", new Date());
+                            map.put("cacheName", cacheName);
+                            mapper.updateCache(map);//更新更新时间 防止多次操作
+    //                        System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + interfaceinfo.getInterfaceName() + "更新更新时间 防止多次操作");
+                            map.put("tableName", tableName);
+                            Long total = mapper.getDataCount(map);//select count(id) from ${tableName} 此处最为耗时
+                            map.put("total", total);
+                            mapper.refreshCache(map);
+                            System.out.println("分页功能 缓存刷新成功:" + tableName);
+                        }else {
+                            System.out.println("分页功能 缓存已是最新:" + tableName);
+                        }
                     }catch (Exception e){
                         e.printStackTrace();
                     }

+ 7 - 4
mybusiness/src/main/resources/mapper/system/TGuijiZennerGasPurchaseCenterInforMapper.xml

@@ -58,13 +58,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </select>
 
     <insert id="createCache" parameterType="java.util.Map">
-        insert into t_cache_guiji (cache_name, total) values (#{cacheName}, #{total})
+        insert into t_cache_guiji (cache_name, total, update_time) values (#{cacheName}, #{total}, #{updateTime})
     </insert>
     <update id="refreshCache" parameterType="java.util.Map">
-        update t_cache_guiji set total = #{total} where cache_name = #{cacheName}
+        update t_cache_guiji set update_time = #{updateTime}, total = #{total} where cache_name = #{cacheName}
     </update>
-    <select id="getCache" parameterType="java.lang.String" resultType="java.lang.Long">
-        select total from t_cache_guiji where cache_name = #{cacheName}
+    <update id="updateCache" parameterType="java.util.Map">
+        update t_cache_guiji set update_time = #{updateTime} where cache_name = #{cacheName}
+    </update>
+    <select id="getCache" parameterType="java.lang.String" resultType="java.util.Map">
+        select total, update_time updateTime from t_cache_guiji where cache_name = #{cacheName}
     </select>
 
     <insert id="insert" parameterType="TGuijiZennerGasPurchaseCenterInfor">