| | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.util.LogUtils; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.constant.ResponseCodeEnum; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | |
| | | |
| | | @Autowired |
| | | LogUtils logUtils; |
| | | |
| | | @Autowired |
| | | RedisTemplate redisTemplate; |
| | | |
| | | @Override |
| | | @Transactional |
| | |
| | | logUtils.saveOperationForManage(request,content,Constants.INSERT_OPERATE_TYPE); |
| | | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); |
| | | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); |
| | | //刷新缓存 |
| | | refreshCache(); |
| | | return resultMap; |
| | | } |
| | | |
| | |
| | | logUtils.saveOperationForManage(request,content,Constants.UPDATE_OPERATE_TYPE); |
| | | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); |
| | | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); |
| | | //刷新缓存 |
| | | refreshCache(); |
| | | return resultMap; |
| | | } |
| | | |
| | |
| | | logUtils.saveOperationForManage(request,content,Constants.DELETE_OPERATE_TYPE); |
| | | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); |
| | | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); |
| | | //刷新缓存 |
| | | refreshCache(); |
| | | return resultMap; |
| | | } |
| | | |
| | |
| | | resultMap.put("totalPageNumber",totalPageNumber); |
| | | return resultMap; |
| | | } |
| | | |
| | | public Map<String,Sensor> getAllSensorFromCache(){ |
| | | Map<String,Sensor> sensors = redisTemplate.opsForHash().entries(RedisConstants.SENSOR_KEY); |
| | | if(ObjectUtils.isEmpty(sensors)) |
| | | return getAllSensorFromDB(); |
| | | return sensors; |
| | | } |
| | | |
| | | private Map<String,Sensor> getAllSensorFromDB(){ |
| | | QueryWrapper<Sensor> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | List<Sensor> sensors = sensorMapper.selectList(wrapper); |
| | | Map<String,Sensor> result = new HashMap<>(); |
| | | for (Sensor sensor : sensors) { |
| | | result.put(sensor.getCode(),sensor); |
| | | } |
| | | //刷新缓存 |
| | | refreshCache(result); |
| | | return result; |
| | | } |
| | | |
| | | private void refreshCache(){ |
| | | Map<String, Sensor> sensors = getAllSensorFromDB(); |
| | | refreshCache(sensors); |
| | | } |
| | | |
| | | private void refreshCache(Map<String,Sensor> sensors){ |
| | | //删除缓存 |
| | | redisTemplate.delete(RedisConstants.SENSOR_KEY); |
| | | //添加缓存 |
| | | redisTemplate.opsForHash().putAll(RedisConstants.SENSOR_KEY,sensors); |
| | | } |
| | | } |