jiazx0107@163.com
2023-10-25 f01dc4edbe2c8689483787ea8e8d5ee2c54195df
src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java
@@ -4,6 +4,8 @@
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.utils.RedisConst;
import com.fzzy.api.utils.RedisUtil;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.service.repository.GatewayDeviceRep;
import org.springframework.beans.BeanUtils;
@@ -17,6 +19,8 @@
    @Resource
    private GatewayDeviceRep gatewayDeviceRep;
    @Resource
    private RedisUtil redisUtil;
    /**
@@ -45,7 +49,10 @@
        if (null == data.getId()) {
            data.setId(ContextUtil.getUUID());
        }
        gatewayDeviceRep.save(data);
        flushCache();
    }
    /**
@@ -58,6 +65,36 @@
        GatewayDevice data2 = new GatewayDevice();
        BeanUtils.copyProperties(data, data2);
        gatewayDeviceRep.delete(data2);
        flushCache();
        return null;
    }
    /**
     * gatewayDeviceService#flushCache
     */
    @Expose
    public void flushCache() {
        List<GatewayDevice> list = listAll();
        if (null == list || list.isEmpty()) return;
        String key;
        for (GatewayDevice device : list) {
            key = RedisConst.buildKey(RedisConst.KYE_GATE_DEVICE, device.getDeviceId());
            redisUtil.set(key, device);
        }
    }
    /**
     * 缓存获取设备信息
     *
     * @param deviceId
     * @return
     */
    public GatewayDevice getCacheDevice(String deviceId) {
        String key = RedisConst.buildKey(RedisConst.KYE_GATE_DEVICE, deviceId);
        return (GatewayDevice) redisUtil.get(key);
    }
}