List<UserEo> userEoList = comDataComponent.getUserListByUid(); for (UserEo userEo : userEoList) { List<DataPermissionSchema> userDataPermissionSchema = getUserDataPermissionSchema(userEo.getUserUid(), userEo.getOrgCode()); //将当前用户的数据权限 存入redis key = SystemsConstant.DATA_PERMISSION_SCHEMA + UserUid redisTemplate.opsForValue().set(SystemsConstant.DATA_PERMISSION_SCHEMA + userEo.getUserUid(), ObjectUtil.toString(userDataPermissionSchema)); }优化代码
时间: 2024-02-03 20:03:33 浏览: 69
C#_List用法详解
可以考虑将redis操作改为批量操作,减少网络通信开销,代码如下:
```
List<UserEo> userEoList = comDataComponent.getUserListByUid();
Map<String, String> dataPermissionMap = new HashMap<>();
for (UserEo userEo : userEoList) {
List<DataPermissionSchema> userDataPermissionSchema = getUserDataPermissionSchema(userEo.getUserUid(), userEo.getOrgCode());
String key = SystemsConstant.DATA_PERMISSION_SCHEMA + userEo.getUserUid();
dataPermissionMap.put(key, ObjectUtil.toString(userDataPermissionSchema));
}
redisTemplate.opsForValue().multiSet(dataPermissionMap);
```
使用`multiSet`方法进行批量设置redis中的key-value对,将所有需要设置的key-value对先存入Map中,然后一次性进行批量设置,减少了网络通信的开销。同时,将redis中key的拼接放到循环外面进行,也可以减少循环中的重复计算。
阅读全文