Bladeren bron

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

yf_zn 10 maanden geleden
bovenliggende
commit
236227eef9

+ 26 - 21
src/main/java/com/yunfeiyun/agmp/iot/common/constant/devicetype/IotDeviceDictEnum.java

@@ -2,8 +2,6 @@ package com.yunfeiyun.agmp.iot.common.constant.devicetype;
 
 import com.yunfeiyun.agmp.common.constant.ErrorCode;
 import com.yunfeiyun.agmp.common.exception.BizException;
-import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
-import com.yunfeiyun.agmp.iot.common.service.TypeCoreService;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -177,8 +175,9 @@ public enum IotDeviceDictEnum {
      * @return
      */
     public String getName() {
-        String typeName = getTypeCoreService().getTypeLv2NameByTypeCode(this.code);
-        return typeName;
+//        String typeName = getTypeCoreService().getTypeLv2NameByTypeCode(this.code);
+//        return typeName;
+        throw new BizException(ErrorCode.FAILURE.getCode(), "该方法不可使用,请见TypeCoreService");
     }
 
     public String getServiceName() {
@@ -201,33 +200,39 @@ public enum IotDeviceDictEnum {
         return null;
     }
 
-    TypeCoreService getTypeCoreService() {
-        TypeCoreService typeCoreService = SpringUtils.getBean(TypeCoreService.class);
-        if (typeCoreService == null) {
-            throw new BizException(ErrorCode.FAILURE.getCode(), "获取typeCoreService需要再bean实例化之后");
-        }
-        return typeCoreService;
-    }
-    /**根据Code查找ServiceName*/
-    public static String findServiceNameByDevTypeBid(String devTypeBid){
-        for(IotDeviceDictEnum item:IotDeviceDictEnum.values()){
-            if(item.getCode().equals(devTypeBid)){
+//    TypeCoreService getTypeCoreService() {
+//        TypeCoreService typeCoreService = SpringUtils.getBean(TypeCoreService.class);
+//        if (typeCoreService == null) {
+//            throw new BizException(ErrorCode.FAILURE.getCode(), "获取typeCoreService需要再bean实例化之后");
+//        }
+//        return typeCoreService;
+//    }
+
+    /**
+     * 根据Code查找ServiceName
+     */
+    public static String findServiceNameByDevTypeBid(String devTypeBid) {
+        for (IotDeviceDictEnum item : IotDeviceDictEnum.values()) {
+            if (item.getCode().equals(devTypeBid)) {
                 return item.getServiceName();
             }
         }
         return "未知类型";
     }
-    /**根据ServiceName查找类型列表*/
-    public static List<String> findDevTypeBidByServiceName(String serviceName){
+
+    /**
+     * 根据ServiceName查找类型列表
+     */
+    public static List<String> findDevTypeBidByServiceName(String serviceName) {
         List<String> devTypeBid = new ArrayList<>();
-        for(IotDeviceDictEnum item:IotDeviceDictEnum.values()){
-            if(item.getServiceName().equals(serviceName)){
+        for (IotDeviceDictEnum item : IotDeviceDictEnum.values()) {
+            if (item.getServiceName().equals(serviceName)) {
                 devTypeBid.add(item.getCode());
             }
         }
-        if(devTypeBid.isEmpty()){
+        if (devTypeBid.isEmpty()) {
             return null;
-        }else{
+        } else {
             return devTypeBid;
         }
     }

+ 0 - 115
src/main/java/com/yunfeiyun/agmp/iot/common/service/DeviceconnCacheService.java

@@ -1,115 +0,0 @@
-package com.yunfeiyun.agmp.iot.common.service;
-
-import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONArray;
-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.domain.IotDevice;
-import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
-import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.stereotype.Service;
-import javax.annotation.Resource;
-import java.util.Set;
-
-@Service
-@Slf4j
-public class DeviceconnCacheService {
-    @Resource
-    private RedisTemplate redisTemplate;
-
-    public void setCache(IotDeviceconn iotDeviceconn){
-        log.info("【设备连接缓存】保存设备连接信息缓存,设备链接信息标识:{}",iotDeviceconn.getDevconnBid());
-        redisTemplate.opsForValue().set(getKey(RedisCacheKey.IOT_DEVICE_CONN)+iotDeviceconn.getDevconnBid(),iotDeviceconn);
-    }
-    public void deleteCache(IotDeviceconn iotDeviceconn){
-        log.info("【设备连接缓存】删除设备连接信息缓存,设备链接信息标识:{}",iotDeviceconn.getDevconnBid());
-        redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_CONN)+iotDeviceconn.getDevconnBid());
-    }
-    public IotDeviceconn getIotDeviceConnByDevconnBid(String devconnBid){
-        log.info("【设备连接缓存】查询设备连接信息,设备连接标识为:{}",devconnBid);
-        // 首先查询出当前设备的连接信息
-        IotDeviceconn iotDeviceconn = (IotDeviceconn) redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_CONN)+devconnBid);
-        if(null == iotDeviceconn){
-            log.error("【设备连接缓存】查询设备连接信息失败,设备连接标识为:{}",devconnBid);
-            throw new IotBizException(IotErrorCode.INVALID_DEVICE_CONN_BID);
-        }
-        return iotDeviceconn;
-    }
-
-    public IotDeviceconn getIotDeviceConnByIotDevice(IotDevice iotDevice){
-        return this.getIotDeviceConnByDevconnBid(iotDevice.getDevconnBid());
-    }
-    private String getKey(RedisCacheKey redisCacheKey){
-        return redisCacheKey.getPrefix() + ":" + redisCacheKey.getModuleType() + ":" + redisCacheKey.getName()+":";
-    }
-    /**
-     * 【mqtt】 通过设备连接标识获取mqtt链接标识
-     * */
-    public String getMqttConnectIdByDeviceConnBid(String devconnBid){
-        log.error("【设备连接缓存】查询mqtt链接标识,设备连接标识为:{}",devconnBid);
-        String connectId = (String) redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID)+devconnBid);
-        if(null == connectId){
-            log.error("【设备连接缓存】查询mqtt链接标识失败,设备连接标识为:{}",devconnBid);
-            throw new IotBizException(IotErrorCode.INVALID_DEVICE_CONN_BID);
-        }
-        return connectId;
-    }
-    /**
-     * 【mqtt】 关联设备连接标识和mqtt链接标识
-     * */
-    public void setMqttConnectionIdByConnBid(String devconnBid,String connectionId){
-        redisTemplate.opsForList().leftPush(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+connectionId,devconnBid);
-        redisTemplate.opsForValue().set(getKey(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID)+devconnBid,connectionId);
-    }
-    /**
-     * 【mqtt】删除mqtt链接标识
-     * */
-    public void deleteMqttConnectionId(String devconnBid){
-        redisTemplate.opsForList().remove(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+getMqttConnectIdByDeviceConnBid(devconnBid),1,devconnBid);
-        redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID)+devconnBid);
-    }
-    /**
-     * 【mqtt】判定mqtt链接标识是否还有绑定的设备连接标识
-     * */
-    public boolean mqttConnectionIdHasLink(String connectionId){
-       return redisTemplate.hasKey(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+connectionId);
-    }
-    /**
-     * 【http】保存租户的http通用连接信息
-     * */
-    public void setHttpCommonConnectionByDevtypeCode(String devconnBid,String devtypeCode){
-        redisTemplate.opsForList().leftPush(getKey(RedisCacheKey.IOT_HTTP_DEVICE_CONN_BID_LIST)+devtypeCode,devconnBid);
-    }
-    /**
-     * 【http】删除租户的http通用连接信息
-     * */
-    public void deleteMqttConnectionId(String devconnBid,String devtypeCode){
-        redisTemplate.opsForList().remove(getKey(RedisCacheKey.IOT_HTTP_DEVICE_CONN_BID_LIST)+devtypeCode,1,devconnBid);
-    }
-    /**
-     * 【http】判定http通用连接是否还有绑定的设备连接
-     * */
-    public boolean httpConnectionBidHasLink(String devtypeCode){
-       return redisTemplate.hasKey(getKey(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST)+devtypeCode);
-    }
-
-
-    public void cleanCache(){
-        // Mqtt设备连接数据标识列表
-        cleanCache(RedisCacheKey.IOT_MQTT_DEVICE_CONN_BID_LIST);
-        // Mqtt设备链接标识
-        cleanCache(RedisCacheKey.IOT_DEVICE_CONN_MQTT_ID);
-        // Mqtt设备连接信息标识
-        cleanCache(RedisCacheKey.IOT_DEVICE_CONN);
-    }
-    public void cleanCache(RedisCacheKey redisCacheKey){
-        Set<String> keySet = redisTemplate.keys(getKey(redisCacheKey)+"*");
-        for(String key : keySet){
-            redisTemplate.delete(key);
-        }
-    }
-
-}

+ 0 - 73
src/main/java/com/yunfeiyun/agmp/iot/common/service/TypeCacheService.java

@@ -1,73 +0,0 @@
-package com.yunfeiyun.agmp.iot.common.service;
-
-import com.alibaba.fastjson2.JSONObject;
-import com.yunfeiyun.agmp.common.enums.RedisCacheKey;
-import com.yunfeiyun.agmp.common.framework.manager.RedisCacheManager;
-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 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;
-
-    public void setCache(TosDevicetype tosDevicetype){
-        redisTemplate.opsForValue().set(getKey(RedisCacheKey.IOT_DEVICE_TYPE)+tosDevicetype.getDevtypeBid(),tosDevicetype);
-        redisTemplate.opsForValue().set(getKey(RedisCacheKey.IOT_DEVICE_TYPE)+tosDevicetype.getDevtypeCode(),tosDevicetype);
-    }
-    public void deleteCache(TosDevicetype tosDevicetype){
-        redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_TYPE)+tosDevicetype.getDevtypeBid());
-        redisTemplate.delete(getKey(RedisCacheKey.IOT_DEVICE_TYPE)+tosDevicetype.getDevtypeCode());
-    }
-
-    public TosDevicetype getCacheObjectByDevTypeCode(String devtypeCode){
-        Object tosDevicetype = redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_TYPE)+devtypeCode);
-        if(null == tosDevicetype){
-            throw new IotBizException(IotErrorCode.CACHE_NOT_FOUNT);
-        }
-        return JSONObject.parseObject(JSONObject.toJSONString(tosDevicetype), TosDevicetype.class);
-    }
-    public TosDevicetype getCacheObjectByDevTypeBid(String devtypeBid){
-        Object tosDevicetype = redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_TYPE)+devtypeBid);
-        if(null == tosDevicetype){
-            throw new IotBizException(IotErrorCode.CACHE_NOT_FOUNT);
-        }
-        return JSONObject.parseObject(JSONObject.toJSONString(tosDevicetype), TosDevicetype.class);
-    }
-    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();
-    }
-    public String getServiceNameByDevTypeBid(String devtypeBid){
-        TosDevicetype tosDevicetype = this.getCacheObjectByDevTypeBid(devtypeBid);
-        if(null == tosDevicetype){
-            throw new IotBizException(IotErrorCode.INVALID_DEVICE_TYPE);
-        }
-        IotDeviceDictEnum iotDeviceDictEnum = IotDeviceDictEnum.findEnumByCode(tosDevicetype.getDevtypeCode());
-        if(null == iotDeviceDictEnum){
-            throw new IotBizException(IotErrorCode.INVALID_DEVICE_TYPE);
-        }
-        return iotDeviceDictEnum.getServiceName();
-    }
-    
-    private String getKey(RedisCacheKey redisCacheKey){
-        return redisCacheKey.getPrefix() + ":" + redisCacheKey.getModuleType() + ":" + redisCacheKey.getName()+":";
-    }
-
-}

+ 0 - 31
src/main/java/com/yunfeiyun/agmp/iot/common/service/TypeCoreService.java

@@ -1,31 +0,0 @@
-package com.yunfeiyun.agmp.iot.common.service;
-
-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);
-    }
-
-}