Просмотр исходного кода

redis业务管理:优化为内存管理,common的类拆分iotm和iots

yf_zn 10 месяцев назад
Родитель
Сommit
5b289bcd12

+ 111 - 0
src/main/java/com/yunfeiyun/agmp/iotm/cache/TypeCacheService.java

@@ -0,0 +1,111 @@
+package com.yunfeiyun.agmp.iotm.cache;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.common.enums.RedisCacheKey;
+import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
+import com.yunfeiyun.agmp.iot.common.domain.TosDevicetype;
+import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
+import com.yunfeiyun.agmp.iotm.web.service.ITosDevicetypeService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+/**
+ * 统一维护type(lv2)的缓存,增删改查
+ */
+@Service
+public class TypeCacheService {
+
+    @Resource
+    private RedisTemplate redisTemplate;
+
+    @Autowired
+    private ITosDevicetypeService tosDevicetypeService;
+
+    /**
+     * 将设备类型信息存入缓存中
+     *
+     * @param tosDevicetype 设备类型对象
+     */
+    public void setCache(TosDevicetype tosDevicetype) {
+        redisTemplate.opsForValue().set(getKey(RedisCacheKey.IOT_DEVICE_TYPE) + tosDevicetype.getDevtypeBid(), tosDevicetype);
+    }
+
+    /**
+     * 从缓存中删除指定设备类型的记录
+     *
+     * @param tosDevicetype 设备类型对象
+     */
+    public void deleteCache(TosDevicetype tosDevicetype) {
+        redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_TYPE) + tosDevicetype.getDevtypeBid());
+    }
+
+    /**
+     * 根据设备类型编码从缓存中获取设备类型对象,如果不存在,则从数据库中查找并存入缓存
+     *
+     * @param devtypeCode 设备类型编码
+     * @return TosDevicetype 设备类型对象
+     */
+    public TosDevicetype getCacheObjectByDevTypeCode(String devtypeCode) {
+        Object tosDevicetype = redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_TYPE) + devtypeCode);
+        if (null == tosDevicetype) {
+            tosDevicetype = tosDevicetypeService.selectTosDevicetypeByDevtypeCode(devtypeCode);
+
+            if (tosDevicetype == null) {
+                throw new IotBizException(IotErrorCode.CACHE_NOT_FOUNT);
+            }
+            setCache((TosDevicetype) tosDevicetype);
+        }
+        return JSONObject.parseObject(JSONObject.toJSONString(tosDevicetype), TosDevicetype.class);
+    }
+
+    /**
+     * 根据设备类型标识符从缓存中获取设备类型对象,实际上调用的是getCacheObjectByDevTypeCode方法
+     *
+     * @param devtypeBid 设备类型标识符
+     * @return TosDevicetype 设备类型对象
+     */
+    public TosDevicetype getCacheObjectByDevTypeBid(String devtypeBid) {
+        return getCacheObjectByDevTypeCode(devtypeBid);
+    }
+
+    /**
+     * 根据设备类型编码获取对应的服务名称
+     *
+     * @param devtypeCode 设备类型编码
+     * @return String 服务名称
+     */
+    public String getServiceNameByDevTypeCode(String devtypeCode) {
+        if (null == devtypeCode) {
+            throw new IotBizException(IotErrorCode.INVALID_DEVICE_TYPE);
+        }
+        IotDeviceDictEnum iotDeviceDictEnum = IotDeviceDictEnum.findEnumByCode(devtypeCode);
+        if (null == iotDeviceDictEnum) {
+            throw new IotBizException(IotErrorCode.INVALID_DEVICE_TYPE);
+        }
+        return iotDeviceDictEnum.getServiceName();
+    }
+
+    /**
+     * 根据设备类型标识符获取对应的服务名称,实际上调用的是getServiceNameByDevTypeCode方法
+     *
+     * @param devtypeBid 设备类型标识符
+     * @return String 服务名称
+     */
+    public String getServiceNameByDevTypeBid(String devtypeBid) {
+        return getServiceNameByDevTypeCode(devtypeBid);
+    }
+
+    /**
+     * 获取Redis缓存键值
+     *
+     * @param redisCacheKey 缓存键枚举
+     * @return String 完整的缓存键
+     */
+    private String getKey(RedisCacheKey redisCacheKey) {
+        return redisCacheKey.getPrefix() + ":" + redisCacheKey.getModuleType() + ":" + redisCacheKey.getName() + ":";
+    }
+}

+ 31 - 0
src/main/java/com/yunfeiyun/agmp/iotm/cache/TypeCoreService.java

@@ -0,0 +1,31 @@
+package com.yunfeiyun.agmp.iotm.cache;
+
+import com.yunfeiyun.agmp.iot.common.domain.TosDevicetype;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * 负责型号相关操作,用来替换之前的枚举写死的name获取相关
+ * 如果其他的,自行处理
+ */
+@Service
+public class TypeCoreService {
+
+    @Autowired
+    private TypeCacheService typeCacheService;
+
+    /**
+     * 获取型号(二级)的名称根据他的code
+     *
+     * @return
+     */
+    public String getTypeLv2NameByTypeCode(String typeCode) {
+        // 从缓存取出来
+        return getLv2ModelByLv2Code(typeCode).getDevtypeName();
+    }
+
+    public TosDevicetype getLv2ModelByLv2Code(String typeCode) {
+        return typeCacheService.getCacheObjectByDevTypeCode(typeCode);
+    }
+
+}

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iotm/device/common/service/impl/IotDeviceCommonServiceImpl.java

@@ -7,7 +7,7 @@ import com.yunfeiyun.agmp.common.utils.StringUtils;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
 import com.yunfeiyun.agmp.iot.common.domain.TosDevicetype;
 import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
-import com.yunfeiyun.agmp.iot.common.service.TypeCacheService;
+import com.yunfeiyun.agmp.iotm.cache.TypeCacheService;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceBaseCtlReqVo;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceBaseFunReqVo;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceBaseListReqVo;

+ 8 - 5
src/main/java/com/yunfeiyun/agmp/iotm/web/mapper/TosDevicetypeMapper.java

@@ -3,13 +3,14 @@ package com.yunfeiyun.agmp.iotm.web.mapper;
 
 import com.yunfeiyun.agmp.iot.common.domain.TosDevicetype;
 import com.yunfeiyun.agmp.iotm.web.domain.resvo.TosDevicetypeResVo;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
 
 /**
  * 设备类型Mapper接口
- * 
+ *
  * @author 杨晓辉
  * @date 2024-11-06
  */
@@ -24,7 +25,7 @@ public interface TosDevicetypeMapper {
 
     /**
      * 查询设备类型列表
-     * 
+     *
      * @param tosDevicetype 设备类型
      * @return 设备类型集合
      */
@@ -32,7 +33,7 @@ public interface TosDevicetypeMapper {
 
     /**
      * 新增设备类型
-     * 
+     *
      * @param tosDevicetype 设备类型
      * @return 结果
      */
@@ -40,7 +41,7 @@ public interface TosDevicetypeMapper {
 
     /**
      * 修改设备类型
-     * 
+     *
      * @param tosDevicetype 设备类型
      * @return 结果
      */
@@ -48,7 +49,7 @@ public interface TosDevicetypeMapper {
 
     /**
      * 删除设备类型
-     * 
+     *
      * @param id 设备类型主键
      * @return 结果
      */
@@ -65,4 +66,6 @@ public interface TosDevicetypeMapper {
      * @return 设备类型集合
      */
     public List<TosDevicetype> selectTosDevicetypeListyCreateDev(TosDevicetype tosDevicetype);
+
+    TosDevicetype selectTosDevicetypeByDevtypeCode(@Param("devtypeCode") String devtypeCode);
 }

+ 7 - 5
src/main/java/com/yunfeiyun/agmp/iotm/web/service/ITosDevicetypeService.java

@@ -11,7 +11,7 @@ import java.util.List;
 
 /**
  * 设备类型Service接口
- * 
+ *
  * @author 杨晓辉
  * @date 2024-11-06
  */
@@ -26,7 +26,7 @@ public interface ITosDevicetypeService {
 
     /**
      * 查询设备类型列表
-     * 
+     *
      * @param tosDevicetype 设备类型
      * @return 设备类型集合
      */
@@ -34,7 +34,7 @@ public interface ITosDevicetypeService {
 
     /**
      * 新增设备类型
-     * 
+     *
      * @param tosDevicetype 设备类型
      * @return 结果
      */
@@ -42,7 +42,7 @@ public interface ITosDevicetypeService {
 
     /**
      * 修改设备类型
-     * 
+     *
      * @param tosDevicetype 设备类型
      * @return 结果
      */
@@ -51,7 +51,7 @@ public interface ITosDevicetypeService {
 
     /**
      * 删除设备类型信息
-     * 
+     *
      * @param id 设备类型主键
      * @return 结果
      */
@@ -77,4 +77,6 @@ public interface ITosDevicetypeService {
      * @return 设备类型集合
      */
     public List<IotDevicetypeListResVo> listByCreateDev(TosDevicetype tosDevicetype);
+
+    TosDevicetype selectTosDevicetypeByDevtypeCode(String devtypeCode);
 }

+ 3 - 3
src/main/java/com/yunfeiyun/agmp/iotm/web/service/impl/IotDeviceServiceImpl.java

@@ -7,12 +7,14 @@ import com.yunfeiyun.agmp.common.utils.StringUtils;
 import com.yunfeiyun.agmp.iot.common.constant.mqtt.IotMqttConstant;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
 import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
+import com.yunfeiyun.agmp.iotm.cache.TypeCacheService;
+import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotHomeDeviceStatResVo;
+import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotHomeTypeStatResVo;
 import com.yunfeiyun.agmp.iot.common.enums.IotDeviceDelStatusEnum;
 import com.yunfeiyun.agmp.iot.common.enums.IotDeviceStatusTypeEnum;
 import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
 import com.yunfeiyun.agmp.iot.common.model.mq.IotDeviceEditMqModel;
 import com.yunfeiyun.agmp.iot.common.service.IotDeviceGeoLocationCommonService;
-import com.yunfeiyun.agmp.iot.common.service.TypeCacheService;
 import com.yunfeiyun.agmp.iotm.device.common.service.IotDeviceRefreshService;
 import com.yunfeiyun.agmp.iotm.mq.service.SendToIotsMsgService;
 import com.yunfeiyun.agmp.iotm.mq.service.SendToTosMsgService;
@@ -21,8 +23,6 @@ import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotDeviceEditReqVo;
 import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotDeviceListReqVo;
 import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotDeviceModifyReqVo;
 import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotDeviceListResVo;
-import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotHomeDeviceStatResVo;
-import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotHomeTypeStatResVo;
 import com.yunfeiyun.agmp.iotm.web.mapper.IotDeviceMapper;
 import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;
 import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceconnService;

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iotm/web/service/impl/IotDeviceconfigServiceImpl.java

@@ -10,7 +10,7 @@ import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
 import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconfig;
 import com.yunfeiyun.agmp.iot.common.model.cmd.CmdGroupModel;
 import com.yunfeiyun.agmp.iot.common.model.cmd.CmdModel;
-import com.yunfeiyun.agmp.iot.common.service.TypeCoreService;
+import com.yunfeiyun.agmp.iotm.cache.TypeCoreService;
 import com.yunfeiyun.agmp.iotm.web.mapper.IotDeviceconfigMapper;
 import com.yunfeiyun.agmp.iotm.web.service.IIotCmdtaskService;
 import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;

+ 7 - 1
src/main/java/com/yunfeiyun/agmp/iotm/web/service/impl/TosDevicetypeServiceImpl.java

@@ -6,7 +6,7 @@ import com.yunfeiyun.agmp.common.utils.StringUtils;
 import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
 import com.yunfeiyun.agmp.iot.common.domain.TosDevicetype;
 import com.yunfeiyun.agmp.iot.common.enums.IotDeviceconnTypeEnum;
-import com.yunfeiyun.agmp.iot.common.service.TypeCacheService;
+import com.yunfeiyun.agmp.iotm.cache.TypeCacheService;
 import com.yunfeiyun.agmp.iotm.mq.service.SendToIotsMsgService;
 import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotDevicetypeListResVo;
 import com.yunfeiyun.agmp.iotm.web.domain.resvo.TosDevicetypeResVo;
@@ -173,4 +173,10 @@ public class TosDevicetypeServiceImpl implements ITosDevicetypeService {
         }
         return iotDevicetypeListResVoList;
     }
+
+    @Override
+    public TosDevicetype selectTosDevicetypeByDevtypeCode(String devtypeCode) {
+
+        return tosDevicetypeMapper.selectTosDevicetypeByDevtypeCode(devtypeCode);
+    }
 }

+ 3 - 3
src/main/resources/application-dev.yml

@@ -23,7 +23,7 @@ server:
   # 服务器的HTTP端口,默认为8021
   port: 8034
   servlet:
-    # 应用的访问路径
+    # 应用的访问路径iotmprod-api
     context-path: /
   tomcat:
     # tomcat的URI编码
@@ -32,9 +32,9 @@ server:
     accept-count: 1000
     threads:
       # tomcat最大线程数,默认为200
-      max: 800
+      max: 1
       # Tomcat启动初始化的线程数,默认值10
-      min-spare: 100
+      min-spare: 1
 
 # 日志配置
 logging:

+ 6 - 1
src/main/resources/mapper/TosDevicetypeMapper.xml

@@ -169,5 +169,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </where>
         group by dt.devtypeBid
     </select>
+    <select id="selectTosDevicetypeByDevtypeCode"
+            resultType="com.yunfeiyun.agmp.iot.common.domain.TosDevicetype">
+               select * from TosDevicetype where devtypeCode=#{devtypeCode}
 
-</mapper>
+    </select>
+
+</mapper>