Browse Source

调整设备类型相关缓存

liuyaowen 1 year ago
parent
commit
64597e898f

+ 13 - 7
src/main/java/com/yunfeiyun/agmp/iot/common/service/TypeCacheService.java

@@ -7,6 +7,7 @@ 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;
@@ -16,27 +17,28 @@ import javax.annotation.Resource;
  */
 @Service
 public class TypeCacheService {
+
     @Resource
-    private RedisCacheManager redisCacheManager;
+    private RedisTemplate redisTemplate;
 
     public void setCache(TosDevicetype tosDevicetype){
-        redisCacheManager.setCacheObject(RedisCacheKey.IOT_DEVICE_TYPE,tosDevicetype.getDevtypeBid(),tosDevicetype);
-        redisCacheManager.setCacheObject(RedisCacheKey.IOT_DEVICE_TYPE,tosDevicetype.getDevtypeCode(),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){
-        redisCacheManager.deleteObject(RedisCacheKey.IOT_DEVICE_TYPE,tosDevicetype.getDevtypeBid());
-        redisCacheManager.deleteObject(RedisCacheKey.IOT_DEVICE_TYPE,tosDevicetype.getDevtypeCode());
+        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 = redisCacheManager.getCacheObject(RedisCacheKey.IOT_DEVICE_TYPE,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 = redisCacheManager.getCacheObject(RedisCacheKey.IOT_DEVICE_TYPE,devtypeBid);
+        Object tosDevicetype = redisTemplate.opsForValue().get(getKey(RedisCacheKey.IOT_DEVICE_TYPE)+devtypeBid);
         if(null == tosDevicetype){
             throw new IotBizException(IotErrorCode.CACHE_NOT_FOUNT);
         }
@@ -63,5 +65,9 @@ public class TypeCacheService {
         }
         return iotDeviceDictEnum.getServiceName();
     }
+    
+    private String getKey(RedisCacheKey redisCacheKey){
+        return redisCacheKey.getPrefix() + ":" + redisCacheKey.getModuleType() + ":" + redisCacheKey.getName()+":";
+    }
 
 }